Data reception of serial port:
(1) Define a character array, and then parse the whole frame of the data after it is received;
Chartransbuf[bufsize]; char_ptr transbufptr=transbuf, uint_32 transnum; uint_32 Totallen=0; Static Chartembuf[1]={0};/************************************* receive a frame of data, deposit Transbuf [] ********************************************/tembuf[0] =0; if(Transnum = _io_read (com_ptr/*file handle for serial device*/, Tembuf,/*read them*/ 1 /*read 1 bytes at a time*/))) { if(tembuf[0]==0x02) {transbuf[0]=tembuf[0]; totallen=0; totallen++;Continue;} //single-byte frame header, which is replaced directly when repeated//multi-byte frame header: Improper handling (multi-byte read together), easy to lose data if(transbuf[0]!=0x02)Continue;//Invalid data if(transbuf[0]==0x02&&tembuf[0]!=0x02&&tembuf[0]!=0x03)//valid data {
Transbuf[totallen]=tembuf[0]; Storing Data Totallen++; if(totallen>4000) {transbuf[0]=0x00; totallen=0; }//Data length is too long (data receive error) Continue; } // if(transbuf[0]==0x02&&tembuf[0]==0x03) {transbuf[totallen]=tembuf[0]; totallen++;}//Reception Complete /*0x02-------Frame Header 0x03 The end of a frame-------the flag continue---jump to the serial port to get the next byte of data*/Transnum=Totallen; Transbuf[totallen]=' /'; Totallen=0; } Else Continue;/*no data is read from the serial port*/
/* A frame of data is fully received;
* Next, according to the definition of the protocol, parse the frame command * /
(2) Segmented reception (parsing):
/******** Frame header is: 0xa0,0xb0,0xc0,0xd0****************/
Fread (Head1,1,1,Uart_dev);/*read 1-byte frame header 0xa0*/ if(head1[0] ==0xA0) {fread (head2,1,3, Uart_dev);/*Read the next 3 bytes of the frame header 0xb0,0xc0,0xd0*/ /*if the frame header is correct, continue to receive 6 bytes, respectively:*/ /*command type, brightness, data length, Color1,color2,color3*/ if((head2[0]==0xb0) && (head2[1]==0xC0) && (head2[2]==0xD0) {fread (Cmddata,1,6, Uart_dev);/*read 6 bytes of data from the serial port*/ } Else { Continue;/*Incorrect frame header*/ } } Else { Continue;/*read the next byte*/}
/* Frame header receive complete, next receive data, end frame, check frame */ /*analysize the command.*/ Switch(cmddata[0])/*Judging Command Types*/ { Case 'D':/*Real-time display*/fread (Datad,1, $, Uart_dev); //Depending on the type of command, the length of the data is different Fread (Tail,1,2, Uart_dev); //End frame, check frame if(tail[0]!=0xE0)Continue;/*End Frame*/
/*
* Next, do the validation, parse the command data, perform the appropriate action */
Comparison:
(1) Single-byte frame head vs Multi-byte frame header: For multi-byte frame header, in the above code, read 3 bytes at a time, there may be loss of data;
(2) Frame length is not fixed (and the difference is larger): Sub-receiving, may be better;
(3)
No good, no nausea, no body.
There is good and malicious action
Knowing good and knowing evil is conscience
To phenomena evil is to be good.
Serial Programming-C language