IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 2021-08-11 -> 正文阅读

[C++知识库]2021-08-11

kvaser在linux下的二次开发

  1. linux下kvaser的驱动和SDK包安装
    下载连接: http://www.kvaser.cn/software/7330130980754/V5_13_0/linuxcan.tar.gz
    安装过程:
    解压:tar -xvzf linuxcan.tar.gz
    安装Kernel 头文件:sudo apt-get install build-essentia
    安装所有驱动:make;sudo make install
    确认安装成功:进入linuxcan/canlib/examples 执行:./listChannels
    如果我们安装的驱动是正确的,终端会显示出总线上可用通道的数量

  2. 开发
    在这以C语言和Cmake编译举例
    CMakeLists.txt文件中
    # 添加链接库
    target_link_libraries(kvaser_test canlib)
    头文件中添加#include <canlib.h>
    这样就可以使用kavser的API了.

  3. 简单代码举例

#include <canlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
static void microseconds_sleep(unsigned long uSec) {
    struct timeval tv;
    tv.tv_sec = uSec / 1000000;
    tv.tv_usec = uSec % 1000000;
    int err;
    do {
        err = select(0, NULL, NULL, NULL, &tv);
    } while (err < 0 && errno == EINTR);
}
int main() {
    canHandle hnd_0;
    canStatus status_0;
    canInitializeLibrary();
    hnd_0 = canOpenChannel(0, 0);
    if (hnd_0 < 0) {
        printf("canOpenChannel failed, hnd_0=%d\n", hnd_0);
        return 1;
    }
    status_0 = canSetBusParams(hnd_0, canBITRATE_500K, 0, 0, 0, 0, 0);
    if (status_0 < 0) {
        printf("canSetBusParams failed, status_0=%d\n", status_0);
        return 1;
    }
    status_0 = canBusOn(hnd_0);
    if (status_0 < 0) {
        printf("canBusOn failed, status_0=%d\n", status_0);
        return 1;
    }
    while (1) {
        canStatus status;
        //测试发送
        // unsigned char data_send[8] = {0,2,3,4,5,7,8,9};
        // status = canWrite(hnd_0, 0x0c02a224, data_send, 8, canMSG_EXT);
        // if (status < 0) {
        //     printf("Failed, status == %d\n", status);
        // }
		 //测试一般读取
        long id;
        unsigned char data_read[8];
        unsigned int dlc, flags;
        unsigned long timestamp;
        status = canRead(hnd_0, &id, data_read, &dlc, &flags, &timestamp);
        if (status == canOK) {
            //printf("succeed, status == %d\n", status);
            for (int i = 0; i < 8; i++) {
                printf("date[%d]: %X\n", i, data_read[i]);
            }
        } else {
            printf("Failed, status == %d\n", status);
        }
        //测试确定ID读取
        //char data_read[8] = {0, 1, 2, 3, 4, 5, 6, 7};
        //uint32_t dlc, flags;
        //uint64_t timestamp;
        //status = canReadSpecific(hnd_0, 0x7FF, &data_read[0], &dlc, &flags, &timestamp);
        //if (status == canOK) {
        //    //printf("succeed, status == %d\n", status);
        //    for (int i = 0; i < 8; i++) {
        //        printf("date[%d]: %X\n", i, data_read[i]);
        //    }
        //} else {
        //    printf("faild, status == %d\n", status);
        //}
        microseconds_sleep(1000000);//这里添加了一个延时,确保缓存区不一直爆满
    }
    canBusOff(hnd_0);
    canClose(hnd_0);
    return 0;
    }
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-12 16:25:18  更:2021-08-12 16:27:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 7:02:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码