Exemplo n.º 1
0
        public void Close()
        {
            if (serialHelper != null)
            {
                serialHelper.PortDataReceiveEvent -= SerialHelper_PortDataReciveEvent;
                serialHelper.Close();
                serialHelper = null;
            }

            if (tcpClientHelper != null)
            {
                tcpClientHelper.ConnectedServer  -= SocketClientHelper_ConnectedServer;
                tcpClientHelper.ReceivedDatagram -= SocketClientHelper_ReceivedDatagram;
                tcpClientHelper.Close();
                tcpClientHelper = null;
            }

            if (tcpServerHelper != null)
            {
                tcpServerHelper.RecvData   -= SocketServerHelper_RecvData;
                tcpServerHelper.ClientConn -= SocketServerHelper_ClientConn;
                tcpServerHelper.CloseAllClient();
                if (tcpServerHelper.IsRun)
                {
                    tcpServerHelper.Stop();
                }
                tcpServerHelper = null;
            }

            if (udpClientHelper != null)
            {
                udpClientHelper.ConnectedServer  -= SocketClientHelper_ConnectedServer;
                udpClientHelper.ReceivedDatagram -= SocketClientHelper_ReceivedDatagram;
                udpClientHelper.Close();
                udpClientHelper = null;
            }

            if (udpServerHelper != null)
            {
                udpServerHelper.RecvData   -= SocketServerHelper_RecvData;
                udpServerHelper.ClientConn -= SocketServerHelper_ClientConn;
                udpServerHelper.CloseAllClient();
                udpServerHelper = null;
            }
        }
Exemplo n.º 2
0
        private void button_CloseSerial_Click(object sender, EventArgs e)
        {
            if (!helper.ComPortIsOpen)
            {
                return;
            }
            try//尝试关闭串口
            {
                helper.Close();

                SetAfterClose();                    //成功关闭串口或串口丢失后的设置
            }
            catch                                   //如果在未关闭串口前,串口就已丢失,这时关闭串口会出现异常
            {
                if (helper.ComPort.IsOpen == false) //判断当前串口状态,如果ComPort.IsOpen==false,说明串口已丢失
                {
                    SetComLose();
                }
            }
        }