# mbw_pm.py - using pymodbus in serial mode to read and write modbus command. # both for Windows and Linux environment. ITvilla 2014 # parameters for reading: mba regadd count port # parameters for writing (1 or 2 consecutive registers): mba regadd hexdata port ''' Test program to communicate with a modbus slave using ModbusRTU protocol over serial or obver TCP ''' import sys import traceback # import pymodbus from pymodbus import * from pymodbus.transaction import * from pymodbus.register_read_message import * cmd=0 port=sys.argv[1] print('port',port) # debug if ":" in port: host=port.split(':')[0] port=int(port.split(':')[1]) from pymodbus.client.sync import ModbusTcpClient as ModbusClient if (port <10000 or port > 10003): # tcp client = ModbusClient(host=host, port=port) else: # xport or barionet client = ModbusClient(host=host, port=port, framer=ModbusRtuFramer) else: from pymodbus.client.sync import ModbusSerialClient as ModbusClient # using serial modbusRTU client = ModbusClient(method='rtu', stopbits=1, bytesize=8, parity='E', baudrate=19200, timeout=1, port=port) for kord in range(10): for i in range(16): client.write_register(address=0, value=i*16*256, unit=1) print('ok')