internal void Connect(IModbusRtuSerialPort serialPort) { if (this.Parity == Parity.None && this.StopBits != StopBits.Two) { throw new InvalidOperationException(ErrorMessage.Modbus_NoParityRequiresTwoStopBits); } _frameBuffer = new ModbusFrameBuffer(256); _serialPort?.Close(); _serialPort = serialPort; _serialPort.Open(); }
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(); }
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(); }
/// <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; }