This time to a serial port to send data encountered problems with PB, used in text way to send and receive, feel with mscomm is more convenient, it is very easy to use, but this time send in hexadecimal way, failures, and indeed the last to understand their own hexadecimal format of data and PB data sending way not understand thoroughly, the access to a large number of data, slowly understand, root cause is PB calls to memory data is short, and the use of PB in project, this aspect also used less, most still is given priority to with business data,
In hexadecimal way to send data, the key is to use a BLOB format stored data, personal understanding, somewhat similar to the c language pointer BLOB format is PB to memory data calls the most direct and convenient way,
Arrange the access to the data, and it's convenient, you refer to in this thank @ invincible triangle cat, @ xjdg0229, @ dazizai (free) post, benefit,
@ dazizai post (free) : questions about the original concept of the character set
https://www.sybasebbs.com/thread-89934-1-1.html
@ invincible triangle cat hexadecimal format in serial communication program to send and receive written realize this is not a PB, reference ideas
PC software (MFC) instruction that was sent to the three axis stepper motor controller is a hex way (namely hexadecimal way transmission, rather than the form of Ascii transmission, such as' 0 ', according to the Ascii way transmission is 48, and is transmitted in the form of hex 0,),
Just use MFC to write a collection and set parameters on the central air-conditioning console application, between control panel and PC through 485 turn a serial port and serial port turn USB communication circuits, programming problem is met a PC to send and receive data format, the processing of control panel can read the protocol is a set of hexadecimal number, such as "66 03 01 00 00 0 c 01 00 00 00 00 00 00 3 c 94" 3 e F5, PC application can parse is a set of hexadecimal number sent by control panel, and serial port communication is binary byte streams, send and receive buffer are char array, how send () function converts a string type of hexadecimal number to a corresponding decimal number coexist in the size of the buffer array? The following functions are available for reference:
HexChar function is the function of the hexadecimal characters in the ASCII code into the size of the corresponding number of hexadecimal @
//that is using the serial debugging assistants, select the check box in Hex way sent to, the characters in a text box are hexadecimal, before the conveyor belt need these characters into decimal form, and then to transfer, namely HexChar () function is implemented by function,
Char HexChar (char c)
{
If ((c>='0') & amp; & (c<='9'))
Return the c - '0';//in the hexadecimal character 0-9 into decimal, or 0 to 9
Else if ((c>='A') & amp; & (c<))
='F'The return - 'A' c + 10;//A - F in hexadecimal, corresponding to 11-16
Else if ((c>='a') & amp; & (c<))
='f'The return - 'a' c + 10;//a - f in hexadecimal, correspond and 11-16, case-insensitive
The else
Return 0 x10;//other returns 0 x10
}
Str2Hex function function is will such as "66 03... "In the form of a string with a space interval is converted to the corresponding hexadecimal number in a BYTE type (typdef unsigned char BYTE) in the array, data array as write a serial port to send buffer array, the actual application will modify the BYTE data type is char, also can use,
The is () function converts a string in the text box below, remove the Spaces, and then converts other characters to a char data type and length, on the array data, for the following serial transmission,
Int Str2Hex (cstrings STR, BYTE * data)
{
Int t, t1;
Int rlen=0, len=STR. GetLength ();
If (len==1)
{
Char h=STR [0];
T=HexChar (h);
Data [0]=(BYTE) t;
Rlen++;
}
//data. SetSize (len/2);
for(int i=0; i
Char l, h=STR;
If (h==' ')
{
i++;
continue;
}
i++;
If (i>=len)
break;
L=STR;
T=HexChar (h);
T1=HexChar (l);
If ((t==16) | | (t1==16))//D broken sentence? For a non problem? Method of problem & amp; # 161; The I? 16? System? Several oy
break;
The else
T=t * 16 + t1;
i++;
Data [rlen]=(BYTE) t;
Rlen++;
}
Return rlen;
}
For the data received, located in the receive buffer BYTE array RecBuf, if you want to the size of the corresponding displayed in hexadecimal, just can be to each element in the array in the following format conversion and string RecText, in hexadecimal display can be realized,
The following function is the function of the char data into hexadecimal output,
Cstrings RecText, STR;
for(int i=0; i
STR. The Format (" % 02 x, "RecBuf);//will receive type BYTE data into the corresponding hexadecimal
RecText. Append (STR);
}
@ xjdg0229 a serial port to send data - the difference between the string and hexadecimal send
When we use a serial port to send data in the first data will be sent/symbol is converted to the corresponding ASCII code, then the ASCII according to binary way a send a,
(note: the following images from https://blog.csdn.net/wityy/article/details/8234739)
The binary letters and Numbers in the memory should be ASCII table comparison, ASCII code is 65, is A hexadecimal is 0 x41, binary memory for 01000001
(memory is saved in a binary number, the 8 bit binary number is the corresponding decimal number of ASC code hex 0 x41 above said is in the middle of a transformation, no actual use, it is not actually stored in the memory of you can see, nor the ASCII table)
There is also a blogger said: "the A852010100000000A91A" refers to the hexadecimal data string of 0 ~ F, does not mean that the string, in programming implementation, the data string stored in a string is a need to convert the: [quote from http://blog.csdn.net/xhao014/article/details/6663738 please understand source author has engender]
The last attached to the code of the test,
Global function blob uf_str2hex (string ls_str);//====================================================================
///FUNCTION: uf_str2hex
///RETURNS: blob
//[ARGEMENTS] :
//value string ls_str need to convert hexadecimal number string
//
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
///DESCRIPTION: convert hexadecimal string style to hexadecimal number
//[AUTHOR] : Liuxj 2020.12.18
//=============================================================
Int li_i li_j, li_len
Int li_ret, temp
Blob lb_str, lb_retu
Byte lb_temp, lb_t [], lb_t2 []
Long ll_ret
Ls_str=trim (ls_str)
Li_len=len (ls_str)
Lb_str=Blob (ls_str EncodingUTF8!)
Ll_ret=1
For li_i=1 to li_len
Li_ret=getbyte (li_i lb_str, lb_temp)
Temp=int (lb_temp)
If the temp=32 then li_ret=0//char (32)=""
If li_ret=1 then
If temp & gt; 47 and temp & lt; 58 then
Lb_t [ll_ret]=byte (temp - 48)
Ll_ret + +
Elseif temp & gt; 64 and temp & lt; Then 71
Lb_t [ll_ret]=byte (temp - 55)//temp - 'A' + 10
Ll_ret + +
Elseif temp & gt; 96 and temp & lt; Then 103
Lb_t [ll_ret]=byte (temp - 87)//temp - 'a' + 10
Ll_ret + +
The else
//messagebox (" prompt ", "read the card error, please place CARDS, card read")
Lb_retu=blob (data errors, the hexadecimal data, "")
Return lb_retu
End the if
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull