Example #1
0
        private void connectBTN_Click(object sender, EventArgs e)
        {
            string           errorStr = "";
            SerialCommConfig sc       = new SerialCommConfig()
            {
                name         = "sercomm",
                addressMajor = commPortTB.Text, addressMinor = (int)baudNUD.Value, devId = (int)devidNUD.Value,
                dataBits     = MBDataBitsEnum.SER_DATABITS_8, parity = MBParityEnum.SER_PARITY_NONE,
                stopBits     = MBStopBitsEnum.SER_STOPBITS_1, pollDelay = 1, retryCnt = 3, timeout = 100
            };

            if (!serConn.Connect(sc, out errorStr))
            {
                MessageBox.Show("Error: " + errorStr);
                return;
            }
        }
Example #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);
            }
        }