简体   繁体   中英

pyModbusTCP can you host multiple servers in the same program with different Slave ID's yet connecting to the same IP address?

So I'm attempting to make some software to replicate Modbus Devices for GUI and systems testing and demos. Issue is I want to run one Device Simulator Python Program on the same machine as it's connecting to so it'll be hosted on Localhost. I want to be able to run a server with different SlaveID's on one Localhost IP - almost simulating a RTU to TCP Gateway. One program, representing multiple different simulated devices on different Slave IDs over TCP. If anyone knows how I can run one server but write to different SlaveIDs on the server or if I can run multiple server objects in one program with different SlaveID's and connect to the general IP?

Not sure how much that makes sense but if anyone has any suggestions please let me know!

Thanks!

I was partway through creating a Custom Server Class for the ModbusServerTCP class when I realised I probably wouldn't be able to host multiple servers through the same program and connect to multiple on the same IP even if I could.

Yes, that actually makes quite a lot of sense.

You can implement a server with multiple IDs, which is actually the subject of one of the examples within pymodbus, you can find it here .

Another (and probably easier) option is to look at the server_async.py example. If you run this example with the argument --slaves 3 your server will respond to IDs 0x01 to 0x03 . By default all slaves will store the same contents but you can change that easily by modifying them within the setup_server function .

And there is a third option that you might want to use. Instead of defining a server with multiple IDs you can create multiple servers with one or more IDs running on different ports . To do that you just have to run multiple instances of the server_async.py example each one from a different command window changing the --port argument. You can run as many servers as you want on the same IP as long as you use different ports. Obviously, if you try to run two servers on the same IP and port you will get an error.

For the first two options, make sure you are running a recent pymodbus version. Older versions were reported to be buggy with regard to multiple slave contexts.

I'm not sure what you mean by almost simulating a RTU to TCP Gateway but be aware that there is a serial forwarder that implements a simple forwarder from an RTU serial client to a Modbus TCP server, that might be useful for your purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM