# Create Modbus server in python

**URL:** <https://forums.balena.io/t/create-modbus-server-in-python/367688>\
**Category:** Project help\
**Created:** [April 11, 2023, 2:42pm UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688 "2023-04-11T14:42:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://forums.balena.io/u/Hugo)\
**Post date:** [April 11, 2023, 2:42pm UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/1 "2023-04-11T14:42:30Z")

</div>

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](https://www.youtube.com/watch?v=FYPQgnQE9fk)” 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

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [April 12, 2023, 11:43am UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/3 "2023-04-12T11:43:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![amr](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amr](https://forums.balena.io/u/amr)\
**Post date:** [April 13, 2023, 10:59am UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/6 "2023-04-13T10:59:09Z")

</div>

Thanks mpous for your reply. The setup is as follows

 ![image](https://global.discourse-cdn.com/balena/original/2X/e/e10842b12f7ce5655dd47549698a905063c96b5a.png)  
At the moment we have a rev pi connect and we had flashed the OS and hello world project.

> **[Get Started with balenaCloud using Revolution Pi Connect and Python - Balena...](https://docs.balena.io/learn/getting-started/revpi-connect/python/)**

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

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [April 14, 2023, 9:40am UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/10 "2023-04-14T09:40:25Z")

</div>

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](https://docs.balena.io/reference/OS/network/#ethernet-setup) 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!

---

<div class="post-metadata">

**Author:** ![amr](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amr](https://forums.balena.io/u/amr)\
**Post date:** [April 19, 2023, 12:20pm UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/12 "2023-04-19T12:20:24Z")

</div>

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

 ![image](https://global.discourse-cdn.com/balena/original/2X/b/bef704d098f41aba28682d0731b2b40b347c63bb.png)

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](https://global.discourse-cdn.com/balena/original/2X/8/8769195c7aca2b64f2adac046b93b28e35e243a3.png)

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

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [May 3, 2023, 10:34am UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/14 "2023-05-03T10:34:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amr](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amr](https://forums.balena.io/u/amr)\
**Post date:** [May 4, 2023, 12:26pm UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/15 "2023-05-04T12:26:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [May 4, 2023, 12:34pm UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/16 "2023-05-04T12:34:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amr](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@amr](https://forums.balena.io/u/amr)\
**Post date:** [May 4, 2023, 12:43pm UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/17 "2023-05-04T12:43:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [May 4, 2023, 1:29pm UTC](https://forums.balena.io/t/create-modbus-server-in-python/367688/18 "2023-05-04T13:29:40Z")

</div>

Thanks for sharing the solution @amr
