using System; using System.Drawing; using System.Collections; using System.Windows.Forms; using System.Data; using System.ComponentModel; namespace SerialPortNoEventsCS { /// Summary description for Form1. public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button bFix; private System.Windows.Forms.Button bStatus; private System.Windows.Forms.Button bQuality; private System.Windows.Forms.Button bMovement; private System.Windows.Forms.Button bSatellites; private System.Windows.Forms.ListBox boxResults; private System.Windows.Forms.Button bStart; private System.Windows.Forms.Button bStop; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.ComboBox ddPort; private System.Windows.Forms.ComboBox ddBaudRate; private System.Windows.Forms.Label label1; private GpsToolsNET.NmeaParser objParser; public Form1() { // Required for Windows Form Designer support InitializeComponent(); } /// Clean up any resources being used. protected override void Dispose( bool disposing ) { base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.bStart = new System.Windows.Forms.Button(); this.bFix = new System.Windows.Forms.Button(); this.bStatus = new System.Windows.Forms.Button(); this.bQuality = new System.Windows.Forms.Button(); this.bMovement = new System.Windows.Forms.Button(); this.bSatellites = new System.Windows.Forms.Button(); this.boxResults = new System.Windows.Forms.ListBox(); this.bStop = new System.Windows.Forms.Button(); this.ddPort = new System.Windows.Forms.ComboBox(); this.ddBaudRate = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); // // bStart // this.bStart.Location = new System.Drawing.Point(152, 244); this.bStart.Size = new System.Drawing.Size(80, 20); this.bStart.Text = "Start"; this.bStart.Click += new System.EventHandler(this.bStart_Click); // // bFix // this.bFix.Location = new System.Drawing.Point(152, 100); this.bFix.Size = new System.Drawing.Size(80, 24); this.bFix.Text = "Position"; this.bFix.Click += new System.EventHandler(this.bFix_Click); // // bStatus // this.bStatus.Location = new System.Drawing.Point(152, 136); this.bStatus.Size = new System.Drawing.Size(80, 24); this.bStatus.Text = "Status"; this.bStatus.Click += new System.EventHandler(this.bStatus_Click); // // bQuality // this.bQuality.Location = new System.Drawing.Point(152, 72); this.bQuality.Size = new System.Drawing.Size(80, 24); this.bQuality.Text = "Quality"; this.bQuality.Click += new System.EventHandler(this.bQuality_Click); // // bMovement // this.bMovement.Location = new System.Drawing.Point(152, 44); this.bMovement.Size = new System.Drawing.Size(80, 24); this.bMovement.Text = "Speed"; this.bMovement.Click += new System.EventHandler(this.bMovement_Click); // // bSatellites // this.bSatellites.Location = new System.Drawing.Point(152, 16); this.bSatellites.Size = new System.Drawing.Size(80, 24); this.bSatellites.Text = "Satellites"; this.bSatellites.Click += new System.EventHandler(this.bSatellites_Click); // // boxResults // this.boxResults.Location = new System.Drawing.Point(8, 16); this.boxResults.Size = new System.Drawing.Size(128, 223); // // bStop // this.bStop.Location = new System.Drawing.Point(152, 220); this.bStop.Size = new System.Drawing.Size(80, 20); this.bStop.Text = "Stop"; this.bStop.Click += new System.EventHandler(this.bStop_Click); // // ddPort // this.ddPort.Location = new System.Drawing.Point(152, 168); this.ddPort.Size = new System.Drawing.Size(80, 21); // // ddBaudRate // this.ddBaudRate.Location = new System.Drawing.Point(152, 192); this.ddBaudRate.Size = new System.Drawing.Size(80, 21); // // label1 // this.label1.Location = new System.Drawing.Point(8, 244); this.label1.Size = new System.Drawing.Size(64, 16); this.label1.Text = "C#"; // // Form1 // this.Controls.Add(this.label1); this.Controls.Add(this.ddBaudRate); this.Controls.Add(this.ddPort); this.Controls.Add(this.boxResults); this.Controls.Add(this.bSatellites); this.Controls.Add(this.bMovement); this.Controls.Add(this.bQuality); this.Controls.Add(this.bStatus); this.Controls.Add(this.bFix); this.Controls.Add(this.bStop); this.Controls.Add(this.bStart); this.Menu = this.mainMenu1; this.MinimizeBox = false; this.Text = "GpsTools .NETcf"; this.Load += new System.EventHandler(this.Form1_Load); } #endregion /// The main entry point for the application. static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { // Replace the licenceKey value with the received one GpsToolsNET.License license = new GpsToolsNET.License(); license.LicenseKey = "E805AEE18FC3A89A6C75"; // NmeaParser has core functionality to access NMEA data objParser = new GpsToolsNET.NmeaParser(); //Do not use events objParser.NoEvents = true; bStop.Enabled = false; bStart.Enabled = true; init_port_baudrate_dd(); // Occurs when the form is closing this.Closing += new System.ComponentModel.CancelEventHandler(Form1_Closing); } private void bStart_Click(object sender, System.EventArgs e) { try { // ComPort sets the number of the serial port on which // GPS is used, as selected by user objParser.ComPort = (short) ddPort.SelectedIndex; if(ddBaudRate.SelectedIndex == 0) { objParser.BaudRate = 0; } else { string s = ddBaudRate.SelectedItem.ToString(); //Returns a baud rate of the serial port in use objParser.BaudRate = Convert.ToInt32(s); } //Start communication with GPS objParser.PortEnabled = true; bStop.Enabled = true; bStart.Enabled = false; } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void bStop_Click(object sender, System.EventArgs e) { try { objParser.PortEnabled = false; bStop.Enabled = false; bStart.Enabled = true; } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void bFix_Click(object sender, System.EventArgs e) { try { GpsToolsNET.GpsFix objFix = null; GpsToolsNET.Position objPos = null; //Time out 0 seconds. Get 2D fix (longitude, latitude) objFix = objParser.GetGpsFix(0, 2); boxResults.Items.Clear(); if(objFix == null) { boxResults.Items.Add("not available"); } else { //Declare objPos as Position object objPos = objFix.Position; //Print out longitude, latitude and altitude strings boxResults.Items.Add("Lat: " + objPos.LatitudeString(0)); boxResults.Items.Add("Lon: " + objPos.LongitudeString(0)); boxResults.Items.Add(objPos.Altitude(0).ToString()); short d, m; double s; string h; objPos.LatitudeDMS(out d, out m, out s, out h); // boxResults.Items.Add(h + d.ToString() + " " + m.ToString() + " " + s.ToString()); objPos.LongitudeDMS(out d, out m, out s, out h); // boxResults.Items.Add(h + d.ToString() + " " + m.ToString() + " " + s.ToString()); double lat, lon; lat = objPos.LatitudeRads * 180 / Math.PI; // Decimal degrees lon = objPos.LongitudeRads * 180 / Math.PI; boxResults.Items.Add(lat.ToString()); boxResults.Items.Add(lon.ToString()); // See reference manual for list of all supported national grids // (map projections) objPos.Grid = GpsToolsNET.Grid.UTM_NORTH; // Use UTM boxResults.Items.Add("Northing: " + objPos.Northing.ToString()); boxResults.Items.Add("Easting: " + objPos.Easting.ToString()); boxResults.Items.Add("Zone: " + objPos.Zone); boxResults.Items.Add("FixType: " + objFix.FixType); DateTime dt = objFix.UTC; if(dt.Year != 1) { // Date available boxResults.Items.Add("UTC date: " + objFix.UTC.ToShortDateString()); } else { boxResults.Items.Add("UTC date: N/A"); } boxResults.Items.Add("UTC time: " + objFix.UTC.ToShortTimeString()); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void bStatus_Click(object sender, System.EventArgs e) { try { GpsToolsNET.ComStatus objStatus; // Get status of the serial link to the GPS objStatus = objParser.GetComStatus(); boxResults.Items.Clear(); // Check if GPS is found and it has a valid GPS fix boxResults.Items.Add("Connected: " + objStatus.ValidNmea.ToString()); if(objStatus.ComPort == -1) { // GpsGate Direct boxResults.Items.Add("GpsGate Direct"); } else { // Baud rate boxResults.Items.Add("BaudRate: " + objStatus.BaudRate.ToString()); // COM port boxResults.Items.Add("COM" + objStatus.ComPort.ToString() + ":" ); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void bQuality_Click(object sender, System.EventArgs e) { try { //Quality contains information about dilution of precision GpsToolsNET.Quality objQuality = objParser.GetQuality(0); boxResults.Items.Clear(); if(objQuality == null) { boxResults.Items.Add("not available"); } else { //horizontal dilution of precision (in meters) boxResults.Items.Add("HDOP: " + objQuality.HDOP.ToString()); //vertical dilution of precision (in meters) boxResults.Items.Add("VDOP: " + objQuality.VDOP.ToString()); //dilution of precision (in meters) boxResults.Items.Add("PDOP: " + objQuality.PDOP.ToString()); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void bMovement_Click(object sender, System.EventArgs e) { try { // Movement object contains information about speed and heading GpsToolsNET.Movement m = objParser.GetMovement(0); boxResults.Items.Clear(); if(m == null) { boxResults.Items.Add("not available"); } else { // Returns the ground speed (0 - as knots) boxResults.Items.Add("Speed: " + m.Speed(0).ToString()); // Returns in degrees in what direction GPS is moving boxResults.Items.Add("Heading: " + m.Heading.ToString()); // Returns the difference in degrees between a real compass and heading boxResults.Items.Add("Variation: " + m.MagneticVariation.ToString()); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void bSatellites_Click(object sender, System.EventArgs e) { try { //Get satellite information (0 miliseconds time out) GpsToolsNET.Satellites sats = objParser.GetSatellites(0); GpsToolsNET.Satellite s; boxResults.Items.Clear(); if(sats == null) { boxResults.Items.Add("not available"); } else { //Gets number of satellites (between 1 and 12) boxResults.Items.Add("Count: " + sats.Count.ToString()); short inx; for(inx = 1; inx <= sats.Count; inx++) { //Item represents a collection of all satellites detected by GPS s = sats.Item(inx); //ID of satellite (see ID) is also often refered to as PRN (number between 1 and 99) //If ID returns 0 it is not a valid satellite //SNR is Signal to Noise Ration for the signal from satellite (number between 0 and 99) //GPS typically starts using a satellite when its SNR value is higher than 25 boxResults.Items.Add("ID: " + s.ID.ToString() + " SNR: " + s.SNR.ToString()); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // You need to stop the serial port (if active) before the form is destroyed // Or else NmeaParser will fire events to the destroyed window. objParser.Dispose(); Application.Exit(); } private void init_port_baudrate_dd() { // Init serial port dropdown short i; ddPort.Items.Add("Auto"); for(i = 1; i < 10; i++) { ddPort.Items.Add("COM" + i + ":"); } ddPort.SelectedIndex = 0; // Init Baud rate drop down ddBaudRate.Items.Add("Auto"); ddBaudRate.Items.Add("4800"); ddBaudRate.Items.Add("9600"); ddBaudRate.Items.Add("19200"); ddBaudRate.Items.Add("14400"); ddBaudRate.Items.Add("38400"); ddBaudRate.Items.Add("57600"); ddBaudRate.SelectedIndex = 0; } } }