I am currently working on a project that involves communication between a ROS (Robot Operating System) environment and an Allen-Bradley CompactLogix PLC using Modbus TCP. I have encountered an issue with the Modbus connection, and I would appreciate any guidance or insights on resolving it.

Problem Description:

I am attempting to send data from ROS to the 1769-L16ER-BB1B Allen Bradley CompactLogix PLC. However, when running the provided Python code snippet, I am encountering the following error:

Modbus Error: [Connection] Failed to connect [ModbusTcpClient]

Code Snippet:


def move_robot_thread(self): plc_ip = '' plc_port = 502

# Create a synchronous ModbusTcpClient instance
client = ModbusTcpClient(plc_ip, plc_port)

    for point in self.palletizing_pattern:
        x, y, z = point
        x_register_value = int(x * 100)  # Convert to an appropriate integer value
        y_register_value = int(y * 100)
        z_register_value = int(z * 100)

        # Write the coordinates to Modbus registers
        client.write_registers(0, [x_register_value, y_register_value, z_register_value])

        print(f"Published pose: x={x}, y={y}, z={z}")
        print("Waiting for response...")

        # Introduce a delay (adjust the duration as needed)

    print("All poses published.")

except Exception as e:
    print(f"Error during Modbus communication: {e}")

    # Close the Modbus connection

def move_robot(self): # Create a new thread for move_robot_thread function move_thread = threading.Thread(target=self.move_robot_thread) # Start the new thread move_thread.start()

Include relevant import statements

!/usr/bin/env python3

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QComboBox

from PyQt5.QtCore import Qt

from PyQt5.QtWidgets import QComboBox

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.figure import Figure

import matplotlib.pyplot as plt

from geometry_msgs.msg import PoseStamped

from std_msgs.msg import String

from std_srvs.srv import Trigger

from pymodbus.client import ModbusTcpClient

import rclpy

import sys

import threading

from rclpy.task import Future

import time

Additional Information:

ROS version: humble Python version: 3.10.12 Allen Bradley PLC model: 1769-L16ER-BB1B Network configuration: modbus tcp


What could be causing the "Failed to connect" Modbus error? Are there specific considerations for Modbus communication with the Allen Bradley CompactLogix PLC? How can I troubleshoot and diagnose the Modbus connection issue?

Any help or advice on resolving this Modbus connection problem would be greatly appreciated.

Thank you,



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.