Home > Software engineering >  MFC serial port receive bytes extract?
MFC serial port receive bytes extract?

Time:09-29

My thought is to want to put the inside of the serial port to receive the byte data extracted, but I can't make it show, Debug, but you can see the corresponding variable is correct, strives for the great god action, the code is as follows:
 void CCommReceiveDlg: : OnCommReceive () 
{
//TODO: add message handler code here


Cstrings wendu;
//cstrings sFile=_T (" ABC. TXT ");
CStdioFile file;

Int nEvent=m_mscomm. Get_CommEvent ();//get event

If (nEvent==2)
{

The VARIANT variant_inp;
COleSafeArray safearray_inp;
Long len, k;
K=m_mscomm. Get_InBufferCount (); The receive buffer characters//
BYTE rxdata [92160]={};
If (k> 0)
{
Variant_inp=m_mscomm. Get_Input ();//read buffer
Safearray_inp=variant_inp;//the VARIANT type variables into ColeSafeArray type
Len=safearray_inp. GetOneDimSize ();//to get effective data length
For (k=0; KSafearray_inp. GetElement (& amp; K, rxdata + k);//is converted into a BYTE array, deposit rxdata
}

For (int j=0; j{
If (rxdata [j]==0 x6d & amp; & Rxdata + 9 [j]==0 x3c)//extraction temperature data
{
Char STR=rxdata + 6 [j];
Wendu. The Format (_T (" % d) ", STR);
M_VoltageDisplay=wendu;
}
}
}

The UpdateData (FALSE);

}

Probably is this meaning; Like every time I send 10 bytes, baotou is in the beginning, 0 x6d, tail is 0 x3c, 7th is my need to extract data, after extracted, according to the engineering code, rarely leave email I sent to the great god, action, genuflect is begged

CodePudding user response:

 for (int j=0; j{
If (rxdata [j]==0 x6d & amp; & Rxdata + 9 [j]==0 x3c)//extraction temperature data
{
Char STR=rxdata + 6 [j];
Wendu. The Format (_T (" % d) ", STR);
M_VoltageDisplay=wendu;
}
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
What do you mean the data here? The value of STR? You under the Debug debugging data correct? Had better not use char types, convert BYTE, char is a symbol of Numbers, range is 128 ~ 127, BYTE is unsigned, the range is 0 to 255. If the data is beyond the scope of char, may finally the value of the data is not what you want,

CodePudding user response:

reference 1st floor VisualEleven response:
 for (int j=0; j{
If (rxdata [j]==0 x6d & amp; & Rxdata + 9 [j]==0 x3c)//extraction temperature data
{
Char STR=rxdata + 6 [j];
Wendu. The Format (_T (" % d) ", STR);
M_VoltageDisplay=wendu;
}
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
What do you mean the data here? The value of STR? You under the Debug debugging data correct? Had better not use char types, convert BYTE, char is a symbol of Numbers, range is 128 ~ 127, BYTE is unsigned, the range is 0 to 255. If the data is beyond the scope of char, may be the last value is not what you want data,



Right, Debug, STR data become the correct data, wendu will be, too

CodePudding user response:

Since the data acquisition is no problem, that is the problem on the show? How do you show? Is to want to put m_VoltageDisplay values displayed in the UI?

CodePudding user response:

The
reference 3 floor VisualEleven response:
that no questions, since data acquisition that is the problem on the show? How do you show? Is to want to put m_VoltageDisplay values displayed in the UI?


M_VoltageDisplay is my EditControl control member variables, I let this EditControl directly show a constant, normal again,

CodePudding user response:

refer to the second floor AD70DDC1BA9 response:
Quote: refer to 1st floor VisualEleven response:

 for (int j=0; j{
If (rxdata [j]==0 x6d & amp; & Rxdata + 9 [j]==0 x3c)//extraction temperature data
{
Char STR=rxdata + 6 [j];
Wendu. The Format (_T (" % d) ", STR);
M_VoltageDisplay=wendu;
}
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
What do you mean the data here? The value of STR? You under the Debug debugging data correct? Had better not use char types, convert BYTE, char is a symbol of Numbers, range is 128 ~ 127, BYTE is unsigned, the range is 0 to 255. If the data is beyond the scope of char, may be the last value is not what you want data,



Right, Debug, STR data become the correct data, wendu will

Me a machine to send data is not less than 50, no more than 180. Is limited, the debug, the seventh data show is 85. The hexadecimal is 55, but the temperature is cstrings type, an int type, the data is right

CodePudding user response:

Streaming devices, normally, need a buffer to receive the data, but there is no process
Because the data may be read is part of the packet,
Such as \ x1 "12345678" \ x02 is a complete package, but if it is received more than once, such as \ x1 "1234" the second is "5678" \ x02
Then according to the above procedure will never couldn't parse the packet

CodePudding user response:

Data storage to rxdata, setting a breakpoint, check the correctness of rxdata data first and then ran the following, each sent 10 bytes, there may be, for the first time, after the completion of 10 bytes sent you at the time of processing data, also the second has been sent to come over, will lead to data loss situation,

Assume that A has been sent, B will have been closed, so your rxdata receives the data will be problems, you can use the ring A serial port to collect data,

CodePudding user response:

To teach, for the code, I also encountered this problem

CodePudding user response:

CodePudding user response:

Streaming will inevitably encounter problems:
1. Stick package
2. The sliding window

CodePudding user response:

Pay attention to the received data, must carry on the corresponding correct conversion, such as hexadecimal, hexadecimal conversion will do

CodePudding user response:

references to the tenth floor kakabulusi response:
streaming transmission will inevitably encounter problems:
1. Stick package
2. The sliding window

Need to pay attention to these problems, it is recommended to use TRACE tracking your wendu variables,

CodePudding user response:

Fyi:
//loop to a function every time send 200 bytes (this is the fixed) buffer, 
//a function need to incoming buffer, the circulation of 240 bytes (fixed) new buffer for processing,
//when dealing with each new printing takes two bytes of the buffer
# ifdef _MSC_VER
# pragma warning (4996) disable:
# endif
#include
#include
#include
# ifdef _MSC_VER
#include
  • Related