• 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: UGV Beast PT Kit AI OpenCV Robot Car MediaPipe For Jetson Orin Industrial USB TO 4CH RS232/485/422/TTL Serial UART Converter FT4232HL Accelerometer Raspberry Pi 7 inch CAPLCD 1280x800 Capacitive TouchScreen HDMI/Type C Display Monitor For Computer mini PC ESP32 S3 Sipeed Lichee Tang Nano 9K FPGA MINI Development Board GW1NR-9 GOAI Gowin SP3232EEN Raspberry Pi 5 PCIe to USB3 HUB Double 2.5G ETH RJ45 and M.2 NVMe SSD PI5 Expansion board HAT+ Raspberry Pi 5 DSI display Core3566104000 Jetson-nano-to-boot-the-system User Guide Industrial Isolated Converter Raspberry Pi CM5 IO BASE B Mini Board a Base Development Platform Compute Module 5 IO Board with EEPROM And Encryption chip Raspberry Pi 15.6 inch QLED Quantum Dot Display 1920x1080 Capacitive Touchscreen For Computer PC/Jetson/Game $167.99 Circular LCD Screen ESP32 S3 2.1 inch Round LCD Development Board 480x480 TouchScreen Display LVGL HMI With QMI8658C 6-Axis Sensor ESP32 C3 LCD Raspberry Pi 10.1 inch DSI LCD TouchScreen Display 800x1280 Also For RK3576/RK3506/ESP32-P4 ESP32 ST7789 tutorial Raspberry Pi 3.97 inch e-Paper link (G) RYBW 800x480 For Arduino / Jetson / STM32 /ESP32