3
votes

I created a modbus slave using modbus-tk as below. I use Simply modbus master software(http://www.simplymodbus.ca/RTUmaster.htm) as the master. How to set the slave to represent a signed integer instead of an unsigned. For example, when I send a 16 bits data, (FC19) HEX should represent -999 decimal , but now I get 64537.

Try to use the example here https://github.com/ljean/modbus-tk/blob/master/examples/tcpmaster_example.py. It seems to only work for 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()

And I got:

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'
1

1 Answers

1
votes

You can configure the slave to return signed rather than unsigned when adding the new slave to the server

slave_1 = server.add_slave(1, unsigned=False)

The add_block doesn't allow data_format argument. The data_format can only be used when executing a master query. So you should remove it and add a new block as following

slave_1.add_block('0', cst.HOLDING_REGISTERS, 0, 100)