RoArm-M2-S Python Serial Communication Control
Python Serial Communication
This chapter primarily explains how to use Python serial ports to enable communication between devices such as PCs, Raspberry Pi, Jeston Orin Nano, and robotic arms. Here is an example of serial communication between a PC and a robotic arm.
Working with RDK X5
Click to download RoArm-M2-S Python demo in the RDK X5 Ubuntu system. After downloading, enter the directory "/home/sunrise/Downloads" to decompress:
cd /home/sunrise/Downloads unzip RoArm-M2-S_python.zip
To create a virtual environment for this project in this project folder, and enter the command: python -m venv [virtual environment name, usually called project name-env].
cd RoArm-M2-S_python python -m venv roarmpython-env
To activate the virtual environment, in the command prompt interface enter: project name- env\Scripts\activate.bat.
source roarmpython-env/bin/activate
After unzipping, you can see that there is a requirements.txt file in the demo, this file is the list of installation packages required for the RoArm-M2-S_Python project, you can directly install all the necessary packages into the virtual environment by entering the following command:
python -m pip install -r requirements.txt
Once the installation is complete, then you can perform serial communication with the robotic arm by running the Python serial communication demo.
Python Serial Communication
In the RoArm-M2-S Python demos, you can see that there are four Python demos, and the Python demo used for serial communication is serial_simple_ctrl.py. The demos are as follows:
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 use a USB cable to connect the RDK X5 motherboard to the robotic arm via the 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, enter ls /dev/tty* in the terminal to view the new ports that appear after the robot arm is connected, as shown in the following figure, the new port is /dev/ttyUSB0.
Use the following command to run the serial communication program, and be sure to add the port number to which the robotic arm is connected. If you are using a Raspberry Pi, Jeston Orin Nano, etc., change it to the corresponding port name as well.
python serial_simple_ctrl.py /dev/ttyUSB0
After the execution is completed, no information will be returned. You can use a JSON command that switches an LED light or a torque lock on and off to verify that it can communicate with the robotic arm.
//Turn on the LED light, the LED light on the robotic arm will light up {"T":114,"led":255} //Turn off the LED light {"T":114,"led":0} //Turn off the torque lock, at this time, you can turn the robotic arm by hand {"T":210,"cmd":0} //Turn on the torque lock, at this time, do not turn the robotic arm by hand {"T":210,"cmd":1}
In this interface, you can send instructions in JSON format, and you can also get feedback from the robotic arm to communicate with the robotic arm. For the meaning of more JSON format commands, please refer to the tutorial RoArm-M2-S_JSON Command Meaning .
Working with Windows
Install Python on Windows
First, go to Python official website to download the latest version of Python installation package. You can download it according to your operating system, I downloaded it here for Windows, the version is 3.12.0.
After downloading, double-click the installation program python-3.12.0-amd64. Remember to check "Add python.exe to PATH", then click "Customize installation" to enter the "Optional Features" interface.
Note: If you are installing on a Windows system, make sure to check "Add Python.exe to PATH".
In the Optional Features screen, check the box and click "Next" to enter the "Advanced Options" screen.
In the "Advanced Options" interface, the installation address can be changed to the address you want to install after clicking "Browse", this is the default installation address, click "Install" to install after setting, and wait for the installation to be completed.
After installation, we can proceed with Python project compilation and communicate JSON instructions with the robotic arm through Python demos.
Deploy Python virtual environment
Click to download RoArm-M2-S Python demo and unzip it, and then enter cmd in the Start menu bar to open the Windows Command Prompt interface, and enter the cd folder path to enter RoArm-M2-S_ Python project folder.
cd C:\Users\liuwei\Desktop\RoArm-M2-S\RoArm-M2-S_python
To create a virtual environment for this project in this project folder, and enter the command: python -m venv [virtual environment name, usually called project name-env].
python -m venv roarmpython-env
To activate the virtual environment, in the command prompt interface enter: project name- env\Scripts\activate.bat.
roarmpython-env\Scripts\activate.bat
After unzipping, you can see that there is a requirements.txt file in the demo, this file is the list of installation packages required for the RoArm-M2-S_Python project, you can directly install all the necessary packages into the virtual environment by entering the following command:
python -m pip install -r requirements.txt
Once the installation is complete, then you can perform serial communication with the robotic arm by running the Python serial communication demo.
Python Serial Communication
In the RoArm-M2-S Python demos, you can see that there are four Python demos, and the Python demo used for serial communication is serial_simple_ctrl.py. The demos are as follows:
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 use a USB cable to connect the PC to the robotic arm via the 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 the Device Manager in the "Start" search bar to view the newly inserted port number. Here, the newly inserted is COM20, and the port number varies for different computers. Remember the port number the robotic arm is connected to.
Use the following command to run the serial communication program, and be sure to add the port number to which the robotic arm is connected. Replace the COM20 with the serial port number that the robotic arm inserts into the PC. If you are using a Raspberry Pi, Jeston Orin Nano, etc., change it to the corresponding port name as well.
python serial_simple_ctrl.py COM20
After the operation is completed, you can see the return information after the robotic arm is initialized.
In this interface, you can send instructions in JSON format, and you can also get feedback from the robotic arm to communicate with the robotic arm. For the meaning of more JSON format commands, please refer to the tutorial RoArm-M2-S_JSON Command Meaning .
Demo
RoArm-M2-S Tutorial Catalog
RoArm-M2-S Tutorial
- RoArm-M2-S_Web Usage
- RoArm-M2-S_Secondary Development Tool Usage
- RoArm-M2-S_JSON Command Meaning
- RoArm-M2-S_WIFI Configuration
- RoArm-M2-S_Robotic Arm Control
- RoArm-M2-S_EoAT Setting
- RoArm-M2-S_FLASH File System Operation
- RoArm-M2-S_Step Recording and Reproduction
- RoArm-M2-S_ESP-NOW Control
- RoArm-M2-S_Python Serial Communication Control
- RoArm-M2-S_Python HTTP Request Communication