The previous article has introduced the composition of the Protocol. A protocol generally has the following features: protocol header + Length + Data + verification. The text format can intuitively define that carriage return and line feed are the end of the Protocol, therefore, we can omit the Data Length and add the Protocol tail. That is, protocol header + Data + validation + Data tail.
Text data is easier to analyze. If data is cached, use stringbuilder. Or do not cache. Most text format data has a line break ending. Make a slight modification. For example, the common NMEA 0183 format satellite coordinate data GGA is analyzed.
$ Gpgga, 121252.000, 3937.3032, N, 11611.6046, E, 2.0, 45.9, 5.7, M,-0000, M, * 77
$ Start
Gpgga command
* End
77 Verification
Make a slight modification to the previous code. The example is not pasted. The text format is simple, just to complete the content. Post for reference. The analysis is much simpler.
[C-sharp]View plaincopy
- Void comm_datareceived (Object sender, serialdatareceivedeventargs E)
- {
- If (closing) return; // if it is being closed, ignore the operation and return directly. Complete a loop of the serial port listening thread as soon as possible.
- Try
- {
- Listening = true; // set the tag, indicating that I have started to process the data and will use the system UI later.
- // The text format is relatively simple, so you can wait.
- String line = comm. Readline (); // This will get the line break ending with the carriage return. But it's not from the beginning. Check again.
- //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////
- // <Protocol Resolution>
- // Because the recovered code is in finally. You can directly return
- If (line [0]! = '$') Return; // although it may be a bit spam, data is not important. You can discard it directly. All follow-up requests are correct.
- Int star = line. indexof ("*", 1 );
- If (Star =-1) return;
- // Calculate the variance or check based on the data behind $ and compare it with the number following. If they are different, no analysis is performed. Verification Error
- // Check whether the header and tail exist correctly. You can analyze the data.
- // Analyze data
- // Omitted
- // To access the UI resources, you must use the invoke method to synchronize the UI.
- This. Invoke (eventhandler) (delegate
- {
- // Determine whether the display is 16 forbidden
- If (checkboxhexview. Checked)
- {
- // Concatenate a hex string in sequence
- Foreach (byte B in BUF)
- {
- Builder. append (B. tostring ("X2") + "");
- }
- }
- Else
- {
- // Convert the string directly according to ASCII rules
- Builder. append (encoding. ASCII. getstring (BUF ));
- }
- // Append the object to the end of the text box and scroll to the end.
- This. txget. appendtext (builder. tostring ());
- // Modify the receipt count
- Labelgetcount. Text = "Get:" + received_count.tostring ();
- }));
- }
- Finally
- {
- Listening = false; // when I have used up, the UI can close the serial port.
- }
- }
[Switch] C # serial port operation series (4) -- protocol, text protocol data parsing