Python CAN Bus Examples
Complete Python code examples for UIROBOT products — CAN bus control via UIM2513 serial gateway and SocketCAN. Includes Python setup, wiring, and code.
This page provides complete Python example projects for controlling UIROBOT motors and I/O modules. Choose your connection method below:
---
## Quick Selection
| Connection Method | Compatible Products | Docs |
|-----------------|-------------------|------|
| **Serial (UIM2513) → CAN Bus** | UIM342 (multi-axis) | [AN251219] |
| **Serial (UIM2513) → CAN Bus** | UIM1616 I/O module | [AN260128] |
---
## Background
UIROBOT provides Python demonstration programs for controlling UIM342 motors and UIM1616 I/O modules via the **UIM2513 RS232-to-CAN gateway**. These programs use serial communication (pyserial) to send commands to the gateway, which forwards them as CAN messages to the target devices.
The code supports **Windows, Linux, and macOS** systems. Refer to the README.md in each sample code package for further platform-specific information.
---
## Required Hardware
| Item | Qty | Description |
|------|:---:|-------------|
| UIM2513 Gateway | 1 | RS232 to CAN bus gateway |
| UIM342 Series Motor | 1+ | CAN bus smart motor (for AN251219) |
| UIM1616 Module | 1 | CAN bus I/O module (for AN260128) |
| 24V DC Power Supply | 1 | For gateway and motors |
| RS232-USB Cable | 1 | For connecting UIM2513 to PC |
| PC with Windows | 1 | For running Python scripts |
---
## Wiring
```
PC (Windows) UIM2513 UIM342/UIM1616
+---------------+ +-----------+ +----------------+
| USB Port |-----| RS232/USB | | |
| Python Script | | CANH ----+----->| CANH |
| (pyserial) | | CANL ----+----->| CANL |
+---------------+ | V+ ----+----->| V+ (24V DC) |
| V- ----+----->| V- (GND) |
+-----------+ +----------------+
```
---
## Setup Python Environment
### Step 1 — Download Python
1. Visit the official Python website: [python.org/downloads](https://www.python.org/downloads/)
2. Select a version suitable for Windows (Python 3.11 or higher recommended)
3. Download the installer (.exe file) matching your system architecture (32-bit or 64-bit)
### Step 2 — Install Python
1. Double-click the downloaded installer to launch the installation wizard
2. **Important**: Check **"Add Python to PATH"** option
3. Select **"Install Now"** for default installation
4. Verify installation:
```bash
python --version
```
### Step 3 — Install Required Libraries
Open Command Prompt (CMD) or PowerShell and install the required libraries:
```bash
# Install pyserial for serial communication
pip install pyserial>=3.5 pyserial-asyncio>=0.6
# Or install all dependencies from requirements.txt
pip install -r requirements.txt
# Upgrade pip (optional but recommended)
python -m pip install --upgrade pip
```
---
## Sample Code
### Download Links
| Application Note | Description | Download |
|-----------------|-------------|----------|
| AN251219 | UIM342 multi-axis control via UIM2513 serial | [Download](https://www.uirobot.com/uploads/files/Windows_Python_Serial_Messge_UIM2513_AN251219.zip) |
| AN260128 | UIM1616 I/O module control via UIM2513 serial | [Download](https://www.uirobot.com/uploads/files/Windows_Python_Serial_Messge_UIM2513_UIM1616_AN260128.zip) |
### Section 1: UIM342 Multi-Axis Control (AN251219)
This demonstration program controls UIM342 motors via the UIM2513 gateway using serial communication.
#### Features
- Serial communication with UIM2513 gateway
- Multi-axis synchronous motion control
- Real-time status monitoring and feedback
- Sensor-triggered homing functionality
- Complete error handling mechanism
#### Run the Program
```bash
# Navigate to the project directory
cd D:\Python_Serial_Messge_UIM2513_AN251219
# Run the main script
python src\main.py
```
#> **Note**: The sample code is provided in the ZIP files above. The code snippet illustrates the general structure.
### Section 2: UIM1616 I/O Module Control (AN260128)
This demonstration program controls the **UIM1616 CAN bus I/O module** via the UIM2513 gateway.
#### Features
- Serial communication with UIM2513 gateway
- Real-time I/O status monitoring and feedback
#### Run the Program
```bash
# Navigate to the project directory
cd D:\Windows_Python_Serial_Messge_UIM2513_UIM1616_AN260128
# Run the main script
python src\main.py
```
> The program will automatically detect available serial ports. Ensure equipment is powered on and CAN cables are properly connected.
---
## Step-by-Step Setup
### Step 1 — Download Sample Code
1. Visit the UIROBOT website: [uirobot.com](https://uirobot.com)
2. Navigate to **Support → Code Example**
3. Find the **Python** section and download the appropriate sample code
4. Extract the downloaded ZIP file
### Step 2 — Install Python
Follow the setup instructions in the **Setup Python Environment** section above.
### Step 3 — Install Required Libraries
```bash
pip install pyserial>=3.5 pyserial-asyncio>=0.6
```
### Step 4 — Connect Hardware
1. Connect the UIM2513 gateway to your PC via USB
2. Wire CANH/CANL between UIM2513 and the target device (UIM342 or UIM1616)
3. Connect 24V DC power supply
4. **Double-check all wiring before powering on**
### Step 5 — Run the Program
```bash
cd [extracted_folder_path]
python src\main.py
```
### Step 6 — Verify Results
- The program will automatically detect available COM ports
- For UIM342: The motor should respond to commands
- For UIM1616: I/O status should be displayed
- Check for any error messages (e.g., power-up issues, CAN cable connections)
---
## Download All Sample Codes
| Application Note | Description | Download Link |
|-----------------|-------------|---------------|
| AN251219 | UIM342 multi-axis control (UIM2513 serial) | [Download ZIP](https://www.uirobot.com/uploads/files/Windows_Python_Serial_Messge_UIM2513_AN251219.zip) |
| AN260128 | UIM1616 I/O module control (UIM2513 serial) | [Download ZIP](https://www.uirobot.com/uploads/files/Windows_Python_Serial_Messge_UIM2513_UIM1616_AN260128.zip) |
---
## Related Documents
- [UIROBOT Code Examples Overview](/docs/code-examples-overview)
- [Arduino CAN Bus Examples](/docs/arduino-examples)
- [UIM342 CAN Bus Motor Controller](/docs/uim342)
- [UIM1616 CAN Bus I/O Module](/docs/uim1616)
- [UIM2513 CAN Bus Gateway](/docs/uim2513)
- [Quick Start Guide](/docs/quick-start)
---
*Last updated: 2026-06-25 | Based on Application Notes AN251219, AN260128*