RoArm-M2-S Python UART Communication

From Waveshare Wiki
Jump to: navigation, search

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.
RoArm-M2-S Python.jpg
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.
RoArm-M2-S Python02.png
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.
RoArm-M2-S Python03.png
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

RoArm-M2-S Tutorial Directory

RoArm-M2-S User Tutorial

RoArm-M2-S ROS2 Basic Tutorial