• 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: Raspberry Pi 5 USB Audio USB TO 2.5G Ethernet Port Converter RJ45 RTL8156B Driver-Free For Windows/macOS/Linux/Android Industrial Modbus POE ETH RJ45 To 8-Ch Relay B RTU/Modbus TCP-Ethernet For IOT Jetson Nano 1.54inch OLED ESP32 Mini TV Raspberry-Pi-GPIO-Audio-amplification-PWM-Sound-Card-Speaker-Buzzer How to Use Product ROS2 JLINK OB Raspberry Pi Pico 2 RP2350 industrial 6-ch Relay RS485 isolation protection Tang Nano Series FPGA FAQ Industrial Isolated USB TO 4CH RS485 (B) Converter UART CH344L For Wall/Rail-Mount Wireless AW CB375N 2.4G/5GHz Dual Band WiFi5 Wireless module RTL8822CE CG Bluetooth 5.0 Raspberry Pi Pico 2 RP2350 1.28 inch Round LCD Development Board 240x240 Display Screen QMI8658 6-Axis Raspberry Pi 5 PCIe to M.2 5G/4G/3G HAT With RM520N-GL Raspberry Pi 5 PCIe to M.2 NVMe SSD Adapter Board (D) HAT Pi5-2280-2242-2230 Raspberry Pi 5 5V5A Power FLASH File System Operation Raspberry Pi LCD ESP32-S3 1.69inch LCD Display With QST Attitude Gyro Sensor QMI8658C For Arduino Python 3.52 inch e-Paper HAT B E Ink Red/Black/White 360x240 Screen Display For Arduino /Raspberry Pi /Jetson Nano /STM32