驱动下载地址:can卡驱动下载地址
1. ubuntu下安装can卡驱动
这里使用的是 ubuntu16.04 虽然说明上没说 16.04 可以使用,但是实际测试是可以的。
cd advcan_source_v2.0.19.0/driver/
make
sudo make install
sudo make nodes
cd utils/
sudo sh ./cansetup ../etc/advpci.conf
应该就可以了。
2. 测试程序
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "can4linux.h"
#define STDDEV "can0"
#define MAX 500
using namespace std;
canmsg_t tx_state[MAX];
canmsg_t tx[MAX];
canmsg_t rx[MAX];
int fd;
int got=0;
int main(int argc, char **argv)
{
int i, sent = 0, count = 0;
char device[40];
if(argc == 2)
{
sprintf(device, "/dev/%s", argv[1]);
}
else
{
sprintf(device, "/dev/%s", STDDEV);
}
printf("using CAN device %s\n", device);
if(( fd = open(device, O_RDWR|O_NONBLOCK )) < 0 )
{
cout<<fd<<endl;
fprintf(stderr,"Error opening CAN device %s\n", device);
exit(1);
}
while(1)
{
tx_state[0].id=0x282;
tx_state[0].length=1;
tx_state[0].data[0]= 0xFF;
write(fd,&tx_state[0],1);
cout << "send!" << endl;
got=read(fd, rx, MAX);
if( got > 0)
{
unsigned int temp=0;
int num=0;
while(num<got)
{
cout << "rx[num].id:" << rx[num].id << endl;
num++;
}
}
}
close(fd);
return 0;
}