kvaser在linux下的二次开发

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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值