void TCPConnectTask() { if (IsInConnectTask) return; while (true) { IsInConnectTask = true; if (client != null) { try { client.Close(); } catch { ;} } client = new System.Net.Sockets.TcpClient(); try { client.Connect(endpoint); if (!client.Connected) throw new Exception(); sensorDev = new Comm.SirfDLE(SensorName, client.GetStream()); sensorDev.OnCommError += new Comm.OnCommErrHandler(OnCommError); sensorDev.OnReceiveText += new Comm.OnTextPackageEventHandler(OnReceiveText); Console.WriteLine(endpoint.Address + ",connected!"); this.IsConnected = true; break; } catch { this.IsConnected = false; Console.WriteLine(endpoint + " connect error!"); ; } System.Threading.Thread.Sleep(5000); } IsInConnectTask = false; }
public void ComConnect() { if (com != null) { try { com.Close(); com.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message + "," + ex.StackTrace); } } com = new System.IO.Ports.SerialPort(ComPort, baud, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One); com.Open(); byte[] tmp=new byte[512]; // while (com.BaseStream.Read(tmp,0, 512) == 512) ; sensorDev = new Comm.SirfDLE(SensorName, com.BaseStream); sensorDev.OnCommError += new Comm.OnCommErrHandler(OnCommError); sensorDev.OnReceiveText += new OnTextPackageEventHandler(OnReceiveText); //new Comm.OnTextPackageEventHandler(delDev_OnReceiveText); }