为了账号安全,请及时绑定邮箱和手机立即绑定

pymodbus:从 Modbus 设备发出读取字符串和多种类型的数据

pymodbus:从 Modbus 设备发出读取字符串和多种类型的数据

慕勒3428872 2021-11-09 20:00:12
我正在尝试从Modbus TCP设备读取String(Usecase-1) & multiple type of data in one request(Usecase-2) 数据,但未能正确解码。系统配置:Python 3.6.5Pymodbus:2.1.0平台:Windows 10 64 位Modbus TCP 服务器:import loggingfrom pymodbus.constants import Endianfrom pymodbus.datastore import ModbusSequentialDataBlockfrom pymodbus.datastore import ModbusSlaveContext, ModbusServerContextfrom pymodbus.device import ModbusDeviceIdentificationfrom pymodbus.payload import BinaryPayloadBuilderfrom pymodbus.server.sync import StartTcpServerclass ModbusTCPServer(object):    # initialize your data store:    hrBuilder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)    # Usecase-1    hrBuilder.add_string("abcdefghij")    #Uncomment below three lines for usecase-2    # hrBuilder.add_32bit_float(20.5)     # hrBuilder.add_32bit_int(45)     # hrBuilder.add_bits([1, 0, 0, 0, 0, 0, 0, 0])    hrBlock = ModbusSequentialDataBlock(0, hrBuilder.to_registers() * 100)    store = ModbusSlaveContext(hr=hrBlock, ir=hrBlock, di=hrBlock, co=hrBlock)    slaves = {        1: store,    }    context = ModbusServerContext(slaves=slaves, single=False)    # initialize the server information        identity = ModbusDeviceIdentification()    modbusDeviceAddress = "127.0.0.1"    modbusDevicePort = 501    # run the TCP server    # TCP:    print("Modbus TCP Server started.")    StartTcpServer(context, identity=identity, address=(modbusDeviceAddress, modbusDevicePort))if __name__ == "__main__":    print("Reading application configurations...")    ModbusTCPServer();
查看完整描述

1 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

使用zero_mode=True在ModbusSlaveContext。


如果要将[0..n]客户端中的寄存器映射到[0..n]服务器中。默认情况下,pymodbus 服务器将寄存器读取地址映射[0..n]到[1..n]其内部存储中的寄存器。这是为了遵守 modbus 规范。引用 pymodbus 源代码。


#The slave context can also be initialized in zero_mode which means that a

# request to address(0-7) will map to the address (0-7). The default is

# False which is based on section 4.4 of the specification, so address(0-7)

# will map to (1-8)::

因此,在您的情况下,您可以设置ModbusSequentialDataBlockto的起始地址1或ModbusSlaveContext使用zero_mode=True.


    hrBlock = ModbusSequentialDataBlock(1, hrBuilder.to_registers() * 100)

    # Or

    store = ModbusSlaveContext(hr=hrBlock, ir=hrBlock, di=hrBlock, co=hrBlock, zero_mode=True)



查看完整回答
反对 回复 2021-11-09
  • 1 回答
  • 0 关注
  • 236 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信