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

Time:10-03

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:

Fyi:
 void HexDump) (char * buf, int len, int addr) {
Int I, j, k;
Char binstr [80].

For (I=0; iIf (0==16) (I %) {
Sprintf (binstr, "% x - 08", I + addr);
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
} else if (15==16) (I %) {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
Sprintf (binstr, "% s", binstr);
For (j=I - 15; j<=I; J++) {
Sprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
} else {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
}
}
If (0! 16)=(I %) {
16 k=16 - (I %);
For (j=0; jSprintf (binstr, "% s", binstr);
}
Sprintf (binstr, "% s", binstr);
K=16 - k;
For (j=I - k; jSprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
}
}

CodePudding user response:

Fyi:
Int CSCommTestDlg: : String2Hex (cstrings STR, CByteArray & amp; Senddata publishes the event)
{
Int hexdata lowhexdata;
Int hexdatalen=0;
Int len=STR. GetLength ();
Senddata publishes the event. SetSize (len/2);
for(int i=0; i{
Char LSTR, HSTR=STR [I];
If (HSTR==' ')
{
i++;
continue;
}
i++;
If (i>=len)
break;
LSTR=STR [I];
hexdata=https://bbs.csdn.net/topics/ConvertHexChar (HSTR);
lowhexdata=https://bbs.csdn.net/topics/ConvertHexChar (LSTR);
If ((hexdata=https://bbs.csdn.net/topics/=16) | | (lowhexdata==16))
break;
The else
hexdata=https://bbs.csdn.net/topics/hexdata * 16 + lowhexdata;
i++;
Senddata publishes the event [hexdatalen]=(char) hexdata;
Hexdatalen++;
}
Senddata publishes the event. The SetSize (hexdatalen);
Return hexdatalen;
}
//this is a character is converted to the corresponding function of hexadecimal values
//can find a lot of C language book
//function: if the character between 0 F, is converted to the corresponding hexadecimal character, otherwise return 1
Char CSCommTestDlg: : ConvertHexChar (char ch)
{
If ((ch>='0') & amp; & (ch<='9'))
Return ch - 0 x30;
Else if ((ch>='A') & amp; & (ch<))
='F'Return ch - 'A' + 10;
Else if ((ch>='a') & amp; & (ch<))
='f'Return ch - 'a' + 10;
The else return (1);
}

Send the code over there:
The UpdateData (TRUE);//read the edit box content
If (m_ctrlHexSend GetCheck ())
{
CByteArray hexdata;
Int len=String2Hex (m_strTXData hexdata);//return here len sent can be used to calculate how many hexadecimal number
M_ctrlComm. SetOutput (COleVariant (hexdata));//send the hexadecimal data
}
The else
M_ctrlComm. SetOutput (COleVariant (m_strTXData));//sending ASCII character data
  • Related