Makers Blog Archive

Modbus TCP with Python on AXC F 2152

Yuri Chamarelli 09 May 2019 min. read
1,038 views 0 comments

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.


This article will go over the steps to communicate to a Modbus bus coupler using a Python script.


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


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

  1. Setting up the AXC F 2152
  2. Setting up IL BK
  3. Testing Modbus communication

Step 1: Setting up the AXC F 2152

  1. Prepare your AXC F 2152
    1. establish an Internet connection
    2. install a Package Manager
    3. update Python 3 and PIP
  2. Install the Modbus package
    1. SSH into the controller using PuTTY
    2. Use PIP to install Modbus package:
      $ pip install pyModbusTCP
  3. Create a Modbus client script
    1. Create new directory:
      $ root@axcf2152:/opt/plcnext/# mkdir PythonExample
    2. Create new file
      $ root@axcf2152:/opt/plcnext/PythonExample/# touch
    3. Edit this file:
      $ nano
    4. 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='',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
  4. Test the script:
    $ root@axcf2152:/opt/plcnext/PythonExample/# python

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”.

  1. Change IP address of bus coupler using IPAssign
  2. Bring up the webserver of your device in a browser
  3. Disable watchdog timer
    1. Navigate here:
      Inline station > Process Data Monitoring > Process Data Watchdog = 0 ms
    2. Press “Apply”
  4. Disable Plug-and-play mode
    1. Navigate here:
      Inline Station > Services > Disable Plug&Play-Mode = Disable
    2. Press “Apply & Reboot”
  5. Take a note of the Modbus registers:
    Modbus registers image

Step 3: Testing Modbus communication

  1. Execute the script:
    $ root@axcf2152:/opt/plcnext/PythonExample# python
    1. Write to an output on the bus coupler using
      address 8001 value 1
    2. Read input from bus coupler:
      address 8000 qty 1

If everything was executed correctly this should have worked.


The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.


Please login/register to comment


Leave a Reply

Never miss a new article
Sign up for the newsletter
Never miss news about PLCnext Technology
Get interesting content via newsletter four times a year
Receive exclusive information before all other users