public void doDisconnect(LinkInterface comPort) { log.Info("We are disconnecting"); try { comPort.BaseStream.DtrEnable = false; comPort.Close(); } catch (Exception ex) { log.Error(ex); } // now that we have closed the connection, cancel the connection stats // so that the 'time connected' etc does not grow, but the user can still // look at the now frozen stats on the still open form try { System.Threading.ThreadPool.QueueUserWorkItem((WaitCallback) delegate { try { aeromagtec.Log.LogSort.SortLogs(Directory.GetFiles(Settings.Instance.LogDir, "*.tlog")); } catch { } } ); } catch { } this.MenuConnect.Image = global::aeromagtec.Properties.Resources.light_connect_icon; }
public void doConnect(LinkInterface comPort, string portname) { bool skipconnectcheck = false; switch (portname) { case "preset": skipconnectcheck = true; break; case "TCP": comPort.BaseStream = new TcpSerial(); break; case "UDP": comPort.BaseStream = new UdpSerial(); break; case "UDPCl": comPort.BaseStream = new UdpSerialConnect(); break; case "AUTO": // do autoscan Comms.CommsSerialScan.Scan(true); DateTime deadline = DateTime.Now.AddSeconds(50); while (Comms.CommsSerialScan.foundport == false) { System.Threading.Thread.Sleep(100); if (DateTime.Now > deadline || Comms.CommsSerialScan.run == 0) { CustomMessageBox.Show(Strings.Timeout); return; } } return; default: comPort.BaseStream = new SerialPort(); break; } // Tell the connection UI that we are now connected. //_connectionControl.IsConnected(true); // Here we want to reset the connection stats counter etc. comPort.MAV.cs.ResetInternals(); //cleanup any log being played comPort.logreadmode = false; if (comPort.logplaybackfile != null) { comPort.logplaybackfile.Close(); } comPort.logplaybackfile = null; try { log.Info("Set Portname"); // set port, then options if (portname.ToLower() != "preset") { comPort.BaseStream.PortName = portname; } log.Info("Set Baudrate"); try { if (baud != "" && baud != "0") { comPort.BaseStream.BaudRate = int.Parse(baud); } } catch (Exception exp) { log.Error(exp); } // prevent serialreader from doing anything comPort.giveComport = true; log.Info("About to do dtr if needed"); // reset on connect logic. if (Settings.Instance.GetBoolean("CHK_resetapmonconnect") == true) { log.Info("set dtr rts to false"); comPort.BaseStream.DtrEnable = false; comPort.BaseStream.RtsEnable = false; comPort.BaseStream.toggleDTR(); } comPort.giveComport = false; // setup to record new logs try { Directory.CreateDirectory(Settings.Instance.LogDir); lock (this) { // create log names var dt = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"); var tlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar + dt + ".tlog"; var rlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar + dt + ".rlog"; // check if this logname already exists int a = 1; while (File.Exists(tlog)) { Thread.Sleep(1000); // create new names with a as an index dt = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "-" + a.ToString(); tlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar + dt + ".tlog"; rlog = Settings.Instance.LogDir + Path.DirectorySeparatorChar + dt + ".rlog"; } //open the logs for writing comPort.logfile = new BufferedStream(File.Open(tlog, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)); comPort.rawlogfile = new BufferedStream(File.Open(rlog, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)); log.Info("creating logfile " + dt + ".tlog"); } } catch (Exception exp2) { log.Error(exp2); CustomMessageBox.Show(Strings.Failclog); } // soft fail // reset connect time - for timeout functions connecttime = DateTime.Now; // do the connect comPort.Open(skipconnectcheck); if (!comPort.BaseStream.IsOpen) { log.Info("comport is closed. existing connect"); try { IsConnected = false; UpdateConnectIcon(); comPort.Close(); } catch { } return; } // set connected icon this.MenuConnect.Image = displayicons.disconnect; } catch (Exception ex) { log.Warn(ex); try { IsConnected = false; UpdateConnectIcon(); comPort.Close(); } catch (Exception ex2) { log.Warn(ex2); } CustomMessageBox.Show("Can not establish a connection\n\n" + ex.Message); return; } }