Create Modbus server in python

Hello there,

I’m pretty new in balena and in coding in general. My project is to run a Modbus server in Python on a RevPI connect using balena to communicate in Modbus with other modules. I tried this code from “pyModbusTCP - the easy way to a Modbus TCP server with Python - YouTube” and it works but when I replace the IP by the IP of the RevPI it doesn’t work. Moreover in the RevPI there is 2 ethernet port with I think one IP adress for each one but one the balena board I can only one IP. My request is not very clear but if someone can help me on something it will be appricated.

Best regards,

Hugo

1 Like

Hello @Hugo thanks for your message. Your project looks super interesting!

I never used this pyModbusTCP project before, however do you have the devices on the same network? how do you connect the modbus device to the RevPi? over Ethernet?

Do you think you can share your docker-compose or Dockerfile template so i can try to replicate and see where is the issue?

Thanks mpous for your reply. The setup is as follows


At the moment we have a rev pi connect and we had flashed the OS and hello world project.

Two questions

  1. How can we access the port B on rev pi connect
  2. How to modify docker files to write an IP adddress for port B
1 Like

Hello @amr

Could you please confirm if you are using a special network, the network_mode or privileges on the docker-compose or Dockerfile? Would you mind to share it, so i can test on a machine here?

Also confirm how are you configuring the Ethernet ports? Could you please check this Network Setup on balenaOS - Balena Documentation and let us know what do you get on the networking files?

Meanwhile I asked to our networking experts to see how we can help you more!

Hello @mpous thanks for the support.
Please see the screen shot.

The IP address 192.168.1.200 belongs to the port B.

Please also see the docker compose file contents

version: “2”

services:

main:
build: ./
restart: always
ports:
- “12345”
privileged: true
network_mode: host

please see a snippet of my modbus server code

server = ModbusServer(“0.0.0.0”, 12345, no_block=True)
hostname = socket.gethostname()
print(hostname)
server_ip_address = socket.gethostbyname(hostname)
print(server_ip_address)

Now print(server_ip_address) does not print 192.168.1.200 but gives an error
image

Can you please tell how i can made my modbus server to run at IP address of 192.168.1.200
ALso i am not able to see the logs correctly however the client side of the modbus connection is working perfectly. I could read and write without problem. Its only the server side where i can niether see the logs nor the correct IP

1 Like

Hello @amr @Hugo did you solve your issue? i’m going to test this on my intel NUC.

Thanks @mpous for your support we solved the issue last week using a go around. we did not socket library as it might create problem. Apart from socket library rest works well

and how did you know the ip address of the Ethernet port @amr ?

So we modified the resin-sample file in systems-connection balena image. We have assigned it the ipv4 address 192.168.1.200.
We just connected the modbus client
client =ModbusClient(host = “192.168.1.200”, port =12345)
and it worked

1 Like

Thanks for sharing the solution @amr