How to process packets in serial communication programs ◆ Gong Jianwei Technology Home ◆ Gong Jianwei 2001.10.30 In serial port communication programs, often receive packets, often ask netizens asked how to extract data from these packets, how to deal with school In this article, I will give two examples to explain that the program shows VC 6.0. For details on serial programming, see another article on my homepage. At the same time, this article is also suitable for the processing of hard data packets in other communication programs. First, it should be noted that all of these processes are made in the serial port event handler onCommunication (). A serial communication event is triggered whenever one or more characters in the serial port buffer, the event drives (call) the serial port event communication processing function oncommunication (), where the received data can be processed, extract the required data. . Two examples, one is a more simple bit data format process, the other is the process of NMEA wireless communication format, and finally answers a netizen to question, everyone can also explore.
1. Problem: A packet whose series is a character, the character value is 7EH (16) '~', followed by a character 'e', then the data string, the tail is also the character value is A character of 7EH: 即 ~ exxxxxx ... ~ How to deal with this data? We still use the oncomm () processing in one of the serial debug helper source and its detailed programming process as an example: void cscommtestdlg :: oncomm () {// Todo: add your control notification handler code herevariant variant_inp; crasafeArray SafeArray_inp; Long Len , K; BYTE RXDATA [2048]; // Set Byte array an 8-bit integerthat is not sign; if (m_ctrlcomm.getCommEvent () == 2) // The event value is 2 indicates that the receiving buffer has characters. {The following you can join the processing code variant_inp = m_ctrlcomm.getinput (); // read buffer SafeArray_inp = variant_inp; // variant type variable converted to COLESAFEARRAY Type Variable LEN = SafeArray_inp.getonedIMSIZE (); // Get Valid data length for (k = 0; k
After getting the verification value, convert it to the ASCII character.
2.3 Program cstring m_strreceived; cstring m_strchecksum; int flag; char CH is the character m_strreceived = (char) ch; switch (ch) {case '$' each time, CHECKSUMFLAG = 0; Break; Case '*': flag = 2; C2 = CHECKSUM & 0x0F; C1 = ((Checksum >> 4) & 0x0f); if (C1 <10) C1 = '0'; ELSE C1 = 'A' - 10; IF (C2 <10) C2 = '0'; ELSE C2 = 'A' - 10; Break; Case CR: Break; Case LF: M_StrRreceived [Port-1] .empty (); Break; Default: IF (flag> 0) {m_strChecksum = ch; if (flag == 1) {strCheck = strCheck c1 c2; if (strCheck = m_strChecksum!) {m_strReceived.Empty ();} else {strInstruction = m_strReceived [port- 1] .left (6); if (Strinstruction == "$ qgoku) // If the string is correct {char * temp = (char *) ((lpctstr) m_strreceived); // Conversion INT speed = (atoi (Temp 7))); // Extract INT data char splevel = * (Temp 25); // Extract CHAR type data}} m_strchecksum.empty ();} flag -;} elseChecksum = checksum ^ ch; Break;} 3 Netizen questions, I answered a question of a netizen, everyone can also explore: The problem is as follows 3: I use your serial program to receive the hexadecimal data: 00 10 10 c0 00 f0 F0 AB AD I have to take a high four digits now, that is, 011c0ffaaa (I will not be implemented, but I have implemented it with Left, it is the character, not the value I want), I want to put it. 011c0ff Makes the following processing 011 converted into a decimal C unchanged 0FF also turns a decimal display, 17 C 255 Answer: Right shift 011c0 After ff, it can be placed in a character variable cstring m_strreceive: then convert it: char * TEMP = ((lpctstr) m_strreceive; char TBUF [6]; // Temporary Viable TBUF [0] = TEMP [1]; TBUF [1] = Temp [2]; TBUF [2] = Temp [3]; TBUF [3] = 0; // 011 Last 0 indicates end int DATA1 = ATOI (TBUF); char chDATA2 == TEMP [4]; // CTBUF [0] = Temp [5]; TBUF [1] = Temp [6]; TBUF [2] = Temp [7]; TBUF [3] = 0; int Data3 = ATOI (TBUF); // 0FF or more Data1, ChData2, Data3 is the data you want