You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否使用pyModbusTCP搭建运行于Linux的Modbus Slave?

Can pyModbusTCP Act as a Modbus Slave (Server) for PLC Connections?

Absolutely! I feel your pain with the terminology mix-up—switching between traditional Modbus "master/slave" and pyModbusTCP's "client/server" can definitely throw you for a loop at first. But good news: the server component in pyModbusTCP is exactly what you're looking for when you need a Modbus Slave. It listens for incoming connections from Modbus Masters (like your PLC) and handles read/write requests just like a standard Slave device.

Here's a minimal, working example to get you up and running:

from pyModbusTCP.server import ModbusServer
import time

# Initialize the Modbus Slave (server)
# Listen on all network interfaces, standard Modbus TCP port 502
server = ModbusServer(host="0.0.0.0", port=502, no_block=True)

try:
    print("Starting Modbus Slave (server)...")
    server.start()
    print("Server is live! Waiting for PLC/master connections...")

    # Set initial values for holding registers (Modbus addresses start at 1)
    # Register 1 = 100, Register 2 = 200
    server.data_bank.set_holding_registers(1, [100, 200])

    # Keep the script running to maintain the server
    while True:
        # Optional: Update a register value periodically for testing
        current_val = server.data_bank.get_holding_registers(1)[0]
        server.data_bank.set_holding_registers(1, [current_val + 1])
        time.sleep(1)

except KeyboardInterrupt:
    print("\nStopping server...")
    server.stop()

Key Details to Note:

  • Terminology Mapping: pyModbusTCP's server = traditional Modbus Slave; its client = traditional Modbus Master.
  • Data Bank Interaction: The server.data_bank object lets you manage all Modbus data types:
    • Holding registers (function codes 03, 16): set_holding_registers() / get_holding_registers()
    • Input registers (function code 04): set_input_registers() / get_input_registers()
    • Coils (function codes 01, 05, 15): set_coils() / get_coils()
    • Discrete inputs (function code 02): set_discrete_inputs() / get_discrete_inputs()
  • Linux Port Considerations: Port 502 is privileged, so you'll need root access to use it, or:
    • Use a port above 1024 (e.g., 5020)
    • Grant bind privileges to Python with sudo setcap 'cap_net_bind_service=+ep' /usr/bin/python3
  • Firewall Rules: Make sure your Linux firewall allows incoming traffic on your chosen port (e.g., sudo ufw allow 502/tcp for UFW).

Testing the Slave

You can validate your setup with a tool like mbpoll (install via sudo apt install mbpoll on Debian/Ubuntu):

  • Read holding registers 1-5: mbpoll -t 4 -c 5 -r 1 -p 502 localhost
  • Write to holding register 1: mbpoll -t 4 -r 1 -p 502 localhost -w 999

内容的提问来源于stack exchange,提问作者Ryan Griggs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:37:33