private void run() { setstate=States.Opening; this.reset_gps_vars(); DataReceived+= new DataReceivedEventHandler(GPS_DataReceived); if (!demomodeon) { if (this.autodiscovery) { if (!autodiscover()) { setstate=States.Stopped; return; } } } if (!demomodeon) { if (!isconnecteddevice()) { OnError(null,"Com Port "+comport+" Is Not On Device",""); setstate=States.Stopped; return; } if (state!=States.AutoDiscovery) { DetailedPortSettings portSettings = new HandshakeNone(); cp = new Port(comport,portSettings); cp.Settings.BaudRate=this.baudrate; cp.RThreshold=64; cp.InputLen=1; try { cp.Open(); } catch (Exception e) { OnError(e,"Could Not Open Com Port "+comport,""); cp.Dispose(); setstate=States.Stopped; return; } cp.OnError+=new Port.CommErrorEvent(cp_OnError); cp.DataReceived+=new Port.CommEvent(cp_DataReceived); } } if (state==States.Opening || state==States.AutoDiscovery) setstate=States.Running; if (demomodeon) { load_demo_data(); } else { while (state==States.Running) { Thread.Sleep(10); } if (cp.IsOpen) cp.Close(); cp.Dispose(); cp=null; } setstate=States.Stopped; }
/// <summary> /// Auto discover our GPS /// </summary> /// <returns>True if we have found a GPS</returns> private bool autodiscover() { Type searchbauds = typeof(OpenNETCF.IO.Serial.BaudRates); // get a list of devices foreach (string port in this.devices()) { foreach (OpenNETCF.IO.Serial.BaudRates bauds in OpenNETCF.EnumEx.GetValues(searchbauds)) { if ((bauds >= minbaudrate) && (bauds <= maxbaudrate)) { // initialize the port like in run proc DetailedPortSettings portSettings = new HandshakeNone(); cp = new Port(port,portSettings); cp.Settings.BaudRate = bauds; cp.RThreshold=64; cp.InputLen=1; OnAutoDiscovery( OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.Testing,port,cp.Settings.BaudRate); try { cp.Open(); } catch { OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.FailedToOpen,port,cp.Settings.BaudRate); } if (cp.IsOpen) { // if port open, invoke cp_DataReceived if bytes occur cp.DataReceived+=new Port.CommEvent(cp_DataReceived); if (state==States.Opening) setstate=States.Running; int cpt=0; while (state==States.Running && cpt <= 100) { // wait 1 seconds for the sentences Thread.Sleep(10); cpt++; } if (state==States.AutoDiscovery) { OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.Opened,port,cp.Settings.BaudRate); comport = port; baudrate = cp.Settings.BaudRate; autodiscovery=false; return true; } else { cp.Close(); setstate=States.Opening; } OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.Failed,port,cp.Settings.BaudRate); } cp.Dispose(); cp=null; } } } // default values OnAutoDiscovery(OpenNETCF.IO.Serial.GPS.AutoDiscoverStates.NoGPSDetected,comport,baudrate); autodiscovery=false; return false; }