public void Disconnect() { if (iComm != null) { if (iComm.isConnected && DisconnectMessage != null && !(CommPort is UdpPort)) { iComm.isConnected = false; IHostMessage disconnectMessage = DisconnectMessage; Send(disconnectMessage, 500); Thread.Sleep(50); } iComm.isConnected = false; } if (iComm != null) { iComm.Close(); iComm.OnMsgReceived -= iConn_OnMsgReceived; iComm.OnBuffReceived -= iConn_OnBuffReceived; } if (iComm != null && iComm.threadProcess != null && iComm.threadProcess.ThreadState != ThreadState.Stopped) { try { iComm.threadProcess.Abort(); } catch { } } iComm = null; }
protected AbstractReader(Socket socket) { IPEndPoint iPEndPoint = (IPEndPoint)socket.RemoteEndPoint; ReaderName = iPEndPoint.Address.ToString() + ":" + iPEndPoint.Port.ToString(); CommPort = new TcpClientPort(iPEndPoint.ToString()); //Type type = Type.GetType("NetAPI.Communication.TcpClient", throwOnError: true); //iComm = (ICommunication)Activator.CreateInstance(type, socket); iComm = new Communication.TcpClient(); iComm.readerName = ReaderName; }
protected bool Connect(out ErrInfo err) { err = new ErrInfo("FF01", ""); if (iComm == null) { try { string text = CommPort.Port.ToString(); if (text == "RS232" || text == "RS485") { //text = "COM"; iComm = new COM(); } else { iComm = new NetAPI.Communication.TcpClient(); } //Type type = Type.GetType("NetAPI.Communication." + text, throwOnError: true); //iComm = (ICommunication)Activator.CreateInstance(type); iComm.readerName = ReaderName; } catch (Exception ex) { err = new ErrInfo("FF01", ex.Message); Log.Fatal(CommPort.Port.ToString() + ":" + ex.Message); return(false); } } if (iComm != null) { //Assembly assembly = Assembly.LoadFrom(APIPath.folderName + "\\API.dll"); //Type type2 = assembly.GetType("NetAPI.Protocol.VRP.Decode", throwOnError: true); //iComm.iProcess = (IProcess)Activator.CreateInstance(type2); iComm.iProcess = new Decode(); try { if (iComm.Open(CommPort.ConnStr)) { iComm.OnMsgReceived += iConn_OnMsgReceived; iComm.OnBuffReceived += iConn_OnBuffReceived; iComm.threadProcess = new Thread(iComm.process); iComm.threadProcess.Start(); if (ConnectMessage != null && !(CommPort is UdpPort)) { bool flag = false; for (int i = 0; i < 2; i++) { IHostMessage connectMessage = ConnectMessage; bool flag2 = Send(connectMessage); if (connectMessage.Status != MsgStatus.Timeout) { flag = true; break; } } if (!flag) { err = new ErrInfo("FF01", "协议消息测试失败"); Disconnect(); return(false); } } return(true); } } catch (Exception ex2) { err = new ErrInfo("FF01", ex2.Message); Log.Error("iComm:" + ex2.Message); return(false); } } return(false); }