About Modbus and Intel Edison
- Transfer
Modbus is a well-known communication protocol that has found application in both industry and amateur projects. At the physical level, serial interfaces (RS232 or RS485) or Ethernet (TCP or UDP are involved here) can be used to organize communication between devices using this protocol. Today we’ll talk about how to organize the interaction of Intel Edison with other devices using Modbus.

There are many Linux libraries that allow you to use Modbus. Among them are the following:
The installation procedure depends on the selected library. Consider the installation for the three above libraries.
For tests, we use pymodbus , which comes with client and server code examples.
First you need to install an additional package, "pyserial". You can do this with the following command:
In order to test communication over the serial interface, we use two Intel Edison Arduino patch boards. TX and RX which are cross-connected, that is, IO output 0 of the first board is connected to IO output 1 of the second board and vice versa. In order to test this design, we use a slightly modified code of the “ synchronous server ” example on one board, and on the other - the code from the “ synchronous client ” example .
Here are the changes made to the server code.
And here is our client code:
The serial port is initialized through the MRAA. After that, Modbus can work with "/ dev / ttyMFD1 ".
If everything is configured correctly, then it will be possible to see Modbus messages coming from the client on the server.
In order to test the interaction via the modbus protocol via Ethernet, we used the examples “ asynchronous server ” and “ asynchronous client ”, again slightly editing their code.
The server is assigned an IPv4 address xxx.xxx.xxx.xxx and a certain port (5020 in our case).
And here are the similar client settings:
If everything, again, is configured correctly, you can see messages from the client part of the system arriving at the server. For example, they might look like this:
The example given here will allow you to establish interaction via the Modbus protocol between Intel Edison and various devices using serial interfaces or Ethernet as a medium. The solution is very popular and universal, therefore, we are sure that you will find it a useful application.

