Hello PLCnext Community,
this contribution is a prototype implementation put together by our USA solution engineer Josh Krug. If you have any question please reach out to us, or ask over here in the forum on the PLCnext Community. Thanks, and stay tuned for more.
Introduction
This article will go over the steps to communicate to a Modbus bus coupler using a Python script.
Objectives
This document covers the following procedures:
- Installing Python modules
- Creating a Python script to allow the controller to act as a Modbus client
- Setting up a Modbus Bus coupler
- Testing the Modbus communications
Requirements
The following hardware and software was used in the development of this procedure:
- AXC F 2152
- IL ETH BK DI8 DO4 2TX-PAC (or any MODBUS tcp device that operates as server)
- PuTTY
Procedure Overview
- Setting up the AXC F 2152
- Setting up IL BK
- Testing Modbus communication
Step 1: Setting up the AXC F 2152
- Prepare your AXC F 2152
- establish an Internet connection
- install a Package Manager
- update Python 3 and PIP
- Install the Modbus package
- SSH into the controller using PuTTY
- Use PIP to install Modbus package:
$ pip install pyModbusTCP
- Create a Modbus client script
- Create new directory:
$ root@axcf2152:/opt/plcnext/# mkdir PythonExample
- Create new file
$ root@axcf2152:/opt/plcnext/PythonExample/# touch modbusClient.py
- Edit this file:
$ nano modbusClient.py
- Copy and paste the following code to your file, change the IP to your BK’s IP, save and exit nano editor:
from pyModbusTCP.client import ModbusClient #Always open port c = ModbusClient(host='192.168.0.25',port = 502, auto_open=True) #Write registers while True: address = int(input(Write to register: ')) value = [int(input('Write value: '))] c.write_multiple_registers(address, value) quit = input('Done writing?(y/n)') if quit.lower() == 'y': break #Read registers while True: address = int(input(Read from register: ')) registers = int(input('Number of registers to be read: ')) try: read_regs = c.read_holding_registers(address,registers) print(read_regs) except: print("registers don't exist") quit = input('Done reading?(y/n)') if quit.lower() == 'y': break
- Create new directory:
- Test the script:
$ root@axcf2152:/opt/plcnext/PythonExample/# python modbusClient.py
If everything has been installed properly you should be prompted to Enter starting registers. To exit the script, press Ctrl + c.
Step 2: Setting up the Bus Coupler
Note: the default password is “private”.
- Change IP address of bus coupler using IPAssign
- Bring up the webserver of your device in a browser
- Disable watchdog timer
- Navigate here:
Inline station > Process Data Monitoring > Process Data Watchdog = 0 ms - Press “Apply”
- Navigate here:
- Disable Plug-and-play mode
- Navigate here:
Inline Station > Services > Disable Plug&Play-Mode = Disable - Press “Apply & Reboot”
- Navigate here:
- Take a note of the Modbus registers:
Step 3: Testing Modbus communication
- Execute the script:
$ root@axcf2152:/opt/plcnext/PythonExample# python modbusClient.py
- Write to an output on the bus coupler using
address 8001 value 1 - Read input from bus coupler:
address 8000 qty 1
- Write to an output on the bus coupler using
If everything was executed correctly this should have worked.
Leave a Reply
You must be logged in to post a comment.