Home > Software engineering >  MFC serial hexadecimal receiving problem
MFC serial hexadecimal receiving problem

Time:10-02

Write a serial port to receive the upper machine, the character to receive normal, how can I receive hexadecimal data and displayed in the edit box, the buffer of data is "\ x1f \ XFF" this, should be displayed in the edit box 1 f ff, looked at other people's code like this
 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);//type into a BYTE array
For (k=0; K{
BYTE bt=* (char *) (rxdata + k);//character
If (m_ctrlHexDiaplay GetCheck ())
Strtemp. The Format (" % 02 x ", bt);//in hexadecimal characters into temporary variables strtemp deposit, note here to join an empty partition
The else
Strtemp. The Format (" % c ", bt);//the character into temporary variables strtemp store

M_EditReceive +=strtemp;//add receive corresponding string edit box
}
but can appear the situation of the program crashes, o great god solve will buffer the hexadecimal data read is shown in the edit box,

CodePudding user response:

There may be cross-border cases

 
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
BOOL bHexFlag=m_ctrlHexDiaplay. GetCheck ();
For (k=0; K{
BYTE bt;
Safearray_inp. GetElement (& amp; K, & amp; Bt);//type into a BYTE array
If (bhexFlag)
Strtemp. The Format (" % 02 x ", bt);//in hexadecimal characters into temporary variables strtemp deposit, note here to join an empty partition
The else
Strtemp. The Format (" % c ", bt);//the character into temporary variables strtemp store
M_EditReceive +=strtemp;//add receive corresponding string edit box
}

CodePudding user response:

 variant_inp=m_mscomm. Get_Input ();//read buffer 
Safearryay_inp=variant_inp;//the VARIANT type variables into ColeSafeArray type
Len=safearryay_inp. GetOneDimSize ();//to get effective data length
//to accept data
For (k=0; K{
Safearryay_inp. GetElement (& amp; K, rxdata + k);//type into a BYTE array
}
Sleep (1);
For (k=0; K{
BYTE bt=* (char *) (rxdata + k);//character
Stremp. The Format (" % 02 x ", bt);
M_EditReceive +=stremp;
}

Code debug mode do not have what problem, at the time of the error, click on the lower left corner of the call stack, and then double-click down on, see the problem of the line of code, the line is that you can understand the wrong
  • Related