Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Modbus on Intel(R) Edison

16 Dec 2015 1  
For modbus on Intel(R) Edison you can easily use WiFi, or serial (RS232) as they are directly available on the platform.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

Modbus is a well known protocol used for instance fo industrial, or home automation tasks. It may run on physical layers such as serial (RS232, or RS485) or ethernet (TCP, or UDP).

For modbus on Intel(R) Edison you can easily use WiFi, or serial (RS232) as they are directly available on the platform.

Python libraries

On Linux you find a whole bunch of modbus libraries such as

  • C/C++: e.g. libmodbus
  • Node: e.g. libmodbus wrapper "modbus"
  • Python: e.g. pymodbus

Installation

  • libmodbus: either compile from source or install package (e.g. from repo.opkg.net via "opkg install libmodbus")
  • node modbus: "npm install modbus"
  • pymodbus: "pip install pymodbus" (pip you can get e.g. from repo.opkg.net)

Test

For a test we use pymodbus which already comes with some samples for client and server.

serial

At first we need in addition the "pyserial" package via

pip install pyserial

In order to test serial we use two Intel(R) Edison Arduino breakout boards. TX and RX are crossed, i.e. IO 0 from each board goes to IO 1 of the other board. To test the setup we use the slightly modified synchronous server on one board and the synchronous client on the other board.

The modifications for the server

# run the server you want
#----------------
# 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 for the client

[...]
#    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()
[...]

Via MRAA the serial port is initialized. Afterwards modbus can access "/dev/ttyMFD1" (see e.g. Using Serialx on Edison).

If everything is set up correctly you should see modbus messages coming in from the client.

TCP/IP over Ethernet (e.g. WiFi)

In order to test modbus over ethernet we use the asynchronous server and asynchronous client examples - again with only minor modifications:

The server is set to the own IPv4 address xxx.xxx.xxx.xxx and a port (here 5020).

[...]
StartTcpServer(context, identity=identity, address=("xxx.xxx.xxx.xxx", 5020))

and the client likewise:

[...]
#---------------------------------------------------------------------------# 
defer = protocol.ClientCreator(reactor, ModbusClientProtocol
        ).connectTCP("xxx.xxx.xxx.xxx", 5020)
defer.addCallback(beginAsynchronousTest)
reactor.run()

Again with everything set up correctly you should see modbus messages from the client popping in at the server side like below

# 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 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0 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: 00020000000400010101

[...]

With that you should be able to talk to your serial, or ethernet based modbus devices.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here