NMEA Protocol host computer C # (GO)

Source: Internet
Author: User

Source: NMEA Protocol host computer C #

Some time ago wrote two with the NMEA protocol of the upper machine, here to do a summary and record. To share with you, but also for the future will not forget.

NMEA protocol in general, relatively simple, is a relatively mature weather protocol.

There are several main parameters and their formats:

    1. Wind speed and wind direction;

    2. Air temperature;

    3. Soil temperature;

    4. Humidity

    5. Atmospheric pressure

Note:

Where the efficacy of the main use of the XOR: that is, from the first character to ' * ' before a character is different or. The resulting data is 4 bits high and the ASCII code is 4 bits low.

<cr>: ' \ r '

<lf>: ' \ n '

Baud Rate: 4800

Refresh Rate: 1Hz

In the design of the host computer, all the receiving methods are passive. So the process is very good to do, only need to read the specific data bits can be converted to data.

Since each piece of data ends with ' \ n ', the serial Interrupt serialportx_datareceived () directly uses Serialportx.readline () to read the data. After reading, the data can be analyzed according to the protocol. Here's My Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMeteorological Monitoring _nmea{classNMEA {#regionVariable Public floatSpeed =0f;  Public floatDirection =0f;  Public floatAirtemp =0f;  Public floatSoiltemp =0f;  Public floatHumidity =0f;  Public floatBarometer =0f;  Public BOOLSpeedstatus =false; Private Const intMax_length = -; Private ReadOnly byteSum_end = Convert.tobyte ('*'); Private ReadOnly byteSum_start = Convert.tobyte ('$'); Private ReadOnly byteNum_0_ascii = Convert.tobyte ('0'); Private ReadOnly byteNum_a_ascii = Convert.tobyte ('A'-Ten); #endregion        #regionProperty Public floatGetSpeed {Get            {                returnSpeed ; }            Set{ speed=value; }        }         Public floatgetdirection {Get            {                returndirection; }            Set{Direction=value; }        }         Public BOOLGetStatus {Get            {                returnSpeedstatus; }            Set{speedstatus=value; }        }         Public floatGetairtemp {Get            {                returnairtemp; }            Set{airtemp=value; }        }         Public floatGetsoiltemp {Get            {                returnsoiltemp; }            Set{soiltemp=value; }        }         Public floatgethumidity {Get            {                returnhumidity; }            Set{Humidity=value; }        }         Public floatGetbarometer {Get            {                returnBarometer; }            Set{Barometer=value; }        }        #endregion        #regionChild functions/// <summary>///Digital to ASCII/// </summary>/// <param name= "Integer" >single bit integer</param>/// <returns>ASCII</returns>         Public byteInteger2char (intInteger) {            byteLcv_ch =0; if(Integer <=9) {lcv_ch= Convert.tobyte (Integer +num_0_ascii); }            Else if(Integer >=0x0A) && (Integer <=0x0F) ) {lcv_ch= Convert.tobyte (Integer +num_a_ascii); }            returnlcv_ch; }        #endregion        /// <summary>///Sum Validation/// </summary>/// <param name= "Array" >Validation Array</param>/// <returns>The checksum value, the character is split into two ASCII integer and an int, high in int 8 bits, low after</returns>         Public intCheckSum (byte[] Array) {            bytesum =0; intres =0; inti;  for(i =1; (Array[i]! = sum_end) && (i < max_length); i++) {sum^=Array[i]; }            if(I! =max_length) Res= (Integer2char (sum >>4)) <<8) | Integer2char (Sum &0xF); returnRes; }        /// <summary>///from the received string, remove the useful data/// </summary>/// <param name= "str" >the received string</param>         Public voidDataprocess (stringstr) {            Char[] Chsplit = {',',}; string[] Strarray =Str.            Split (Chsplit); Switch(strarray[0])            {                 Case "$WIMWV": Direction=float. Parse (strarray[1]); speed=float. Parse (strarray[3]); Char[] Charray = strarray[5].                    ToCharArray (); Speedstatus= (charray[0] =='A'?true:false);  Break;  Case "$WIMTA": Airtemp=float. Parse (strarray[1]);  Break;  Case "$WIMTS": Soiltemp=float. Parse (strarray[1]);  Break;  Case "$WIMHU": Humidity=float. Parse (strarray[1]);  Break;  Case "$WIMMB": Barometer=float. Parse (strarray[3]);  Break; default: Break; }        }        /// <summary>///the correctness of the data received is judged/// </summary>/// <param name= "str" >the received string</param>/// <returns>checksum normal returns True</returns>         Public BOOLReceivecheck (stringstr) {            BOOLres =false; Char[] Chsplit = {'*',}; string[] Strarray =Str.            Split (Chsplit); if(Strarray.length = =2)            {                if(strarray[1]. Length = =3)//Normal length                {                    byte[] array = Encoding.Default.GetBytes (strarray[1]); intCheck =CheckSum (Encoding.Default.GetBytes (str)); if(Check! =0)                    {                        if(check = = (array[0] <<8) | array[1]) {res=true; }                    }                }            }            returnRes; }    }}

NMEA Protocol host computer C # (GO)

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.