Modbus为众所皆知的协议,使用在工业界或家用自动化任务。可以跑在串行端口物理层(RS232, or RS485)或以太网络(TCP, 或之上)。
关于modbus在Intel® Edison,您能轻松的使用WiFi或是串行端口(RS232),当他们能直接适用于此平台当中。
Python链接库
在Linux系统中,您能发现许多关于modbus的程式库如下:
• C/C++: e.g. libmodbus
• Node: e.g. libmodbus wrapper "modbus"
• Python: e.g. pymodbus
安装
• libmodbus:编译来源码或是安装相关套件(e.g. fromrepo.opkg.net via "opkg 安装 libmodbus")
• node modbus: "npm安装modbus"
• pymodbus: "pip 安装pymodbus" (pip you can get e.g. from repo.opkg.net)
测试
我们能在客户与伺服端的范例利用pymodbus来做测试。
串行端口
首先,我们需要其他的"pyserial"套件
pip install pyserial
为了测试串行端口,我们使用2个Intel® Edison Arduino breakout面板。TX与RX式交叉关系,每个面板上的IO 0与其他面板的IO 1相互对应。为了测试设定功能,我们透过修改在面板上的 同步服务器与在其他面板上的同步客户端。
服务器修改内容
# 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)
客户端修改内容
[...]
# 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()
[...]
将MRAA串行端口初始化。然后modbus能存取"/dev/ttyMFD1" (可参照e.g. Using Serialx on Edison
若是其设定程序正确,您会看到从客户端发送modbus的讯息。
TCP/IP透过以太网络(e.g. WiFi)
为了透过以太网络来测试modbus,我们以非同步伺服器与非同步客戶端为范例-同样内容只做局部修改:
服务器设定IPv4地址为xxx.xxx.xxx.xxx而连结埠为5020。
[...]
StartTcpServer(context, identity=identity, address=("xxx.xxx.xxx.xxx", 5020))
客户端修改内容:
[...]
#---------------------------------------------------------------------------#
defer = protocol.ClientCreator(reactor, ModbusClientProtocol
).connectTCP("xxx.xxx.xxx.xxx", 5020)
defer.addCallback(beginAsynchronousTest)
reactor.run()
若是设定程序正确,您会看到来自于Modbus讯息出现在伺服端如下所示
# 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 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bus.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
[...]
然后您能针对串行端口或是依据modbus装置的以太网络做说明。
文章来源:英特尔来发人员专区