我使用 modbus-tk 创建了一个 modbus 从站,如下所示。我使用Simply modbus master 软件(http://www.simplymodbus.ca/RTUmaster.htm)作为master。如何设置从站以表示有符号整数而不是无符号整数。例如,当我发送 16 位数据时, (FC19) HEX 应该表示 -999 decimal ,但现在我得到 64537。
尝试使用此处的示例https://github.com/ljean/modbus-tk/blob/master/examples/tcpmaster_example.py。好像只对Master有效。
import sys
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serial
PORT = 0
#PORT = '/dev/ptyp5'
def main():
"""main"""
logger = modbus_tk.utils.create_logger(name="console", record_format="%
(message)s")
#Create the server
server = modbus_rtu.RtuServer(serial.Serial(PORT))
try:
logger.info("running...")
logger.info("enter 'quit' for closing the server")
server.start()
slave_1 = server.add_slave(1)
slave_1.add_block('0', cst.HOLDING_REGISTERS, 0, 100,data_format=">b")
while True:
cmd = sys.stdin.readline()
args = cmd.split(' ')
if cmd.find('quit') == 0:
sys.stdout.write('bye-bye\r\n')
break
finally:
server.stop()
if __name__ == "__main__":
main()
我得到了:
Traceback (most recent call last):
File "C:/Users/Yang/Documents/RBES
work/Projects&study/Sensors/serial_code/modbus-tk-test.py", line 50, in main
slave_1.add_block('block1', cst.HOLDING_REGISTERS, 100,
127,data_format='>b')
TypeError: add_block() got an unexpected keyword argument 'data_format'