/// <summary> ///校准 Tcp连接断开 /// </summary> void NetCalibrationStop() { if (upnpCalibration != null) { upnpCalibration.Stop(); upnpCalibration = null; } if (oldCalibration != null) { oldCalibration.Close(); } }
/// <summary> /// UNPN初始化 /// </summary> /// <param name="state">true 为路由绑定, false 为 普通模式</param> /// <returns>成功为true</returns> bool NetUPNPInit(LinkNetMode state) { try { if (upnpServer != null) { throw new Exception("upnpServer 不为空,不能建立重复连接,必须首先断开"); } //txtLinkMsg.Text = ""; ip = getIP(); int port = int.Parse(txtPort.Text); if (state == LinkNetMode.UPNPServer) { txtLinkMsg.Text += "UPnp Server is running ... \n"; txtLinkMsg.ScrollToEnd(); upnpServer = new UpnpServer(port, port); //作为服务器端,产生数据 // serialControlCenter.RtuFrameArrived += tcpTransmission_RtuFrameArrived; } else if (state == LinkNetMode.TcpServer) { txtLinkMsg.Text += "TcpServer is running ... \n"; txtLinkMsg.ScrollToEnd(); upnpServer = new UpnpServer(ip, port); //作为服务器端,产生数据 // serialControlCenter.RtuFrameArrived += tcpTransmission_RtuFrameArrived; } else if (state == LinkNetMode.TcpClient) { txtLinkMsg.Text += "TcpClient is running ... \n"; txtLinkMsg.ScrollToEnd(); upnpServer = new UpnpServer(ip, port, false); } upnpServer.NetDataArrayArrived += NetData_Arrived; upnpServer.ExceptionArrived += Exception_TestArrived; upnpServer.LinkingMsg += LinkMsg_TestArrived; return(true); } catch (Exception ex) { MessageBox.Show(ex.Message, "UPNP net初始化"); return(false); } }
/// <summary> /// NetUNP服务终止 /// </summary> void NetUNPStop() { try { if (upnpServer != null) { upnpServer.Stop(); upnpServer.NetDataArrayArrived -= NetData_Arrived; upnpServer.ExceptionArrived -= Exception_TestArrived; upnpServer.LinkingMsg -= LinkMsg_TestArrived; upnpServer = null; } } catch (Exception ex) { MessageBox.Show(ex.Message, "NetUNPStop"); } }
/// <summary> /// TCP 校准初始化 /// </summary> private void upnpCalibrationInit() { try { if (upnpCalibration != null) { throw new Exception("upnpCalibrationInit 不为空,不能建立重复连接,必须首先断开"); } OldCalibrationInit(); upnpCalibration = new UpnpServer(IPAddress.Parse(txtIp.Text), 8000, false); upnpCalibration.NetDataArrayArrived += CalibrationNetData_Arrived; upnpCalibration.ExceptionArrived += CalibrationException_TestArrived; upnpCalibration.LinkingMsg += CalibrationLinkMsg_TestArrived; } catch (Exception ex) { MessageBox.Show(ex.Message, "TCP初始化"); } }