示例#1
0
        private void connectBTN_Click_1(object sender, EventArgs e)
        {
            serConn = new PLCSerialConnector();
            string           errorStr = "";
            SerialCommConfig sc       = new SerialCommConfig()
            {
                name         = "sercomm",
                addressMajor = comPortTB.Text, addressMinor = (int)baudRateNUD.Value, devId = (int)devIDNUD.Value,
                dataBits     = MBDataBitsEnum.SER_DATABITS_8, parity = MBParityEnum.SER_PARITY_NONE,
                stopBits     = MBStopBitsEnum.SER_STOPBITS_1, pollDelay = 3, retryCnt = 3, timeout = 100
            };

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