C # serial Operation series (4)--Protocol chapter, text Protocol data analysis

Source: Internet
Author: User

C # serial Operation series (4)--Protocol chapter, text protocol data analysisTags: c#uiobjectstringbyte2010-06-09 01:50 19739 people read review (+) collection report Classification:Communication Class library design (4)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The previous article has introduced the composition of the Protocol, a protocol, generally has: protocol header + length + data + checksum, text format can be intuitively defined carriage return line is the end of the protocol, so we can omit the length of the data, increase the end of the protocol. That is: protocol header + data + checksum + data tail.

Text-mode data is easier to analyze. If the data is cached, you can consider using StringBuilder. Or do not cache or can. Most text formatting data has a newline end. You can change it slightly. For example, analysis of common NMEA 0183 format satellite coordinate data gga.

$GPGGA, 121252.000,3937.3032,n,11611.6046,e,1,05,2.0,45.9,m,-5.7,m,,0000*77

$ start

Gpgga command Word

* End

77 Checksum

A little modification of the previous code is possible. The example is not posted. The text format is simple, just for the content to be complete. Paste for reference. Only the area of analysis is much simplified.

[C-sharp]View PlainCopy
  1. void comm_datareceived (object sender, Serialdatareceivedeventargs e)
  2. {
  3. if (Closing) return; If you are shutting down, ignore the operation, return directly, and complete the serial listener thread one loop as soon as possible.
  4. Try
  5. {
  6. Listening = true;   Set the tag to indicate that I have started working on the data and will use the system UI for a while.
  7. Text format is relatively simple, you can death.
  8. String line = Comm. ReadLine (); //This gets the carriage return to the end of the line. But it's not starting from scratch, it's going to check.
  9. /////////////////////////////////////////////////////////////////////////////////////////////////////////// //  
  10. //< Protocol Resolution >
  11. Because the recovered code is in the finally. You can return directly.
  12. if (line[0]! = ' $ ') return; Although it may be a bit of rubbish, the data is unimportant. Just throw it away. The follow-up is all right.
  13. int star = line.  IndexOf ("*", 1);
  14. if (star = =-1) return;
  15. Calculates the XOR based on the later data and compares it to the number following the *. If different, no analysis is performed. Because of a checksum error
  16. When the tail is determined to exist, the checksum is correct. You can analyze the data.
  17. //Analysis Data
  18. //Slightly
  19. //Because you want to access UI resources, you need to use Invoke to synchronize the UI.
  20. This . Invoke ((EventHandler) (delegate
  21. {
  22. //Determine if it is displayed as 16 forbidden
  23. if (checkboxhexview.checked)
  24. {
  25. //Stitching out 16 binary strings in turn
  26. foreach (byte b in buf)
  27. {
  28. Builder.  Append (b.tostring ("X2") + "");
  29. }
  30. }
  31. Else
  32. {
  33. //Translate directly into a string by ASCII rules
  34. Builder. Append (Encoding.ASCII.GetString (BUF));
  35. }
  36. //Append form to the end of the text box and scroll to the end.
  37. This.txGet.AppendText (builder.  ToString ());
  38. //Modify receive Count
  39. Labelgetcount.text = "Get:" + received_count.  ToString ();
  40. }));
  41. }
  42. finally
  43. {
  44. Listening = false;   I'm done, the UI can shut down the serial port.
  45. }
  46. }

Top
4
Step

C # serial Operation series (4)--Protocol chapter, text Protocol data analysis

Related Article

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.