C # serial 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.

 

Void comm_datareceived (Object sender, serialdatareceivedeventargs e) <br/>{< br/> If (closing) return; // if it is disabled, ignore the operation and return directly, complete a loop of serial port listening threads as soon as possible <br/> try <br/> {<br/> listening = true; // set the flag, indicating that I have started to process data, the system UI will be used later. <Br/> // The text format is relatively simple. You can wait. <Br/> string line = comm. Readline (); // This returns the line break ending with the carriage return. However, if it is not from the beginning, check <br/> //////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /<br/> // <Protocol Resolution> <br/> // The recovered code is in finally. You can directly return <br/> If (line [0]! = '$') Return; // although it may be a bit spam, data is not important. You can discard it directly. All the subsequent operations are correct <br/> int star = line. indexof ("*", 1); <br/> If (Star =-1) return; <br/> // 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 </P> <p> // The verification is correct when the header and tail are determined to exist. You can analyze the data. <Br/> // analyze the data <br/> // slightly </P> <p> // to access the UI resources, use invoke to synchronize the UI. <Br/> This. invoke (eventhandler) (DeleGate <br/>{< br/> // determines whether it is displayed as 16 forbidden <br/> If (checkboxhexview. checked) <br/>{< br/> // concatenates a hex string in sequence <br/> foreach (byte B in BUF) <br/>{< br/> builder. append (B. tostring ("X2") + ""); <br/>}< br/> else <br/> {<br/> // convert the string to a string based on ASCII rules. <br/> builder. append (encoding. ASCII. getstring (BUF); <br/>}< br/> // append to the end of the text box and scroll to the end. <Br/> This. txget. appendtext (builder. tostring (); <br/> // modify the receipt count <br/> labelgetcount. TEXT = "Get:" + received_count.tostring (); <br/> })); <br/>}< br/> finally <br/> {<br/> listening = false; // when I use up, the UI can close the serial port. <Br/>}</P> <p>

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.