• sales

    +86-0755-88291180

Python UART Communication

Python UART Communication

This chapter mainly introduces how to use a Python UART port to establish communication between the robotic arm and devices such as PC/Raspberry Pi/Jetson Orin Nano. Here, we take the UART communication between the PC and the robotic arm as an example:

Install Python on Windows

First, download the latest Python package from Python official website according to your OS. Here, the system downloaded is Windows 3.12.0.

After downloading, double-click python-3.12.0-amd64. Please ensure to check "Add python.exe to PATH", then click "Customize installation" to enter "Optional Features". Note that if you are installing it on a Windows system, be sure to check the "Add Python.exe to PATH" box.

In the Optional Features interface, keep the check mark and click "Next" to enter the "Advanced Options" interface.
In the "Advanced Options" interface, you can click "Browse" to change the installation address to the address you want to install, here is the default installation address, click " Install" to install after setting, and wait for the installation to complete.

Once installed, we can then compile the Python project and control the robotic arm with JSON commands using the Python demos.

Configure Python Virtual Environment

Click here to download the Python demo for RoArm-M2-S, unzip it, input "cmd" on the startup menu, open the Windows command prompt interface, and then input "cd file path" to enter the "RoArm-M2-S_Python" project:

cd C:\Users\liuwei\Desktop\RoArm-M2-S\RoArm-M2-S_python

Create a virtual environment for this project in this project folder by typing the command: python -m venv [the name of the virtual environment, generally named project "env"].

python -m venv roarmpython-env

To activate this virtual environment, you can input: project name-env\Scripts\activate.bat.

roarmpython-env\Scripts\activate.bat

After unpacking, you can see that there is a requirements.txt file in the demo, which is a list of packages needed for the RoArm-M2-S_Python project, you can install all the necessary packages directly into the virtual environment by typing the following command:

python -m pip install -r requirements.txt

RoArm-M2-S Python04.png
Once the installation is complete, you can next communicate serially with the robotic arm by running the Python serial communication demo.

Python UART Communication

In the RoArm-M2-S Python demo, you can see that there are four Python demos. The Python demo for serial communication is serial_simple_ctrl.py as shown below:

import serial
import argparse
import threading

def read_serial():
    while True:
        data = ser.readline().decode('utf-8')
        if data:
            print(f"Received: {data}", end='')

def main():
    global ser
    parser = argparse.ArgumentParser(description='Serial JSON Communication')
    parser.add_argument('port', type=str, help='Serial port name (e.g., COM1 or /dev/ttyUSB0)')

    args = parser.parse_args()

    ser = serial.Serial(args.port, baudrate=115200, dsrdtr=None)
    ser.setRTS(False)
    ser.setDTR(False)

    serial_recv_thread = threading.Thread(target=read_serial)
    serial_recv_thread.daemon = True
    serial_recv_thread.start()

    try:
        while True:
            command = input("")
            ser.write(command.encode() + b'\n')
    except KeyboardInterrupt:
        pass
    finally:
        ser.close()


if __name__ == "__main__":
    main()

Power on the robotic arm, and connect the PC to the robotic arm with a USB cable through a Type-C interface. If you are using another device to connect to the robotic arm, you can also connect it via the RX/TX pins.
After connecting, search for Device Manager in the search bar of "Start" to check the newly inserted port number, here the newly inserted port number is COM20, different computers insert different port numbers, remember the port number of the robotic arm.
Use the following commands to run the UART communication demo, please remember to add the port number that the robotic arm is connected to. Replace COM20 with the new port number of the robotic arm in the PC. If you use devices such as Raspberry Pi and Jetson Orin Nano, you need to change them to the corresponding port name.

python serial_simple_ctrl.py COM20

RoArm-M2-S Python 05.png
After the run is complete, you can see the return message after the robotic arm has been initialized.
In this interface, you can send the JSON command, or obtain the feedback information of the robotic arm. For more JSON commands, you can refer to RoArm-M2-S JSON Command Meaning.

Demo

TAG: Industrial RS232 to RS485 Raspberry Pi 5 Audio ESP32 S3 Development Board 1.85 inch Round LCD Display 360×360 QMI8658 Sensor / MIC / SD /MP3 Audio /Battery Port LVGL/HMI For Arduino DeepSeek XiaoZhi AI Voice Chat Robot BOX ESP32-S3 Development Board 1.28 inch Round LCD 1.28inch TouchScreen N16R8 Raspberry Pi 5 ABS Case X1008 Raspberry Pi 5 PCIe to 2.5" 3.5" SATA HDD/SSD Dual Double-SATA (NOT BOOT From SATA/SSD Modbus POE ETH Relay Waveshare User Guide ADXL357BEZ RS485 to RJ45 Raspberry Pi 7 inch CAPLCD 1280x800 Capacitive TouchScreen HDMI/Type C Display Monitor For Computer mini PC ESP32 P4 Development Board WIFI6 3.4 inch LCD Round Display 3.4inch TouchScreen 800×800 Dual Microphones ESP32-S3 1.69inch Touchscreen LCD Round Display WiFi Bluetooth With QST Attitude Gyro Sensor QMI8658 Omni-Directional Lidar Luckfox Changeable Photo Frames ESP32-C3-Ornament-Trinket-LVGL-Astronaut-Clock-Watch-MINI-TV-1.69inch-Round-LCD-TouchScreen-ST7789-240x280 15.6 inch Dual LCD Computer PC Monitor Display Double Secondary Screen Type C Mini HDMI 1080P For Windows/MacOS User Guide LuckFox Pico MLX90641