Initial commit: Complete USB2CAN cross-platform framework

Features:
- Cross-platform support (Windows/Linux/macOS)
- CAN and CANFD protocol support
- USB communication (WinUSB/libusb)
- Device management and configuration
- Message transmission and reception
- Filter configuration
- CMake build system
- Comprehensive examples and tests
This commit is contained in:
2025-09-11 17:56:26 +08:00
commit 81bee50cd9
37 changed files with 5536 additions and 0 deletions

184
examples/main.cpp Normal file
View File

@@ -0,0 +1,184 @@
/**
* @file main.cpp
* @brief USB2CAN框架示例程序
*/
#include "../include/api/Usb2Can.h"
#include <iostream>
#include <iomanip>
#include <thread>
#include <chrono>
/**
* @brief 打印设备信息
* @param info 设备信息
*/
void printDeviceInfo(const usb2can::device::Usb2CanDeviceInfo& info) {
std::cout << "Device Information:" << std::endl;
std::cout << " Type: " << (info.type == usb2can::device::DeviceType::CAN ? "CAN" : "CANFD") << std::endl;
std::cout << " Vendor ID: 0x" << std::hex << std::setw(4) << std::setfill('0') << info.vendorId << std::endl;
std::cout << " Product ID: 0x" << std::hex << std::setw(4) << std::setfill('0') << info.productId << std::endl;
std::cout << " Manufacturer: " << info.manufacturer << std::endl;
std::cout << " Product Name: " << info.productName << std::endl;
std::cout << " Serial Number: " << info.serialNumber << std::endl;
std::cout << " Hardware Version: " << info.hwVersion << std::endl;
std::cout << " Firmware Version: " << info.fwVersion << std::endl;
std::cout << " Manufacture Date: " << info.manufactureDate << std::endl;
std::cout << " Device Path: " << info.devicePath << std::endl;
std::cout << " Clock Frequency: " << std::dec << info.clockFrequency << " Hz" << std::endl;
std::cout << " Device Index: " << info.deviceIndex << std::endl;
}
/**
* @brief 打印CAN消息
* @param message CAN消息
*/
void printCanMessage(const usb2can::can::CanMessage& message) {
std::cout << "CAN Message:" << std::endl;
std::cout << " ID: 0x" << std::hex << std::setw(8) << std::setfill('0') << message.id << std::endl;
std::cout << " Timestamp: " << std::dec << message.timestamp << " us" << std::endl;
std::cout << " Frame Type: " << (message.frameType == usb2can::can::CanFrameType::STANDARD ? "Standard" : "Extended") << std::endl;
std::cout << " Data Length: " << static_cast<int>(message.dataLength) << std::endl;
std::cout << " Data: ";
for (int i = 0; i < message.dataLength; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(message.data[i]) << " ";
}
std::cout << std::endl;
std::cout << " Extended: " << (message.isExtended ? "Yes" : "No") << std::endl;
std::cout << " Remote: " << (message.isRemote ? "Yes" : "No") << std::endl;
std::cout << " Bus Status: 0x" << std::hex << static_cast<int>(message.busStatus) << std::endl;
std::cout << " Error Status: 0x" << std::hex << static_cast<int>(message.errorStatus) << std::endl;
std::cout << " TX Error Counter: " << std::dec << static_cast<int>(message.txErrorCounter) << std::endl;
std::cout << " RX Error Counter: " << static_cast<int>(message.rxErrorCounter) << std::endl;
}
/**
* @brief 热插拔回调函数
*/
void hotplugCallback() {
std::cout << "Hotplug event detected!" << std::endl;
}
int main() {
// 创建USB2CAN实例
usb2can::Usb2Can usb2can;
// 初始化框架
if (!usb2can.initialize()) {
std::cerr << "Failed to initialize USB2CAN framework" << std::endl;
return 1;
}
// 注册热插拔回调
if (!usb2can.registerHotplugCallback(hotplugCallback)) {
std::cerr << "Failed to register hotplug callback" << std::endl;
}
// 扫描设备
int deviceCount = usb2can.scanDevices();
std::cout << "Found " << deviceCount << " USB2CAN devices" << std::endl;
if (deviceCount == 0) {
std::cout << "No USB2CAN devices found. Please connect a device and try again." << std::endl;
return 0;
}
// 打印所有设备信息
for (int i = 0; i < deviceCount; ++i) {
usb2can::device::Usb2CanDeviceInfo info;
if (usb2can.readDeviceInfo(i, info)) {
std::cout << "\nDevice " << i << ":" << std::endl;
printDeviceInfo(info);
}
}
// 打开第一个设备
if (!usb2can.openDevice(0)) {
std::cerr << "Failed to open device 0" << std::endl;
return 1;
}
std::cout << "\nDevice 0 opened successfully" << std::endl;
// 获取设备类型
int deviceType = usb2can.getDeviceType(0);
std::cout << "Device type: " << (deviceType == 0 ? "CAN" : "CANFD") << std::endl;
// 获取设备主频
uint32_t clockFreq = usb2can.getDeviceClockFrequency(0);
std::cout << "Device clock frequency: " << clockFreq << " Hz" << std::endl;
// 初始化CAN
usb2can::can::CanConfig canConfig;
canConfig.baudrate = 500000; // 500kbps
canConfig.mode = usb2can::can::CanMode::NORMAL;
if (!usb2can.initCan(0, canConfig)) {
std::cerr << "Failed to initialize CAN" << std::endl;
usb2can.closeDevice(0);
return 1;
}
std::cout << "CAN initialized successfully" << std::endl;
// 设置过滤器
if (!usb2can.setFilter(0, 0, 0, 0, 0, true)) {
std::cerr << "Failed to set filter" << std::endl;
}
// 创建并发送CAN消息
usb2can::can::CanMessage txMsg;
txMsg.id = 0x123;
txMsg.frameType = usb2can::can::CanFrameType::STANDARD;
txMsg.isExtended = false;
txMsg.isRemote = false;
txMsg.dataLength = 8;
const char* testData = "USB2CAN";
txMsg.setData(reinterpret_cast<const uint8_t*>(testData), 7);
std::cout << "\nSending CAN message:" << std::endl;
printCanMessage(txMsg);
if (!usb2can.transmit(0, txMsg)) {
std::cerr << "Failed to transmit CAN message" << std::endl;
} else {
std::cout << "CAN message transmitted successfully" << std::endl;
}
// 接收CAN消息
std::cout << "\nWaiting for CAN messages..." << std::endl;
for (int i = 0; i < 5; ++i) {
usb2can::can::CanMessage rxMsg;
if (usb2can.receive(0, rxMsg, 1000)) {
std::cout << "\nReceived CAN message:" << std::endl;
printCanMessage(rxMsg);
} else {
std::cout << ".";
std::cout.flush();
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
std::cout << std::endl;
// 获取设备状态
usb2can::can::CanStatus status;
if (usb2can.getStatus(0, status)) {
std::cout << "\nDevice Status:" << std::endl;
std::cout << " Bus Status: 0x" << std::hex << static_cast<int>(status.busStatus) << std::endl;
std::cout << " Error Status: 0x" << std::hex << static_cast<int>(status.errorStatus) << std::endl;
std::cout << " TX Error Counter: " << std::dec << static_cast<int>(status.txErrorCounter) << std::endl;
std::cout << " RX Error Counter: " << static_cast<int>(status.rxErrorCounter) << std::endl;
std::cout << " Timestamp: " << status.timestamp << " us" << std::endl;
}
// 关闭设备
usb2can.closeDevice(0);
std::cout << "\nDevice closed" << std::endl;
// 注销热插拔回调
usb2can.unregisterHotplugCallback();
return 0;
}