Libraries
There are many Linux libraries that allow you to use Modbus. Among them are the following:
- C / C ++, e.g. libmodbus.
- Node, for example, "modbus" - a wrapper for libmodbus.
- Python e.g. pymodbus
Installation
The installation procedure depends on the selected library. Consider the installation for the three above libraries.
- Libmodbus You must either compile the library from the source, or install the package, for example, with repo.opkg.net using the opkg install libmodbus command.
- To install node modbus, you need the “npm install modbus” command
- For pymodbus, use the pip install pymodbus command. Pip can be taken, for example, from repo.opkg.net .
Testing
For tests, we use pymodbus , which comes with client and server code examples.
Serial Testing
First you need to install an additional package, "pyserial". You can do this with the following command:
pip install pyserialIn order to test communication over the serial interface, we use two Intel Edison Arduino patch boards. TX and RX which are cross-connected, that is, IO output 0 of the first board is connected to IO output 1 of the second board and vice versa. In order to test this design, we use a slightly modified code of the “ synchronous server ” example on one board, and on the other - the code from the “ synchronous client ” example .
Here are the changes made to the server code.
# запустите нужный сервер
# ----------------
# Tcp:
# StartTcpServer(context, identity=identity, address=("localhost", 5020))
# Udp:
# StartUdpServer(context, identity=identity, address=("localhost", 502))
# Ascii:
# StartSerialServer(context, identity=identity, port='/dev/pts/3', timeout=1)
# RTU:
import mraa, serial as s
mraa.Uart(0)
StartSerialServer(context, identity, port='/dev/ttyMFD1', stopbits=s.STOPBITS_ONE, bytesize=s.EIGHTBITS, baudrate=115200, parity=s.PARITY_NONE)And here is our client code:
[...]
# client = ModbusClient('localhost', retries=3, retry_on_empty=True)
# ---------------------------------------------------------------------------#
# client = ModbusClient('localhost', port=502)
# client = ModbusClient(method='ascii', port='/dev/pts/2', timeout=1)
# client = ModbusClient(method='rtu', port='/dev/pts/2', timeout=1)
import mraa, serial as s
mraa.Uart(0)
client = ModbusClient(method='rtu', port='/dev/ttyMFD1', stopbits=1, bytesize=8, baudrate=115200, parity=s.PARITY_NONE)
client.connect()
[...]The serial port is initialized through the MRAA. After that, Modbus can work with "/ dev / ttyMFD1 ".
If everything is configured correctly, then it will be possible to see Modbus messages coming from the client on the server.
TCP / IP over Ethernet (e.g. WiFi)
In order to test the interaction via the modbus protocol via Ethernet, we used the examples “ asynchronous server ” and “ asynchronous client ”, again slightly editing their code.
The server is assigned an IPv4 address xxx.xxx.xxx.xxx and a certain port (5020 in our case).
[...]
StartTcpServer(context, identity=identity, address=("xxx.xxx.xxx.xxx", 5020))And here are the similar client settings:
[...]
#---------------------------------------------------------------------------#
defer = protocol.ClientCreator(reactor, ModbusClientProtocol
).connectTCP("xxx.xxx.xxx.xxx", 5020)
defer.addCallback(beginAsynchronousTest)
reactor.run()If everything, again, is configured correctly, you can see messages from the client part of the system arriving at the server. For example, they might look like this:
# python AsynchronousServerExample.py
INFO:pymodbus.server.async:Starting Modbus TCP Server on 192.168.178.72:5020
DEBUG:pymodbus.server.async:Client Connected [IPv4Address(TCP, '192.168.178.72', 5020)]
DEBUG:pymodbus.server.async:0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x5 0x0 0x1 0xff 0x0 0x0 0x2 0x0 0x0 0x0 0x6 0x0 0x1 0x0 0x1 0x0 0x1 0x0 0x3 0x0 0x0 0x0 0x8 0x0 0xf 0x0 0x1 0x0 0x8 0x1 0xff 0x0 0x4 0x0 0x0 0x0 0x6 0x0 0x1 0x0 0x1 0x0 0x8 0x0 0x5 0x0 0x0 0x0 0x8 0x0 0xf 0x0 0x1 0x0 0x8 0x1 0x0 0x0 0x6 0x0 0x0 0x0 0x6 0x0 0x2 0x0 0x1 0x0 0x8 0x0 0x7 0x0 0x0 0x0 0x6 0x0 0x6 0x0 0x1 0x0 0xa 0x0 0x8 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x1 0x0 0x1 0x0 0x9 0x0 0x0 0x0 0x17 0x0 0x10 0x0 0x1 0x0 0x8 0x10 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0x0 0x0 0x6 0x0 0x4 0x0 0x1 0x0 0x8 0x0 0xb 0x0 0x0 0x0 0x1b 0x0 0x17 0x0 0x1 0x0 0x8 0x0 0x1 0x0 0x8 0x10 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0xc 0x0 0x0 0x0 0x6 0x0 0x4 0x0 0x1 0x0 0x8
DEBUG:pymodbus.transaction:0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x5 0x0 0x1 0xff 0x0 0x0 0x2 0x0 0x0 0x0 0x6 0x0 0x1 0x0 0x1 0x0 0x1 0x0 0x3 0x0 0x0 0x0 0x8 0x0 0xf 0x0 0x1 0x0 0x8 0x1 0xff 0x0 0x4 0x0 0x0 0x0 0x6 0x0 0x1 0x0 0x1 0x0 0x8 0x0 0x5 0x0 0x0 0x0 0x8 0x0 0xf 0x0 0x1 0x0 0x8 0x1 0x0 0x0 0x6 0x0 0x0 0x0 0x6 0x0 0x2 0x0 0x1 0x0 0x8 0x0 0x7 0x0 0x0 0x0 0x6 0x0 0x6 0x0 0x1 0x0 0xa 0x0 0x8 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x1 0x0 0x1 0x0 0x9 0x0 0x0 0x0 0x17 0x0 0x10 0x0 0x1 0x0 0x8 0x10 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0xa 0x0 0x0 0x0 0x6 0x0 0x4 0x0 0x1 0x0 0x8 0x0 0xb 0x0 0x0 0x0 0x1b 0x0 0x17 0x0 0x1 0x0 0x8 0x0 0x1 0x0 0x8 0x10 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0x14 0x0 0xc 0x0 0x0 0x0 0x6 0x0 0x4 0x0 0x1 0x0 0x8
DEBUG:pymodbus.factory:Factory Request[5]
DEBUG:pymodbus.datastore.context:validate[5] 2:1
DEBUG:pymodbus.datastore.context:setValues[5] 2:1
DEBUG:pymodbus.datastore.context:getValues[5] 2:1
DEBUG:pymodbus.server.async:send: 00010000000600050001ff00
DEBUG:pymodbus.factory:Factory Request[1]
DEBUG:pymodbus.datastore.context:validate[1] 2:1
DEBUG:pymodbus.datastore.context:getValues[1] 2:1
DEBUG:pymodbus.server.async:send: 00020000000400010101Summary
The example given here will allow you to establish interaction via the Modbus protocol between Intel Edison and various devices using serial interfaces or Ethernet as a medium. The solution is very popular and universal, therefore, we are sure that you will find it a useful application.