0

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?

  • Please share the code that you have tried to communicate with the PLC so we can help you – Carlo Zanocco Sep 26 '20 at 16:07
  • I have added the code – Sajay Varghese Sep 26 '20 at 17:22
  • You will need to use the PLC's IP Address, not local host (the PLC is the Modbus/TCP Server, you are the client), make sure you can access that IP Address (i.e. make sure you can access the server via your subnet/ipaddress/gateway settings), utilize the correct Modbus/TCP port on the PLC (the default is 502, probably not 12345). – franji1 Sep 29 '20 at 20:18
  • @franji1 So to access the address of the variable in the PLC, I'll have to define the variable as a Modbus variable? or is there some function block that I'll have to write in ladder logic so that the python code can communicate? – Sajay Varghese Sep 30 '20 at 12:35
  • No. I am not familiar with the actual library API, but I am familiar with Modbus and Modbus/TCP. Do you understand TCP (Clients and Servers and IP Addresses and Ports)? Let's start there, before we even get to Modbus issues. TCP is like making a phone call. Modbus is like asking your mother for a recipe. You first have to make a phone call to your mother. If you try to call your mother, but get your neighbor, asking your neighbor for your mother's recipe will make no sense. Let's first get the phone call working. Do you understand TCP? – franji1 Sep 30 '20 at 16:58
  • @franji1 What I understand of TCP is that it is a communication protocol that helps in transferring data in small packets over the internet to a particular destination address. Is it right? – Sajay Varghese Oct 01 '20 at 05:38
  • You need to know the PLC's IP address. – franji1 Oct 01 '20 at 13:04
  • @franji1 For the PLC's IP address, it comes written with the PLC or we can extract it using RSLinx using the Ethernet/IP drivers right? – Sajay Varghese Oct 01 '20 at 16:56
  • The PLC is the "server" in the role of Modbus Slave, so someone typically assigns the IP Address (it is usually NOT DHCP assigned). The Modbus/TCP port is defaulted to 502, but that could be different based on your installation. Once you figure that out, then that is what goes into your ModbusClient constructor, e.g. `client = ModbusClient(host="10.0.1.42", port=502)` – franji1 Oct 01 '20 at 17:27
  • @franji1 Thanks a lot, I understood your point. I'm planning to simulate the code on CodeSys before actually buying a PLC. Do you know if I could and how could I implement the simulation on CodeSys? – Sajay Varghese Oct 09 '20 at 16:25
  • Sorry, no. You probably need to go on CodeSys forum for that help. A Modbus "server" usually is a "configuration" in a PLC, not "code". – franji1 Oct 09 '20 at 16:28
  • Ok. Thanks a lot!! – Sajay Varghese Oct 09 '20 at 16:29
  • @franji1 Do you know how I can simulate the communication btw the PLC(Ladder Logic) and PC(Python code) via ModbusTCP such that the same code can be used directly when I buy a PLC. – Sajay Varghese Oct 16 '20 at 12:31
  • Like I can write a Ladder Logic program(on PC) and a Python code(on PC) and communicate btw them. – Sajay Varghese Oct 16 '20 at 13:28
  • Just run the pyModbusServer on one PC and pyModbusClient on another PC. – franji1 Oct 16 '20 at 16:46
  • @franji1 But in this case I won't be able to write a LadderLogic program so as to simulate the PLC working right? – Sajay Varghese Oct 17 '20 at 05:20
  • You need a PLC simulator then. I would recommend the one our company makes. It's free. Most of the big vendors charge $. The Simulator code can then run on one of our PLCs, but not on one of the big vendors. Also, our PLC hardware is less expensive. I would recommend you spend some money and buy the hardware you need, OR switch to automationdirect.com and look at the Do-more Simulator and PLC line. Do-more Designer PLC Programming software is free. The Simulator is free. Or buy the PLC you think you need and test it on that hardware. – franji1 Oct 18 '20 at 16:28
  • @franji1 So i can use the Do-more simulator as the PLC and it will communicate with my python code through modbus? – Sajay Varghese Oct 21 '20 at 07:28
  • Yes. You actually do not have to set up anything in the Simulator since the Modbus/TCP Server is enabled by default (port 502). Your python client code will be reading from MI memory (Modbus Inputs), MIR (Modbus Input Registers), and reading/writing MC memory (Modbus Coil), and MHR (Modbus Holding Registers). Client-side Holding Register 40001 is actually MHR1 in the Do-more Simulator (it strips off the 4xxxx prefix). So reading 5 Modbus Holding Registers 40003 thru 40007 from the Client's perspective would return MHR3 thru MHR7 from the Simulator. The other types behave simularly. – franji1 Oct 21 '20 at 13:13
  • @franji1 Is there some documentation or resource of using the Modbus communication in Do-more? – Sajay Varghese Oct 21 '20 at 16:42
  • Look at the Help System. It just works (server is a passive operation). What do you need to know? There are a bunch of videos out there on how to use Do-more, if that's your question. – franji1 Oct 21 '20 at 20:03
  • Realize Do-more can be both a Modbus Server and a Modbus Client. Do not confuse the two operations. You are only concerned about Do-more as a Modbus Server (your Python code is the Modbus Client). – franji1 Oct 21 '20 at 20:04
  • @franji1 Thanks a lot for all the help. When i run the command "client.open()", it says "true". That means the client is connected to the Do-more Server. But when i start to read coils, its always showing "False". Any idea to resolve this? – Sajay Varghese Oct 22 '20 at 16:45
  • Write some ladder logic to manipulate the MC coils. Have a Normally Open ST4 contact drive MC1 coil, and a Normally Closed ST4 contact drive MC2. Write it to the Simulator and put it in RUN mode. If you need help with this, I recommend going to community.automationdirect.com and asking questions there. – franji1 Oct 22 '20 at 17:06
  • Thanks a lot!! It worked finally... – Sajay Varghese Oct 23 '20 at 18:10

0 Answers0