public void Disconnect() { if (protocol == null) { System.Diagnostics.Debug.WriteLine("Disconnect - protocol = null"); return; } protocol.closeProtocol(); protocol = null; }
public bool Connect(ProtocolTypeEnum protocolType, BaseCommConfig commCfg, out string errorStr) { errorStr = ""; TCPCommConfig ipCfg = commCfg as TCPCommConfig; if (protocol != null) { if (slaveIP == ipCfg.addressMajor) { return(true); } else { errorStr = "Connected to a different device"; return(false); } } try { if (protocolType == ProtocolTypeEnum.TCP) { protocol = new MbusTcpMasterProtocol(); } else { protocol = new MbusUdpMasterProtocol(); } } catch (OutOfMemoryException ex) { return(false); } slaveAddr = ipCfg.devId; protocol.timeout = ipCfg.timeout; protocol.retryCnt = ipCfg.retryCnt; protocol.pollDelay = ipCfg.pollDelay; protocol.setPort((short)ipCfg.addressMinor); int res = protocol.openProtocol(ipCfg.addressMajor); if ((res == BusProtocolErrors.FTALK_SUCCESS)) { slaveIP = ipCfg.addressMajor; errorStr = string.Format("Modbus/TCP Device: {0} Port: {1} opened successfully", ipCfg.addressMajor, ipCfg.addressMinor); return(true); } else { errorStr = ("Could not open protocol, error was: " + BusProtocolErrors.getBusProtocolErrorText(res)); protocol = null; return(false); } }