Skip to content

ModbusRTU Demo

1. Project introduction

This project, via the RealMan Python development package, configures the ModbusRTU mode of the communication port, writes single coil data, reads single coil data, writes a single register, reads holding registers, and closes the communication port in ModbusRTU mode.

2. Code structure

RMDemo_ModbusRTU/

├── README.md        <- Core project document
├── requirements.txt    <- List of project dependencies
├── setup.py        <- Project installation script

├── src/          <- Project source code
│  ├── main.py       <- Main procedure entry
│  └── core/        <- Core function or business logic code
│└── demo_modbus_rtu.py<- Demo that configures the ModbusRTU mode of the communication port, writes single coil data, reads single coil data, writes a single register, reads holding registers, and closes the communication port in ModbusRTU mode.
└── Robotic_Arm/      <- RealMan robotic arm secondary development package

3. Project download

Download RM_API2 locally via the link: development package download. Then, navigate to the RM_API2\Demo\RMDemo_Python directory, where you will find RMDemo_ModbusRTU.

4. Environment configuration

Required environment and dependencies for running in Windows and Linux environments:

ItemLinuxWindows
System architecturex86 architecture-
python3.9 or higher3.9 or higher
Specific dependency--

Linux configuration

  1. Refer to the python official website - linux to download and install python3.9.

  2. After entering the project directory, open the terminal and run the following command to install dependencies:

bash
pip install -r requirements.txt

Windows configuration

  1. Refer to the python official website - Windows to download and install python3.9.

  2. After entering the project directory, open the terminal and run the following command to install dependencies:

bash
pip install -r requirements.txt

5. Notes

This demo uses the RM65-B robotic arm as an example. Please modify the data in the code according to your actual situation.

6. User guide

6.1 Quick run

Follow these steps to quickly run the code:

  1. Configuration of the IP address of the robotic arm: open the demo_modbus_rtu.py file and modify the initialization parameters of the RobotArmController class in the main function to the current IP address of the robotic arm. The default IP address is "192.168.1.18".

    python
    # Create a robot arm controller instance and connect to the robot arm
    robot_controller = RobotArmController("192.168.1.18", 8080, 3)
  2. Running via command line: navigate to the RMDemo_ModbusRTU directory in the terminal and enter the following command to run the Python script:

    bash
    python ./src/main.py
  3. Running result: Upon successful execution, the running state of the robotic arm will be displayed in the terminal.

After running the script, the output result is as follows:

current api version:  0.2.9

Successfully connected to the robot arm: 1

API Version:  0.2.9

Successfully set the Modbus mode

Successfully wrote the single coil

Successfully read the coils, data: [1]

Successfully wrote the single register

Successfully read the holding registers, data: [180]

Successfully closed the Modbus mode

Successfully disconnected from the robot arm

6.2 Code description

The following are the main functions of the demo_modbus_rtu.py file:

  • Connect the robotic arm

    python
    robot_controller = RobotArmController("192.168.1.18", 8080, 3)

    Connect the robotic arm to the specified IP address and port.

  • Get the API version

    python
    print("\nAPI Version: ", rm_api_version(), "\n")

    Get and display the API version.

  • Configure ModbusRTU mode.

    python
    robot_controller.set_modbus_mode()
  • Write single coil data

    python
    robot_controller.write_single_coil(1)
  • Read single coil data

    python
    robot_controller.read_coils()
  • Write a single register

    python
    robot_controller.write_single_register(180)
  • Read holding registers

    python
    robot_controller.read_holding_registers()
  • Close ModbusRTU mode

    python
    robot_controller.close_modbus_mode()
  • Disconnect from the robotic arm

    python
    robot_controller.disconnect()

7. License information

  • This project is subject to the MIT license.

8. Controller and end effector interface diagram

Controller IO Interface Diagram

Controller_IO Interface Diagram 1

The definitions of wires are explained in the following Table.

No.First-generation cable wiring sequenceSecond-generation cable wiring sequenceDefinitionDescriptionWire number (for second-generation cables only)Remarks
1Pink and brownBlack stripe brown/brownVOUTExternal output +NO.112 V/24 V
2Gray and purpleGray/purpleP_IO_GNDExternal output -NO.2
3YellowYellow485ANO.3
4Yellow and greenBlack stripe yellow485BNO.4
5Purple and whiteBlack stripe whiteIO1Igital input/output bidirectional channelNO.5
6Red and whiteWhite stripe redIO2Igital input/output bidirectional channelNO.6
7Green and whiteBlack stripe greenIO3Igital input/output bidirectional channelNO.7
8Yellow and whiteWhite stripe blackIO4Igital input/output bidirectional channelNO.8
9Blue and whiteBlack stripe orangeOUT_P_INDigital power supply for external inputNO.90−24 V
10Light blueBlack stripe blueOUT_P_OUTDigital power supply for external outputNO.100−24 V
11Deep blueBlueOUT_P_GNDExternal digital groundNO.11
12GreenGreenFDCAN_ACAN_HNO.12
13RedRedFDCAN_BCAN_LNO.13
14WhiteWhiteBlankReservedNO.14
15BlackBlackBlankReservedNO.15
16OrangeOrangeBlankReservedNO.16

Note

The voltage of digital I/O is determined based on the reference voltage connected, and the 16-core extension interface cable of robotic arm provides only 12 V and 24 V power supply voltages. If other output voltages are required for the digital I/O, then reference voltages need to be led in from the pins OUT_P_OUT+, OUT_P_IN+, and OUT_P_GND.

End Effector IO Interface Diagram

The end effector IO interface connects external tools through a 6-core aerial plug. The pins and definitions of the aerial plug are as follows.

End Effector_IO Interface Diagram

Pin No.Wiring colorFunction
1YellowRS485_A
2WhiteRS485_B
3RedDigital interface 1 (DI1/DO1)
4BlackDigital interface 2 (DI2/DO2)
5GreenPower GND
6BluePower output: 0 V/5 V/12 V/24 V, controllable by program

Note

The multiplexing functions in the table above are switched by program commands. Pin 3 and pin 4 are digital input channels (DI1 and DI2) by default before delivery, and the power output of pin 6 is 0 V (programmed).