Windows Mobile GPS serial port communication NMEA command and Data Processing (1) (C #)

Source: Internet
Author: User
In the past few days, GPS on WM wanted to obtain satellite data and found some information. There are two possible methods: 1, the Traditional GPS Programming Method reads data from the GPS device through the serial port, parses and processes the data. 2. Apply the gpsid function library provided by Windows Mobile to interview the Traditional GPS programming 1 for serial communication, first open the port of the GPS device and receive the data namespace using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Io. Ports; using GPS; // This is a type of GPS device. The following describes public nmeaprotocol protocol = new nmeaprotocol ();
Public SerialPort Port = new SerialPort (); system. text. encoding encoding = system. text. asciiencoding. getencoding (1252); connection event private void connectbutton_click (Object sender, system. eventargs E)
{
Connectbutton. Enabled = false;
Disconnectbutton. Enabled = true;
Comlistbox. Enabled = false;
Port. portname = comlistbox. selecteditem as string;
Port. Parity = parity. None;
Port. baudrate = 4800;
Port. stopbits = stopbits. One;
Port. databits = 8;
Port. open ();
Timer1.enabled = true;
} // Timer control event private void timerjavastick (Object sender, system. eventargs E)
{
Readdata ();
} // Read data
Private void readdata ()
{
Byte [] bdata = new byte [256]; try
{
// Bdata = serialhelper. Read ();
Port. Read (bdata, 0,256); Protocol. parsebuffer (bdata );
}
Catch (exception E)
{
System. Diagnostics. Debug. writeline (E. tostring ());
// Swallow it.
} // Display the data displaynmearawdata (bdata );
Displaygeneralinfo ();
Displaysatellites ();
} Private void displaynmearawdata (byte [] bdata)
{
String sdata = "";
If (null! = Bdata)
{
Sdata = encoding. getstring (bdata );
} If (dumprawdatacheck. Checked)
{
// If dumped 100 k of data get rid of the oldest 50 K
If (nmeatext. Text. length> 100*1000)
{
Nmeatext. Text = nmeatext. Text. substring (,);} nmeatext. Text = nmeatext. Text + sdata;
Nmeatext. selectionstart = nmeatext. Text. Length-1;
Nmeatext. scrolltocaret ();
}
} Private void displaygeneralinfo ()
{
Labellatitude. Text = protocol. gpgga. Latitude. tostring ();
Labellongpolling. Text = protocol. gpgga. longpolling. tostring ();
Labelaltitude. Text = protocol. gpgga. Altitude. tostring (); datetime UTC = datetime. minvalue; If (protocol. uplmc. Month! = 0 & Protocol. uplmc. Year! = 0 & Protocol. uplmc. Day! = 0)
{

UTC = new datetime (protocol. uplmc. Year + 2000, Protocol. uplmc. Month,
Protocol. uplmc. Day, Protocol. gpgga. Hour, Protocol. gpgga. Minute,
Protocol. gpgga. Second, datetimekind. UTC );
Labeldate. Text = UTC. tow.datestring ();
Labeltimelocal. Text = UTC. tolocaltime (). tostring ();
Labeltime. Text = UTC. tow.timestring ();
} Listgpsquality. selectedindex = (INT) protocol. gpgga. gpsquality; labelfixmode. Text = protocol. gpgsa. mode = 'A '? "Automatic": "Manual ";
Labelpdop. Text = protocol. gpgsa. pdop. tostring ();
Labelvdop. Text = protocol. gpgsa. vdop. tostring ();
Labelhdop. Text = protocol. gpgsa. hdop. tostring (); labeldatavalid. Text = protocol. uplmc. datavalid = 'A '? "Data valid": "navigation receive warning";} private void displaysatellites ()
{
Labelsatellitesinview. Text = protocol. gpgsv. satellitesinview. tostring ();

Pen circlepen = new pen (system. Drawing. color. darkblue, 1); graphics G = picsats. creategraphics ();

Int centerx = picsats. width/2;
Int centery = picsats. Height/2; double maxradius = (math. Min (picsats. Height, picsats. width)-20)/2; // draw circles
Double [] elevations = new double [] {0, math. PI/2, math. PI/3, math. PI/6}; foreach (double elevation in elevations)
{
Double Radius = (double) system. Math. Cos (elevation) * maxradius;
G. drawellipse (circlepen, (INT) (centerx-radius), (INT) (centery-radius), (INT) (2 * radius), (INT) (2 * radius ));
}
// 90 degrees elevation reticule
G. drawline (circlepen, new point (centerX-3, centery), new point (centerx + 3, centery ));
G. drawline (circlepen, new point (centerx, centerY-3), new point (centerx, centery + 3); pen satellitepen = new pen (system. drawing. color. lightgoldenrodyellow, 4); foreach (satellite sat in protocol. gpgsv. satellites. values)
{
// If has a listitem
Listviewitem lvitem = (listviewitem) sat. thing; If (null = lvitem)
{
Lvitem = new listviewitem
(
New String []
{
Sat. Id. tostring (),
Sat. elevation. tostring (),
Sat. azimuth. tostring (),
Sat. Used. tostring ()
}
); Listsatellites. Items. Add (lvitem );

Sat. Thing = lvitem; // lvitem;
}
Else
{
Lvitem. Text = SAT. Id. tostring ();
Lvitem. subitems [1]. Text = SAT. elevation. tostring ();
Lvitem. subitems [2]. Text = SAT. azimuth. tostring ();
Lvitem. subitems [3]. Text = SAT. Used. tostring ();
} // Draw satellites
Double H = (double) system. Math. Cos (Sat. elevation * Math. Pi)/180) * maxradius;

Int satx = (INT) (centerx + H * Math. Sin (Sat. azimuth * Math. Pi)/180 ));
Int saty = (INT) (centery-H * Math. cos (SAT. azimuth * Math. pi)/180); G. drawrectangle (satellitepen, satx, saty, 4, 4); G. drawstring (SAT. id. tostring (), new font ("verdana ",
8, fontstyle. Regular), new system. Drawing. solidbrush (color. Black), new
Point (satx + 5, saty + 5 ));
}
} Http://www.devdiv.com/home.php? MoD = Space & uid = 26317 & Do = Blog & id = 1187

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.