首页 > 解决方案 > How to read modbus slaves (RTU) connected to a Siemens PAC4200 gateway (TCP) with python?

问题描述

Please note I need to read the meters remote via the network via modbus gateway.

Siemens documentation states gateway can be reached via port 17002 (module 1)

I tried a TCP-client to the server on port 502 which works fine with pymodbus.

Slaves addresses are at 10-24.

a piece of sample code would be great.

标签: pythontcpmodbus

解决方案


If you have already connected to the PAC4200's server the gateways to the RS485 bus should be very much the same.

Just change the port, Modbus unit ID and target the registers you want to read.

A bare minimum should look like this:

from pymodbus.client.sync import ModbusTcpClient

host = 'device_IP'     # The same you used already for port 502
port = 17002           # for MOD1 slot, use 17003 for MOD2

client = ModbusTcpClient(host, port)
client.connect()

start_reg=0x1          # write starting target register here
number_of_regs=10      # write number of registers to read here
slave_unit=10          # write slave ID of target device here

rr = client.read_holding_registers(start_reg,number_of_regs,unit=slave_unit)

print rr.registers     # print registers, change to print(rr.registers) for Python 3.x

I'm not able to infer what devices you are connecting to the RS485 bus. If the sample code above is not working you should double-check the device's Modbus map and the connections to the PAC (see section 6.6 Connecting the PAC RS485 expansion module on the manual).


推荐阅读