Beispiel #1
0
        public bool EstablishConnection(int address, MasterConnectionData connectionData)
        {
            ResetConnection();
            _serialPort          = new SerialPort();
            _serialPort.BaudRate = (int)connectionData.BaudRate;
            _serialPort.DataBits = (int)connectionData.DataBits;
            _serialPort.Parity   = connectionData.Parity;
            _serialPort.StopBits = connectionData.StopBits;
            _serialPort.PortName = connectionData.PortName;
            try
            {
                _serialPort.Open();
                _slave = ModbusSerialSlave.CreateAscii((byte)address, _serialPort);
                _slave.Transport.ReadTimeout             = connectionData.TimeoutInMs;
                _slave.Transport.Retries                 = connectionData.RetransmissionCount;
                _slave.Transport.SlaveBusyUsesRetryCount = true;
                //_slave.ModbusSlaveRequestReceived += _receivedRequestEventHandler;
                _slave.WriteComplete += _prepareAnswerEventHandler;
                _slaveListenerThread  = new Thread(_slave.Listen);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Could not establish connection.", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        public bool EstablishConnection(MasterConnectionData connectionData)
        {
            ResetConnection();
            _serialPort          = new SerialPort();
            _serialPort.BaudRate = (int)connectionData.BaudRate;
            _serialPort.DataBits = (int)connectionData.DataBits;
            _serialPort.Parity   = connectionData.Parity;
            _serialPort.StopBits = connectionData.StopBits;
            _serialPort.PortName = connectionData.PortName;
            try
            {
                _serialPort.Open();
                _master = ModbusSerialMaster.CreateAscii(_serialPort);
                _master.Transport.ReadTimeout             = connectionData.TimeoutInMs;
                _master.Transport.Retries                 = connectionData.RetransmissionCount;
                _master.Transport.SlaveBusyUsesRetryCount = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Could not establish connection.", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return(false);
            }

            return(true);
        }