我创建了modbus从服务器将数据写入寄存器。我能够从从端写入浮点值和整数值。
在modbus母版中,我只能访问整数值,但不能读取浮点值。
我通过了这个https://github.com/ljean/modbus-tk/issues/72,但这并没有解决我的问题。
对于读取整数值,我可以使用下面的代码并读取这些值。
master = modbus_tcp.TcpMaster()
master.set_timeout(time_out_period)
result = master.execute(slave = 100, function_code = 3 , starting_address = 0, quantity_of_x = 25) 但是对于浮点值,我使用了上面和下面的代码。
master = modbus_tcp.TcpMaster()
master.set_timeout(time_out_period)
result = master.execute(slave = 100, function_code = 3 , starting_address = 0, quantity_of_x = 25 , data_format='>f') 我在读取浮点数时出错,
解压缩需要一个长度为4的字节对象。
发布于 2017-12-11 19:15:45
X的数量应该是2的倍数。因为浮点数需要两个16位寄存器或单词,所以如果你想要25,它应该是50。
发布于 2020-01-30 03:14:17
您还需要提供正确的数据格式,以反映有多少单个 float值(下面是大端值)试图解压缩;
1浮子
logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 2, data_format='>f'))2个浮标
logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 4, data_format='>ff'))3个浮标
logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 6, data_format='>fff'))发布于 2020-03-03 00:10:37
这很容易用Numpy。例如:
import numpy as np
# Sample registers to read
start_address = 0
items = 10
# Get the reply from slave
reply = master.execute(1, cst.READ_HOLDING_REGISTERS, start_address, items*2)
# Convert the reply to Numpy array of type int16
d16 = np.array(reply, dtype=np.int16)
# Convert to an array of type float32
f32 = d16.view(dtype = np.float32)https://stackoverflow.com/questions/44628859
复制相似问题