Intel(R) Edison 上的 Modbus
对于英特尔(R)爱迪生平台上的Modbus,您可以轻松使用WiFi或串口(RS232),因为它们直接在该平台上可用。
获取全新的英特尔®物联网开发者套件,这是一种完整的硬件和软件解决方案,允许开发者使用英特尔®伽利略和英特尔®爱迪生板创建令人兴奋的新解决方案。访问英特尔®开发者专区,了解物联网。
Modbus是一种广为人知的协议,用于工业或家庭自动化任务。它可以在物理层上运行,例如串口(RS232或RS485)或以太网(TCP或UDP)。
对于英特尔(R)爱迪生平台上的Modbus,您可以轻松使用WiFi或串口(RS232),因为它们直接在该平台上可用。
Python库
在Linux上,您可以找到许多Modbus库,例如
- C/C++: 例如 libmodbus
- Node: 例如 libmodbus 封装器 "modbus"
- Python: 例如 pymodbus
安装
- libmodbus: 可以从源代码编译或安装软件包(例如从 repo.opkg.net 通过 "opkg install libmodbus")
- node modbus: "npm install modbus"
- pymodbus: "pip install pymodbus" (可以通过例如 repo.opkg.net 获取 pip)
测试
为了测试,我们使用 pymodbus,它已经包含了一些客户端和服务器的示例。
serial
首先,我们需要额外安装 "pyserial" 包,通过
pip install pyserial
为了测试串口,我们使用两个英特尔(R)爱迪生Arduino扩展板。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" (例如参见 在爱迪生上使用Serialx)。
如果一切设置正确,您应该看到来自客户端的Modbus消息。
通过以太网(例如WiFi)的TCP/IP
为了测试通过以太网的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 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0 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
[...]
有了这些,您应该能够与您的串口或以太网Modbus设备通信。