Ejemplo n.º 1
0
 public void Disconnect()
 {
     if (protocol == null)
     {
         System.Diagnostics.Debug.WriteLine("Disconnect - protocol = null");
         return;
     }
     protocol.closeProtocol();
     protocol = null;
 }
Ejemplo n.º 2
0
        public bool Connect(BaseCommConfig commCfg, out string errorStr)
        {
            errorStr = "";
            SerialCommConfig serCfg = commCfg as SerialCommConfig;

            if (protocol != null)
            {
                if (commPort == serCfg.addressMajor)
                {
                    return(true);
                }
                else
                {
                    errorStr = "Connected to a different device";
                    return(false);
                }
            }
            try {
                protocol = new MbusRtuMasterProtocol();
            } catch (OutOfMemoryException ex) {
                return(false);
            }
            slaveAddr          = serCfg.devId;
            protocol.timeout   = serCfg.timeout;
            protocol.retryCnt  = serCfg.retryCnt;
            protocol.pollDelay = serCfg.pollDelay;
            int res = protocol.openProtocol(
                serCfg.addressMajor, serCfg.addressMinor, (int)serCfg.dataBits,
                (int)serCfg.stopBits, (int)serCfg.parity);

            if ((res == BusProtocolErrors.FTALK_SUCCESS))
            {
                commPort = serCfg.addressMajor;
                errorStr = $"Modbus/TCP Device: {serCfg.addressMajor} Baud: {serCfg.addressMinor} opened successfully";
                return(true);
            }
            else
            {
                errorStr = $"Could not open protocol, error was: {BusProtocolErrors.getBusProtocolErrorText(res)}";
                protocol = null;
                return(false);
            }
        }