Exemple #1
0
        /// <summary>
        /// Connect to the specified <paramref name="remoteEndpoint"/>.
        /// </summary>
        /// <param name="remoteEndpoint">The IP address and optional port of the end unit. Examples: "192.168.0.1", "192.168.0.1:502", "::1", "[::1]:502". The default port is 502.</param>
        /// <param name="endianness">Specifies the endianness of the data exchanged with the Modbus server.</param>
        public void Connect(string remoteEndpoint, ModbusEndianness endianness)
        {
            if (!ModbusUtils.TryParseEndpoint(remoteEndpoint.AsSpan(), out var parsedRemoteEndpoint))
            {
                throw new FormatException("An invalid IPEndPoint was specified.");
            }

            this.Connect(parsedRemoteEndpoint, endianness);
        }
Exemple #2
0
        /// <summary>
        /// Connect to the specified <paramref name="port"/>.
        /// </summary>
        /// <param name="port">The COM port to be used, e.g. COM1.</param>
        /// <param name="endianness">Specifies the endianness of the data exchanged with the Modbus server.</param>
        public void Connect(string port, ModbusEndianness endianness)
        {
            IModbusRtuSerialPort serialPort;

            serialPort = new ModbusRtuSerialPort(new SerialPort(port)
            {
                BaudRate     = this.BaudRate,
                Handshake    = this.Handshake,
                Parity       = this.Parity,
                StopBits     = this.StopBits,
                ReadTimeout  = this.ReadTimeout,
                WriteTimeout = this.WriteTimeout
            });

            this.Connect(serialPort, endianness);
        }
        internal void Connect(IModbusRtuSerialPort serialPort, ModbusEndianness endianness)
        {
            if (this.Parity == Parity.None && this.StopBits != StopBits.Two)
            {
                throw new InvalidOperationException(ErrorMessage.Modbus_NoParityRequiresTwoStopBits);
            }

            base.SwapBytes = BitConverter.IsLittleEndian && endianness == ModbusEndianness.BigEndian ||
                             !BitConverter.IsLittleEndian && endianness == ModbusEndianness.LittleEndian;

            _frameBuffer = new ModbusFrameBuffer(256);

            _serialPort?.Close();
            _serialPort = serialPort;
            _serialPort.Open();
        }
Exemple #4
0
        internal void Connect(IModbusRtuSerialPort serialPort, ModbusEndianness endianness)
        {
            /* According to the spec (https://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf),
             * section 2.5.1 RTU Transmission Mode: "... the use of no parity requires 2 stop bits."
             * Remove this check to improve compatibility (#56).
             */

            //if (this.Parity == Parity.None && this.StopBits != StopBits.Two)
            //    throw new InvalidOperationException(ErrorMessage.Modbus_NoParityRequiresTwoStopBits);

            base.SwapBytes = BitConverter.IsLittleEndian && endianness == ModbusEndianness.BigEndian ||
                             !BitConverter.IsLittleEndian && endianness == ModbusEndianness.LittleEndian;

            _frameBuffer = new ModbusFrameBuffer(256);

            _serialPort?.Close();
            _serialPort = serialPort;
            _serialPort.Open();
        }
Exemple #5
0
        /// <summary>
        /// Connect to the specified <paramref name="remoteEndpoint"/>.
        /// </summary>
        /// <param name="remoteEndpoint">The IP address and port of the end unit.</param>
        /// <param name="endianness">Specifies the endianness of the data exchanged with the Modbus server.</param>
        public void Connect(IPEndPoint remoteEndpoint, ModbusEndianness endianness)
        {
            base.SwapBytes = BitConverter.IsLittleEndian && endianness == ModbusEndianness.BigEndian ||
                             !BitConverter.IsLittleEndian && endianness == ModbusEndianness.LittleEndian;

            _frameBuffer = new ModbusFrameBuffer(260);

            _tcpClient?.Close();
            _tcpClient = new TcpClient();

            if (!_tcpClient.ConnectAsync(remoteEndpoint.Address, remoteEndpoint.Port).Wait(this.ConnectTimeout))
            {
                throw new Exception(ErrorMessage.ModbusClient_TcpConnectTimeout);
            }

            _networkStream              = _tcpClient.GetStream();
            _networkStream.ReadTimeout  = this.ReadTimeout;
            _networkStream.WriteTimeout = this.WriteTimeout;
        }
Exemple #6
0
 /// <summary>
 /// Connect to localhost at port 502.
 /// </summary>
 /// <param name="endianness">Specifies the endianness of the data exchanged with the Modbus server.</param>
 public void Connect(ModbusEndianness endianness)
 {
     this.Connect(new IPEndPoint(IPAddress.Loopback, 502), endianness);
 }
Exemple #7
0
 /// <summary>
 /// Connect to the specified <paramref name="remoteIpAddress"/> at port 502.
 /// </summary>
 /// <param name="remoteIpAddress">The IP address of the end unit. Example: IPAddress.Parse("192.168.0.1").</param>
 /// <param name="endianness">Specifies the endianness of the data exchanged with the Modbus server.</param>
 public void Connect(IPAddress remoteIpAddress, ModbusEndianness endianness)
 {
     this.Connect(new IPEndPoint(remoteIpAddress, 502), endianness);
 }