1

我正在探索使用 pyModbusTCP 模块和 python 与设备通信的选项。供应商已向我发送了通信协议布局,但我不明白如何将手册中的信息与 pyModbusTCP 想要的输入联系起来。

我还没有该设备,而且我对 Modbus 也很陌生,主要阅读了 Wikipedia 和其他一些文章。

这是手册的摘录(抱歉,我不知道如何插入表格):

+------------------------+---------------+--------------------------+
|    Modbus address      |      Bit      |    Description           |
+------------------------+---------------+--------------------------+
|    2                   |      0        |    Binary, some function |
|                        |      1        |    Binary, some function |
|                        |      2        |    Binary, some function |
|                        |      3        |    Binary, some function |
|                        |      4        |    Binary, some function |
|                        |      5        |    Binary, some function |
|                        |      6        |    Binary, some function |
|                        |      7        |    Binary, some function |
+------------------------+---------------+--------------------------+
|    104                 |      0        |    Binary, some function |
|                        |      1        |    Binary, some function |
|                        |      2        |    Binary, some function |
|                        |      3        |    Binary, some function |
|                        |      4        |    Binary, some function |
|                        |      5        |    Binary, some function |
|                        |      6        |    Binary, some function |
|                        |      7        |    Binary, some function |
+------------------------+---------------+--------------------------+
|    112                 |               |    Current level, 0-200  |
+------------------------+---------------+--------------------------+

手册还说明了支持 MODBUS 功能 0x03、0x01、0x06、0x10 和 0x05。

我的计划是使用 pyModbusTCP 中的这个函数来读取线圈: read_coils(bit_addr, bit_nb=1)write_single_coil(bit_addr, bit_value)

Modbus function READ_COILS (0x01)
Parameters: 
bit_addr (int) – bit address (0 to 65535)
bit_nb (int) – number of bits to read (1 to 2000)
Returns:    
bits list or None if error

Return type:    
list of bool or None

我给你的问题——善良的读者——如下:

  1. 例如,如何实现读取“Modbus 地址”=104“位”=3?我错过了一些基本的东西吗?我想避免编写自定义通信程序,但尽可能依赖 pyModbusTCP。pyModbusTCP 的读取位示例在这里:https ://pymodbustcp.readthedocs.io/en/latest/examples/read_bit.html

  2. 例如,如何实现编写,即“Modbus 地址”=2“位”=6?以下是否正确:

from pyModbusTCP.client import ModbusClient
import time
SERVER_HOST = "localhost"
SERVER_PORT = 502
c = ModbusClient()
c.host(SERVER_HOST)
c.port(SERVER_PORT)

addr = 2
while True:
    # if open() is ok, write coils (modbus function 0x01)
    if c.is_open():
        is_ok = c.write_single_coil(addr, 0) # bit 0
        time.sleep(0.5)
        is_ok = c.write_single_coil(addr, 0) # bit 1
        time.sleep(0.5)
        is_ok = c.write_single_coil(addr, 0) # bit 2
        time.sleep(0.5)
        is_ok = c.write_single_coil(addr, 0) # bit 3
        time.sleep(0.5)
        is_ok = c.write_single_coil(addr, 0) # bit 4
        time.sleep(0.5)
        is_ok = c.write_single_coil(addr, 0) # bit 5
        time.sleep(0.5)
        is_ok = c.write_single_coil(addr, 1) # bit 6 <<--
        time.sleep(0.5)
        is_ok = c.write_single_coil(addr, 0) # bit 7
        time.sleep(0.5)
        time.sleep(1)

非常感谢您的任何见解和帮助。

4

0 回答 0