1
votes

Is it possible to build a modbus-RTU ethernet gateway using a linux box or arduino?

I have plc slaves linked together using modbus RTU, and I want to connect one of them over TCP with my pc as master.

I wonder if I can use a linux box (rasbery pi/raspbian) connected to a router, as a modbus-to-tcp converter by piping the usb port to my local ip on some port, as one plc will be connected in modbus rtu to the linux box`s usb port. Piping command will be something Like this:

nc -l 5626 > /dev/ttyUSB0 < /dev/ttyUSB0

My goal is to connect a pc (networked with the linux box) to that plc through the linux box using modbus.

3

3 Answers

1
votes

Yes you can use a linux box with ethernet port.

Its not a simple 'nc' is it.

First things first, you need to know, Does your PLC's support Modbus TCP or Modbus RTU over TCP.

bcos both the formats are not the same, they are not interchangeable.

once you ascertain this, you need to write a TCP Client to Connect to the Slaves as they run the Servers.

If they don't support Modbus TCP yet, you need to write a TCP servers as well.

on top of that, you have the handshaking delays and half open connections and what not.

If it is a commercial device, you need to look at the setup it will be running as well.

Hope this helps

1
votes

I'm not entirely sure as to what your requirements are, but you may want to have a look at the following library. It's a Modbus TCP->RTU library I assume you can use.

https://github.com/3cky/mbusd

Best Regards

0
votes

While it's certainly possible to build a Modbus TCP/RTU gateway, you won't be able to do it with a simple nc command, for two reasons:

  1. The actual Modbus protocol data unit (PDU), merely containing the Modbus function code and data, is embedded in an application data unit (ADU) so the request reaches the correct device free of communication errors. This ADU is different for RTU and TCP. Generically, the ADU looks like this:

    ------------------------------------------
    | Additional address | PDU | Error check |
    ------------------------------------------
    

    For RTU, the "Additional address" is a single byte identifying the unit/slave on the serial line, and the "Error check" is a 16-bit CRC.

    For TCP, the "Additional address" is the 7-byte Modbus application protocol header (MBAP), consisting of transaction identifier, protocol identifier, and a length field (2 bytes each), plus a single byte identifying the unit/slave (usually 255, though for a gateway, this would be the ID of the RTU slave behind it). The "Error check" is empty (zero bytes) as that is already taken care of by TCP.

  2. The communication semantics are different for RTU and TCP.

    For RTU, you write a request to the serial line, wait for the reply, and only then write the next request. There must be 3½ characters of silence between frames.

    For TCP, you can in principle have multiple connections being served concurrently. If you tried to forward two TCP requests to the serial line simultaneously, chaos would ensue.

Still, you can build a working gateway with a slightly more complicated program than nc:

  • Accept connections on TCP port 5626 (note that the actually recommended port number for Modbus TCP is 502), convert the received TCP ADUs to RTU ADUs and put them into a queue together with a back channel.
  • Another part of your program takes one item at a time from that queue, sends the ADU over the serial line and reports the result back through the back channel. Then it goes on the to the next item, and so on.
  • The results arriving on the back channels are converted to TCP ADUs and sent back on the respective TCP connection.

This would certainly work on a Raspberry Pi, and possibly also on Arduino, depending on how large you want your queue to be.


Sources: