How do I read and write to a PLC through python code on my laptop via ethernet?

2.7k views Asked by At

I am doing a project of communicating with a PLC through python code on my Laptop using pyModbusTCP. I am getting stuck on how to go about the process as there isn't much info on the net. I have made a simulation of it using pyModbusServer and pyModbusClient (both on the laptop) which works perfectly but now that I am working with a PLC, I am not getting how to go about it. Anyone who could help or has any links/resources, it would be really helpful. TIA.

The server code was:

from pyModbusTCP.server import ModbusServer, DataBank
from time import sleep
from random import uniform
server = ModbusServer("127.0.0.1",12345,no_block=True)

try:
    print("Start Server.....")
    server.start()
    print("Server is online")
    state= [0]
    while True:
        DataBank.set_words(0,[int(uniform(0,100))])
        if state!= DataBank.get_words(1,5):
            state=DataBank.get_words(1,5)
            print("Value of Register 1 has changed to"+str(state))
            sleep(0.5)

except:
    print("Server shutdown...")
    server.stop()
    print("Server is offline")   

The client code was:

from pyModbusTCP.client import ModbusClient
client=ModbusClient(host="127.0.0.0",port=12345)
client.open()
client.read_holding_registers(1)
client.write_multiple_registers(1)
client.close()

This was the simulation of a PLC and PC communicating. I wanted to know how to communicate with an actual PLC, like what kind of code, what libraries or can i use the similar code. I could not find any resources relating to it. Could you guide me pls?

0

There are 0 answers