private void WriteClick() { s = ConState.write; dataerrer = true; issessce = false; Common.Common.massage = ""; //writeP(); if (PortCommunication.ComName == null) { mondelName.Text = "Select Port ..."; GetComList(); //获取端口 if (PortCommunication.ComName == null) { MessageBox.Show("Please Select Port ..."); foreach (Control item in this.Controls) { item.Enabled = true; } return; } } //lg = new Loading(); //lg.Location = new Point(this.Width / 2 - lg.Width / 2, this.Height / 2 - lg.Height / 2); //lg.TopLevel = false; //lg.Parent = this; try { PortCommunication.NewSerialPort(); PortCommunication.openPort(); } catch (Exception) { MessageBox.Show("The port failed to open, please check the port!"); foreach (Control item in this.Controls) { item.Enabled = true; } return; } conn = new Thread(br3.writeMessage); conn.IsBackground = true; PortCommunication.timerSend.Interval = PortCommunication.timeout; PortCommunication.timerSend.Enabled = true; PortCommunication.timerSend.Start(); //timer1.Start(); foreach (Control item in this.Controls) { item.Enabled = false; } conn.Start(); //lg.BringToFront(); //lg.Show(); }
private void ReadClick() { s = ConState.read; dataerrer = true; issessce = false; Common.Common.massage = ""; if (PortCommunication.ComName == null) { mondelName.Text = "Select Port ..."; GetComList(); //获取端口 if (PortCommunication.ComName == null) { MsgBox.Show("Please Select Port ..."); SetEnable(true); return; } } //lg = new Loading(); //lg.Location = new Point(this.Width / 2 - lg.Width / 2, this.Height / 2 - lg.Height / 2); //lg.TopLevel = false; //try //{ // lg.Parent = this; //} //catch (Exception) //{ // ; //} try { PortCommunication.NewSerialPort(); PortCommunication.openPort(); } catch (Exception) { MsgBox.Show("The port failed to open, please check the port!"); SetEnable(true); return; } conn = new Thread(br3.readMessage); conn.IsBackground = true; //timerSend.Interval = timeout; PortCommunication.timerSend.Interval = PortCommunication.timeout; PortCommunication.timerSend.Enabled = true; PortCommunication.timerSend.Start(); //timerSend.Start(); //timer1.Start(); SetEnable(false); conn.Start(); }
public virtual void TCPLoop() { while (!m_bStop) { if (m_enConState < ConState.Connected) { if (m_bPassive) //Is Server { if (m_enConState == ConState.listen) { m_ClientSocket = m_ServerSocket.Accept(); m_enConState = ConState.Connected; } else //Server Socket is not Listening { if (ServerListen()) m_enConState = ConState.listen; } } else //Is Client { if (m_enConState < ConState.Connected) { if (ClientConnect()) m_enConState = ConState.Connected; else m_enConState = ConState.Opening; } } } if (m_enConState == ConState.Connected) { if (!IsConnect(m_iPortNum)) { CloseSocket(); } } Thread.Sleep(10); } }
protected void CloseSocket() { m_ClientSocket.Shutdown(SocketShutdown.Both); // is have problam m_ClientSocket.Close(); Thread.Sleep(100); if (m_bPassive) m_enConState = ConState.listen; else m_enConState = ConState.Opening; }
protected bool ClientConnect() { try { m_enConState = ConState.Opening; m_ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_ClientSocket.Connect(IPAddress.Parse(m_sIPAddress), m_iPortNum); return true; } catch (Exception) { m_ClientSocket = null; Thread.Sleep(500); return false; } }