C++ (Windows) CAN Bus Examples
Complete C++ Windows code examples for UIROBOT products — CAN bus control via UirSDK3 for UIM342 motors and UIM1616 I/O modules. Supports 32-bit and 64-bit architectures.
# C++ Windows CAN Bus Examples
This page provides **C++ Windows** example projects for controlling UIROBOT products using the **UirSDK3** and **CAN30SDK** libraries. These demonstrations support both serial (UIM2513) and Ethernet (UIM2523) gateway connections.
---
## Quick Selection
| Connection Method | Compatible Products | Docs |
|-----------------|-------------------|------|
| **C++ SDK (32-bit) → CAN Bus** | UIM342 (basic) | [AN250821] |
| **C++ SDK (64-bit) → CAN Bus** | UIM342 (basic) | [AN250822] |
| **C++ SDK (32-bit) → CAN Bus** | UIM342 (BuiltIn2) | [AN250915] |
| **C++ SDK (64-bit) → CAN Bus** | UIM342 (BuiltIn2) | [AN250916] |
| **C++ SDK (32-bit) → CAN Bus** | UIM342 (6 motors) | [AN251105] |
| **Serial (UIM2513)** | UIM342 (UART) | [AN251021] |
| **C++ SDK (32-bit) → CAN Bus** | UIM1616 I/O module | [AN251030] |
| **C++ SDK (64-bit) → CAN Bus** | UIM1616 I/O module | [AN251111] |
---
## Background
The C++ demonstration programs use **UirSDK3** and **CAN30SDK** to control UIROBOT devices via CAN bus networks. These projects demonstrate how to control UIM342 motors and UIM1616 I/O modules through UIM2513 (serial) or UIM2523 (Ethernet) gateways.
### Key Features
- Supports both serial (UIM2513) and Ethernet (UIM2523) communication
- Multi-axis synchronous motion control (UIM342)
- Real-time status monitoring and feedback
- Sensor-triggered homing functionality (UIM342)
- Complete error handling mechanism
---
## Required Hardware
| Item | Qty | Description |
|------|:---:|-------------|
| UIM342 Series Motor | 1+ | CAN bus smart motor |
| UIM1616 Module | 1 | CAN bus I/O module |
| UIM2513 Gateway | 1 | RS232-to-CAN gateway (serial mode) |
| UIM2523 Gateway | 1 | Ethernet-to-CAN gateway (IP: 192.168.1.254) |
| MMC904 | 1 | Mini-PC (IP: 192.168.1.16, optional) |
| 24V DC Power Supply | 1 | For motors, gateways, and I/O modules |
| Windows PC | 1 | For development (Visual Studio) |
---
## Wiring
```
Windows PC (Visual Studio C++)
+-----------------------------------+
| UirSDK3 + CAN30SDK |
| - UIM2513 (Serial COM) |
| - UIM2523 (Ethernet 192.168.1.x) |
+-------++---------------++---------+
|| ||
Serial|| Ethernet
|| ||
+-------v--------+ +----v----------+
| UIM2513 | | UIM2523 |
| RS232-to-CAN | | Ethernet-to-CAN|
| Serial Gateway | | (192.168.1.254)|
+-------+-------+ +-------+--------+
| |
+---------+---------+
|
CAN Bus (twisted pair)
|
+-------v--------+
| UIM342 Motor |
| or UIM1616 I/O |
+-----------------+
```
---
## Software Installation
### Step 1 — Install Visual Studio
1. Visit: [visualstudio.microsoft.com](https://visualstudio.microsoft.com/)
2. Download and install **Visual Studio** (Community edition is free)
3. During installation, select **Desktop development with C++** workload
### Step 2 — Download Sample Code
Download the appropriate ZIP files from the table below and extract them.
---
## Section 1: UIM342 Motor Control (AN250821 / AN250822)
### Open the Project
1. Unzip the downloaded ZIP file
2. Open the Visual Studio solution (`.sln`) file:
- 32-bit: `Win32_Cpp_libUirSdkWin32_UIM342.sln`
- 64-bit: `Win64_Cpp_libUirSdkWin64_UIM342.sln`
### SDK File Structure
Ensure the following directories are correctly placed relative to the project:
```text
[Project Folder]/
├── uirSDKfiles/ # SDK header files
├── UirSdkLib/ # Library files
├── Win32_Cpp_libUirSdkWin32_UIM342.sln
└── ...
```
### Compile
1. Ensure SDK files are located in `../uirSDKfiles/` directory
2. Ensure library files are located in `../UirSdkLib/` directory
3. Open the project in Visual Studio
4. Select the appropriate platform (x86 for 32-bit, x64 for 64-bit)
5. Click **Build → Build Solution**
6. After successful compilation, DLL files are automatically copied to the output directory
### Run
1. Ensure gateway devices are properly connected
2. Check network configuration (IP address / serial port)
3. Run the executable from Visual Studio (F5) or from the output folder
### Select Gateway Type
The program supports multiple gateway types. Select the appropriate one during runtime:
- **UIM2513**: RS232 serial connection
- **UIM2523**: Ethernet connection (IP: 192.168.1.254)
- **MMC904**: Embedded controller connection (IP: 192.168.1.16)
> **Note**: The actual sample code is provided in the ZIP files above. The code snippet illustrates the general structure using UirSDK3.
---
## Section 2: UIM1616 I/O Module Control (AN251030 / AN251111)
### Open the Project
1. Unzip the downloaded ZIP file
2. Open the Visual Studio solution (`.sln`) file:
- 32-bit: `Win32_Cpp_libUirSdkWin32_UIM1616.sln`
- 64-bit: `Win64_Cpp_libUirSdkWin64_UIM1616.sln`
### Compile
1. Select the appropriate platform (x86 for 32-bit, x64 for 64-bit)
2. **Build → Build Solution**
3. DLL files are automatically copied to the output directory
### Run
1. Ensure gateway devices are properly connected
2. Check network configuration (IP address / serial port)
3. Confirm UIM1616 Module ID is set correctly
4. Run the executable
### Select Gateway Type
Same as UIM342 — select UIM2513 (serial) or UIM2523 (Ethernet) during runtime.
---
## Step-by-Step Setup
### Step 1 — Connect Hardware
1. Connect the gateway (UIM2513 or UIM2523) to your PC and CAN bus network
2. Wire CANH/CANL between the gateway and target devices (UIM342 or UIM1616)
3. Connect 24V DC power supply
4. **Double-check all wiring before powering on**
### Step 2 — Install Visual Studio
Install Visual Studio with **Desktop development with C++** workload.
### Step 3 — Download and Open Sample Code
1. Download the appropriate ZIP file
2. Extract and ensure SDK files are in the correct directories
3. Open the `.sln` file in Visual Studio
### Step 4 — Compile
Build the solution. Verify no compilation errors. DLLs are automatically copied to the output directory.
### Step 5 — Run and Select Gateway
Run the program and select the gateway type when prompted. The program will establish communication with the target device via the selected gateway.
---
## Additional Tips
- **Architecture**: Choose the correct version (32-bit or 64-bit) matching your Windows OS
- **SDK Path**: Ensure `uirSDKfiles` and `UirSdkLib` directories are in the correct relative path
- **Gateway Selection**: Use UIM2513 for serial connection, UIM2523 for Ethernet connection
- **Node IDs**: Ensure each device on the CAN bus has a unique Node ID
- **Network**: For Ethernet mode, all devices must be on the same subnet (192.168.1.xxx)
---
## Download All Sample Codes
| Application Note | Description | Download Link |
|-----------------|-------------|---------------|
| AN250821 | UIM342 32-bit basic | [Download ZIP](https://www.uirobot.com/uploads/files/Win32_Cpp_libUirSdkWin32_UIM342_AN250821.zip) |
| AN250822 | UIM342 64-bit basic | [Download ZIP](https://www.uirobot.com/uploads/files/edca53197b92f7ad3258773be7a3215e.zip) |
| AN250915 | UIM342 BuiltIn2 32-bit | [Download ZIP](https://www.uirobot.com/uploads/files/274d6b862bc67d1c4c2bcc9a1c3c4d65.zip) |
| AN250916 | UIM342 BuiltIn2 64-bit | [Download ZIP](https://www.uirobot.com/uploads/files/58038951ba35ed1f5d98efe672dcfd97.zip) |
| AN251105 | UIM342 6-motor 32-bit | [Download ZIP](https://www.uirobot.com/uploads/files/Win32_Cpp_libUirSdkWin32_UIM342_Six_Motors_AN251105.zip) |
| AN251021 | UIM2513 serial (C++) | [Download ZIP](https://www.uirobot.com/uploads/files/Win32_Cpp_Serial_Messge_UIM2513_AN251021.zip) |
| AN251030 | UIM1616 32-bit | [Download ZIP](https://www.uirobot.com/uploads/files/Win32_Cpp_libUirSdkWin32_UIM1616_AN251030.zip) |
| AN251111 | UIM1616 64-bit | [Download ZIP](https://www.uirobot.com/uploads/files/Win64_Cpp_libUirSdkWin64_UIM1616_AN251111.zip) |
---
## Video Tutorial
:::video[UIROBOT C++ and C# Usage Guide for Visual Studio 2022](https://www.youtube.com/watch?v=yBTrlSD8ITI)
:::
---
## Related Documents
- [UIROBOT Code Examples Overview](/docs/code-examples-overview)
- [C# (Windows) CAN Bus Examples](/docs/csharp-windows-examples)
- [UIM342 CAN Bus Motor Controller](/docs/uim342)
- [UIM1616 CAN Bus I/O Module](/docs/uim1616)
- [UIM2513 CAN Bus Gateway](/docs/uim2513)
- [UIM2523 Ethernet to CAN Gateway](/docs/uim2523)
- [Quick Start Guide](/docs/quick-start)
---
*Last updated: 2026-06-25 | Based on Application Notes AN250821-AN251111*