pythonraspberry-piraspbianpymodbustcp

pyModbusTCP Raspberry Pi to PC connection eth0


The story so far. I Set up a Server and Client using the very helpful Johannes YouTube tutorial, initially I did this on the Raspberry Pi without any issues using "localhost".

https://github.com/Johannes4Linux/Simple-ModbusTCP-Server/blob/master/Simple_ModbusServer.py

I then attempted to set the Client up on my Windows PC using the Ethernet port and connect to the Server (Pi).

I used the "inet" ip address from the "ifconfig" command within the Pi not the address from the "ipconfig" within windows (they were different).

client = ModbusClient(host="192.168.0.16", port=502, debug=True)
client.open()

connect error
False

I have attempted to ping this address "cmd" from windows but the connection times out.

ping 168.168.0.16

Pinging 168.168.0.16 with 32 bytes of data:
Request timed out.

It feels like I am missing something really obvious. If anyone is able to help me I would really appreciate it.


Solution

  • Ok so it turns out I am a bit of a wally. And many crucial concepts were missing from my implementation.

    Debugging the problem using ipconfig/all revealed that the ipV4 = 192.168.4.180(duplicate)

    I had set the IP adress on the PC and the Pi, I had forgotten that I had changed the dchpcd.conf file. So the devices were trying to obtain the same address within the network subnet.

    The below implementation worked for me:

    enter image description here

    Setup on Pi

    from pyModbusTCP.server import ModbusServer, DataBank
    
    server = ModbusServer(host="192.168.4.181", port=502, no_block=True)
    

    Setup on PC

    from pyModbusTCP.client import ModbusClient
    
    client = ModbusClient(host="192.168.4.181", port=502)
    client.open()
    

    I found this guide very useful in my networking refresher spell: https://www.ionos.co.uk/digitalguide/server/configuration/provide-raspberry-pi-with-a-static-ip-address/#:~:text=To%20assign%20an%20IP%20address,with%20the%20IPv4%20address%20192.168.

    I am happy to delete this but thought I'd leave it here just in case someone had comments or found it useful. As per usual I answer my own question but sometimes just formalising my own ignorance into black and white is useful.