To read:
Private voidSerialport1_datareceived (Objectsender, Serialdatareceivedeventargs e) { intn = serialport1.bytestoread;//record first, avoid some reason, man-made reason, operation time between times, cache inconsistentRx_cnt + = n;//increase receive Count int_frame_num =0;//The index of the received frame in the frame array (the type of data received) int_frame_len =0;//Frame Length if(N >20000)return;//the data received is too long and may be wrongSerialport1.read (Data_buf, Data_num, n);//reads buffered data, starts reading from Data_num (unread) of data_buf (buffer), joins with previously unhandled dataData_num + =N; intI =0;//I start at 0 each time I process the receive. while(I < Data_num-Ten)//iterate over the received data,//Data_num last unhandled data length. { if(Data_buf[i] = =0xaa&& Data_buf[i +1] ==0xaa)//Frame Header{_frame_len= Data_buf[i +2];//frame Length () 0A_frame_num = Data_buf[i +3];//the index (function) of the received frame in the frame array if(Data_num-i-5) >= _frame_len)//Data Reception completed { bytesum =0;//Checksum for(intj = I; J <= I +3+ _frame_len; J + +)//Calculate sumSum + = Data_buf[j];//and the if(sum = = Data_buf[i +4+ _frame_len])//The sum check passes, { intJ; Switch(_frame_num)//The index of the received frame in the frame array (the type of data received) { Case 1://STATUSJ= I +4; S_acc_x= (float) ((Int16) (Data_buf[j] <<8| Data_buf[j +1])) / -;//byte processingS_acc_y = (float) ((Int16) (Data_buf[j +2] <<8| Data_buf[j +3])) / -;//2 bytes//D_ang_yaw = (float) ((Int16) (Data_buf[j + 4] << 8 | data_buf[j + 5])///s_acc_1= (Int16) (Data_buf[j +6] <<8| Data_buf[j +7]);//2 bytesS_acc_5 = (Int32) (Data_buf[j +8] << -| Data_buf[j +9] << -| Data_buf[j +Ten] <<8| Data_buf[j + One]);//4 bytes Break; default: Break; } I= I +5+ _frame_len;//I point to the next frame of data } Else//sum check did not pass{I++; } } Else//HEAD Fun Len meets the requirements, but the data is not received { for(intj = I; J <= Data_num-1; J + +) {data_buf[j-I] =Data_buf[j]; } data_num= Data_num-I; return; } } Else//HEAD Fun LEN does not meet the requirements{I++; } } if(I < Data_num)//The few bytes left are not processed, and a byte is processed, and I is incremented by 1. Data_num Length not processed { for(intj = I; J <= Data_num-1; J + +) {data_buf[j-I] = data_buf[j];//put the data that has not been processed to the front of the array. } data_num= Data_num-I; } }
C # serial Port full receiver program