# usb-fdcan **Repository Path**: jingyuancode/usb-fdcan ## Basic Information - **Project Name**: usb-fdcan - **Description**: usb-fdcan是配合usb-can-card-v3固件的usb驱动程序,相比于usb-can-v2提供了更可靠的传输,更丰富的can协议支持 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-11-28 - **Last Updated**: 2026-01-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # USB-FDCAN ## 简介 usb-fdcan是配合usb-can-card-v3固件的usb驱动程序,相比于usb-can-v2提供了更可靠的传输,更丰富的can协议支持 ## 发布说明 - v1.0:首次发布 ## 使用说明 1. 编译 ``` bash mkdir build cd build cmake .. make ``` 2. 对象实例化 - 对象默认构造函数会根据VID和PID识别第一个符合的设备。当插入多张can卡,或者有其他相同VID和PID的设备时不适用。 - 构造函数可以根据iProduct字符串识别can卡。 ``` c++ /** * @brief Construct a new usb fdcan object,opens the first found usb-fdcan device */ usb_fdcan usb_can; /** * @brief Construct a new usb fdcan object with device name * @param name Device iProduct string */ usb_fdcan usb_can("usb-fdcan"); ``` - 可以通过以下命令在linux下查看can卡的iProduct ``` bash lsusb -v -d 0483:5740 ``` 3. 设置iProduct字符串 - 通过set_name函数设置can卡iProduct,字符串长度应小于32(包含'\0') - 该字符串将在can卡下一次上电时生效 ``` c++ /** * @brief Set the USB FDCAN device name, the name will available after reconnecting the device * @param name Device iProduct string * @return USB_FDCAN_STATUS */ USB_FDCAN_STATUS set_name(std::string_view name); ``` 4. 配置终端电阻 - 终端电阻通过软件控制,默认不接入 ``` c++ /** * @brief Enable or disable register access * @param enable true to enable, false to disable * @return USB_FDCAN_STATUS */ USB_FDCAN_STATUS register_access(bool enable); ``` 5. 发送报文 - 发送仅提供一条API,通过默认参数自动适应不同类型的保文 - length为报文真实长度,函数内部会自动适配协议。 ``` c++ /** * @brief Transmit a CAN/FDCAN frame * @param id Identifier * @param data Pointer to data bytes * @param length Length of data bytes * @param id_type Identifier type, standard or extended * @param frame_type Frame type, classic, remote or FD * @param brs Bit rate switch, only valid for FD frame * @return USB_FDCAN_STATUS */ USB_FDCAN_STATUS transmit( uint32_t id, const uint8_t* data = nullptr, int length = 0, USB_FDCAN_ID_TYPE id_type = USB_FDCAN_ID_STD, USB_FDCAN_FRAME_TYPE frame_type = USB_FDCAN_FRAME_CLASSIC, bool brs = false ); ``` 6. 接收报文 - 接收报文采用回调的方式,通过函数重载匹配两种不同的回调函数 - 回调函数分为lite和normal两种版本,lite只识别标准ID,经典CAN报文,normal版本识别所有的报文类型 - listen_all注册的回调函数会接收所有报文 ``` c++ /** * @brief Listen for CAN/FDCAN frames with standard ID, using lite callback * @param id Standard Identifier to listen * @param callback Callback function pointer * @return USB_FDCAN_STATUS */ USB_FDCAN_STATUS listen( uint16_t id, void (*callback)( uint16_t id, uint8_t* data, int length ) ); /** * @brief Listen for CAN/FDCAN frames with specified ID and type, using fix callback * @param id Identifier to listen * @param id_type Identifier type, standard or extended * @param callback Callback function pointer * @return USB_FDCAN_STATUS */ USB_FDCAN_STATUS listen( uint32_t id, USB_FDCAN_ID_TYPE id_type, void (*callback)( uint32_t id, uint8_t* data, int length, USB_FDCAN_ID_TYPE id_type, USB_FDCAN_FRAME_TYPE frame_type ) ); /** * @brief Listen for all CAN/FDCAN frames, using universal callback * @param callback Callback function pointer * @return USB_FDCAN_STATUS */ USB_FDCAN_STATUS listen_all( void (*callback)( uint32_t id, uint8_t* data, int length, USB_FDCAN_ID_TYPE id_type, USB_FDCAN_FRAME_TYPE frame_type ) ); ```