• 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: PoE-M.2-HAT+B User Guide ESP32-S3FH4R2-MINI-Tiny-Stamp-WIFI-Bluetooth5 Raspberry Pi Pico ST7789 SpotPear XIAO RA4M1 Tiny SuperMini Board For Arduino Uno IDE For SeeedStudio R7FA4M1 BPI Pi5 Case STM32 1.54inch OLED Raspberry Pi Pico 2 RP2350 1.28 inch Round LCD Development Board 240x240 Display Screen QMI8658 6-Axis ESP32-S3 LCD Driver Board RGB/SPI N8R8 For 2.1/2.8/4inch Captive TouchScreen Display Round Cortex-A53 Raspberry Pi 5 Raspberry Pi 5 PCIe to USB3.2 Gen1 Hub 5Gbps For Raspberry Pi OS Drive free 360° Milk-V Duo Camera Tutorial Modbus-Protocol-Specification User Guide ESP32 S3 Development Board 1.43 inch AMOLED Display 1.43inch TouchScreen For Arduino LVGL With SD-Port/ QMI8658C 6-Axis Sensor Development Board ESP32 S3 Development Board 1.85 inch Round LCD Display 360×360 QMI8658 Sensor / MIC / SD /MP3 Audio /Battery Port LVGL/HMI For Arduino X1002 Raspberry Pi 5 PCIe to M.2 NVMe SSD Adapter Board HAT Pi5 2280