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; } }
public void Open() { IsLink = false; Close(); IPAddress address = null; ushort port = 0; switch (CommunicationParam.InterlockMode) { case InterlockMode.串口: serialHelper = new SerialHelper(); serialHelper.InitSerial(CommunicationParam); serialHelper.PortDataReceiveEvent += SerialHelper_PortDataReciveEvent; if (serialHelper.IsLink) { Util.Notify(string.Format("串口{0}打开完成", CommunicationParam.ComName)); } break; case InterlockMode.TCP服务器: if (IPAddress.TryParse(communicationParam.TcpIP, out address) && ushort.TryParse(communicationParam.TcpPort, out port)) { tcpServerHelper = new TCPServerHelper(address, port); tcpServerHelper.Start(); tcpServerHelper.RecvData += SocketServerHelper_RecvData; tcpServerHelper.ClientConn += SocketServerHelper_ClientConn; tcpServerHelper.ClientClose += SocketServerHelper_ClientClose; if (tcpServerHelper.IsRun == false) { Util.Notify(Level.Err, "服务器打开失败,请检查设置"); return; } else { Util.Notify(string.Format("服务器{0}端口{1}打开完成", tcpServerHelper.ServerIP, tcpServerHelper.Port)); } } else { Util.Notify(Level.Err, string.Format("IP:{0}或端口:{1}设置错误", tcpServerHelper.ServerIP, tcpServerHelper.Port)); return; } break; case InterlockMode.TCP客户端: if (IPAddress.TryParse(communicationParam.TcpIP, out address) && ushort.TryParse(communicationParam.TcpPort, out port)) { try { tcpClientHelper = new TCPClientHelper(); tcpClientHelper.ConnectedServer += SocketClientHelper_ConnectedServer; //要放在Connect函数之前。 tcpClientHelper.Connect(address.ToString(), port); tcpClientHelper.ReceivedDatagram += SocketClientHelper_ReceivedDatagram; if (tcpClientHelper.IsConnected == false) { Util.Notify(Level.Err, "客户端打开失败,请检查设置"); return; } else { Util.Notify(string.Format("与服务器:IP:{0} 端口:{1}连接成功", tcpServerHelper.ServerIP, tcpServerHelper.Port)); } } catch { Util.Notify(Level.Err, string.Format("与服务器:IP:{0} 端口:{1}连接失败", tcpServerHelper.ServerIP, tcpServerHelper.Port)); return; } } else { Util.Notify(Level.Err, string.Format("IP:{0}或端口:{1}设置错误", tcpServerHelper.ServerIP, tcpServerHelper.Port)); } break; //case InterlockMode.UDP: // Util.Notify(Level.Err, "UDP服务器未完成,请修改设置"); // return; //if (IPAddress.TryParse(communicationParam.TcpIP, out address) && ushort.TryParse(communicationParam.TcpPort, out port)) //{ // udpServerHelper = new SocketServerHelper(address, port, ProtocolType.Udp); // udpServerHelper.Start(); // udpServerHelper.RecvData += SocketServerHelper_RecvData; // udpServerHelper.ClientConn += SocketServerHelper_ClientConn; // udpServerHelper.ClientClose += SocketServerHelper_ClientClose; // if (udpServerHelper.IsRun == false) // { // Util.Notify("服务器打开失败,请检查设置"); // } // else // { // Util.Notify(string.Format("服务器{0}端口{1}打开完成", udpServerHelper.ServerIP, udpServerHelper.Port)); // } //} //break; //case InterlockMode.相机IO: // Util.Notify("相机输出模式开启"); // break; default: break; } IsLink = true; //当发生异常时,异常代码都已经在底层处理过,所以该层没有必要再放在try-catch模块中再处理一次。收到throw会自动跳出本函数,不再执行剩下的。 }