[Switch] C # serial port operation series (4) -- protocol, text protocol data parsing

Source: Internet
Author: User

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
  1. Void comm_datareceived (Object sender, serialdatareceivedeventargs E)
  2. {
  3. 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.
  4. Try
  5. {
  6. Listening = true; // set the tag, indicating that I have started to process the data and will use the system UI later.
  7. // The text format is relatively simple, so you can wait.
  8. String line = comm. Readline (); // This will get the line break ending with the carriage return. But it's not from the beginning. Check again.
  9. //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////
  10. // <Protocol Resolution>
  11. // Because the recovered code is in finally. You can directly return
  12. 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.
  13. Int star = line. indexof ("*", 1 );
  14. If (Star =-1) return;
  15. // 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
  16. // Check whether the header and tail exist correctly. You can analyze the data.
  17. // Analyze data
  18. // Omitted
  19. // To access the UI resources, you must use the invoke method to synchronize the UI.
  20. This. Invoke (eventhandler) (delegate
  21. {
  22. // Determine whether the display is 16 forbidden
  23. If (checkboxhexview. Checked)
  24. {
  25. // Concatenate a hex string in sequence
  26. Foreach (byte B in BUF)
  27. {
  28. Builder. append (B. tostring ("X2") + "");
  29. }
  30. }
  31. Else
  32. {
  33. // Convert the string directly according to ASCII rules
  34. Builder. append (encoding. ASCII. getstring (BUF ));
  35. }
  36. // Append the object to the end of the text box and scroll to the end.
  37. This. txget. appendtext (builder. tostring ());
  38. // Modify the receipt count
  39. Labelgetcount. Text = "Get:" + received_count.tostring ();
  40. }));
  41. }
  42. Finally
  43. {
  44. Listening = false; // when I have used up, the UI can close the serial port.
  45. }
  46. }

[Switch] C # serial port operation series (4) -- protocol, text protocol data parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.