//样例只是提供一个简单的调用so库的方法供参考,程序接收,与发送函数设置在两个线程中,并且线程没有同步。
//现实中客户编程中,发送与接收函数不能同时调用(不支持多线程),如果在多线程中,一定需要互锁。需要客户自行完善代码。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include "controlcan.h"
#include <ctime>
#include <cstdlib>
#include "unistd.h"
VCI_BOARD_INFO pInfo;//用来获取设备信息。
int count=0;//数据列表中,用来存储列表序号。
VCI_BOARD_INFO pInfo1 [50];
int num=0;
void *receive_func(void* param) //接收线程。
{
int reclen=0;
VCI_CAN_OBJ rec[3000];//接收缓存,设为3000为佳。
int i,j;
int *run=(int*)param;//线程启动,退出控制。
int ind=0;
printf(" 1 enter receive_func \n ");
while(1)//((*run)&0x0f)
{
//printf(" enter 11 receive_func ind=%d \n ",ind);
if((reclen=VCI_Receive(VCI_USBCAN2,0,ind,rec,3000,100))>0)//调用接收函数,如果有数据,进行数据处理显示。
{
printf("recv reclen = %d\n ",reclen);
for(j=0;j<reclen;j++)
{
printf("Index:%04d ",count);count++;//序号递增
printf("CAN%d RX ID:0x%08X", ind+1, rec[j].ID);//ID
if(rec[j].ExternFlag==0) printf(" Standard ");//帧格式:标准帧
if(rec[j].ExternFlag==1) printf(" Extend ");//帧格式:扩展帧
if(rec[j].RemoteFlag==0) printf(" Data ");//帧类型:数据帧
if(rec[j].RemoteFlag==1) printf(" Remote ");//帧类型:远程帧
printf("DLC:0x%02X",rec[j].DataLen);//帧长度
printf(" data:0x"); //数据
for(i = 0; i < rec[j].DataLen; i++)
{
printf(" %02X", rec[j].Data[i]);
}
printf(" TimeStamp:0x%08X",rec[j].TimeStamp);//时间标识。
printf("\n");
}
}
else if (reclen == -1)
{
printf(" no can dev,%d\n ",reclen);
}
ind=!ind;//变换通道号,以便下次读取另一通道,交替读取。
usleep(100);
}
printf("run thread exit\n");//退出接收线程
pthread_exit(0);
}
main()
{
printf(">>this is hello !\r\n");//指示程序已运行
num=VCI_FindUsbDevice2(pInfo1);
printf(">>USBCAN DEVICE NUM:");printf("%d", num);printf(" PCS");printf("\n");
for(int i=0;i<num;i++)
{
printf("Device:");printf("%d", i);printf("\n");
printf(">>Get VCI_ReadBoardInfo success!\n");
printf(">>Serial_Num:%c", pInfo1[i].str_Serial_Num[0]);
printf("%c", pInfo1[i].str_Serial_Num[1]);
printf("%c", pInfo1[i].str_Serial_Num[2]);
printf("%c", pInfo1[i].str_Serial_Num[3]);
printf("%c", pInfo1[i].str_Serial_Num[4]);
printf("%c", pInfo1[i].str_Serial_Num[5]);
printf("%c", pInfo1[i].str_Serial_Num[6]);
printf("%c", pInfo1[i].str_Serial_Num[7]);
printf("%c", pInfo1[i].str_Serial_Num[8]);
printf("%c", pInfo1[i].str_Serial_Num[9]);
printf("%c", pInfo1[i].str_Serial_Num[10]);
printf("%c", pInfo1[i].str_Serial_Num[11]);
printf("%c", pInfo1[i].str_Serial_Num[12]);
printf("%c", pInfo1[i].str_Serial_Num[13]);
printf("%c", pInfo1[i].str_Serial_Num[14]);
printf("%c", pInfo1[i].str_Serial_Num[15]);
printf("%c", pInfo1[i].str_Serial_Num[16]);
printf("%c", pInfo1[i].str_Serial_Num[17]);
printf("%c", pInfo1[i].str_Serial_Num[18]);
printf("%c", pInfo1[i].str_Serial_Num[19]);printf("\n");
printf(">>hw_Type:%c", pInfo1[i].str_hw_Type[0]);
printf("%c", pInfo1[i].str_hw_Type[1]);
printf("%c", pInfo1[i].str_hw_Type[2]);
printf("%c", pInfo1[i].str_hw_Type[3]);
printf("%c", pInfo1[i].str_hw_Type[4]);
printf("%c", pInfo1[i].str_hw_Type[5]);
printf("%c", pInfo1[i].str_hw_Type[6]);
printf("%c", pInfo1[i].str_hw_Type[7]);
printf("%c", pInfo1[i].str_hw_Type[8]);
printf("%c", pInfo1[i].str_hw_Type[9]);printf("\n");
printf(">>Firmware Version:V");
printf("%x", (pInfo1[i].fw_Version&0xF00)>>8);
printf(".");
printf("%x", (pInfo1[i].fw_Version&0xF0)>>4);
printf("%x", pInfo1[i].fw_Version&0xF);
printf("\n");
}
printf(">>\n");
printf(">>\n");
printf(">>\n");
if(VCI_OpenDevice(VCI_USBCAN2,0,0)==1)//打开设备
{
printf(">>open deivce success!\n");//打开设备成功
}else
{
printf(">>open deivce error!\n");
exit(1);
}
if(VCI_ReadBoardInfo(VCI_USBCAN2,0,&pInfo)==1)//读取设备序列号、版本等信息。
{
printf(">>Get VCI_ReadBoardInfo success!\n");
//printf(" %08X", pInfo.hw_Version);printf("\n");
//printf(" %08X", pInfo.fw_Version);printf("\n");
//printf(" %08X", pInfo.dr_Version);printf("\n");
//printf(" %08X", pInfo.in_Version);printf("\n");
//printf(" %08X", pInfo.irq_Num);printf("\n");
//printf(" %08X", pInfo.can_Num);printf("\n");
printf(">>Serial_Num:%c", pInfo.str_Serial_Num[0]);
printf("%c", pInfo.str_Serial_Num[1]);
printf("%c", pInfo.str_Serial_Num[2]);
printf("%c", pInfo.str_Serial_Num[3]);
printf("%c", pInfo.str_Serial_Num[4]);
printf("%c", pInfo.str_Serial_Num[5]);
printf("%c", pInfo.str_Serial_Num[6]);
printf("%c", pInfo.str_Serial_Num[7]);
printf("%c", pInfo.str_Serial_Num[8]);
printf("%c", pInfo.str_Serial_Num[9]);
printf("%c", pInfo.str_Serial_Num[10]);
printf("%c", pInfo.str_Serial_Num[11]);
printf("%c", pInfo.str_Serial_Num[12]);
printf("%c", pInfo.str_Serial_Num[13]);
printf("%c", pInfo.str_Serial_Num[14]);
printf("%c", pInfo.str_Serial_Num[15]);
printf("%c", pInfo.str_Serial_Num[16]);
printf("%c", pInfo.str_Serial_Num[17]);
printf("%c", pInfo.str_Serial_Num[18]);
printf("%c", pInfo.str_Serial_Num[19]);printf("\n");
printf(">>hw_Type:%c", pInfo.str_hw_Type[0]);
printf("%c", pInfo.str_hw_Type[1]);
printf("%c", pInfo.str_hw_Type[2]);
printf("%c", pInfo.str_hw_Type[3]);
printf("%c", pInfo.str_hw_Type[4]);
printf("%c", pInfo.str_hw_Type[5]);
printf("%c", pInfo.str_hw_Type[6]);
printf("%c", pInfo.str_hw_Type[7]);
printf("%c", pInfo.str_hw_Type[8]);
printf("%c", pInfo.str_hw_Type[9]);printf("\n");
printf(">>Firmware Version:V");
printf("%x", (pInfo.fw_Version&0xF00)>>8);
printf(".");
printf("%x", (pInfo.fw_Version&0xF0)>>4);
printf("%x", pInfo.fw_Version&0xF);
printf("\n");
}else
{
printf(">>Get VCI_ReadBoardInfo error!\n");
exit(1);
}
//初始化参数,严格参数二次开发函数库说明书。
VCI_INIT_CONFIG config;
config.AccCode=0;
config.AccMask=0xFFFFFFFF;
config.Filter=1;//01接收所有帧
config.Timing0=0x00;/*波特率125 Kbps 0x03 0x1C*/
config.Timing1=0x1C;
config.Mode=1;//正常模式
if(VCI_InitCAN(VCI_USBCAN2,0,0,&config)!=1)
{
printf(">>Init CAN1 error\n");
VCI_CloseDevice(VCI_USBCAN2,0);
}
if(VCI_StartCAN(VCI_USBCAN2,0,0)!=1)
{
printf(">>Start CAN1 error\n");
VCI_CloseDevice(VCI_USBCAN2,0);
}
if(VCI_InitCAN(VCI_USBCAN2,0,1,&config)!=1)
{
printf(">>Init can2 error\n");
VCI_CloseDevice(VCI_USBCAN2,0);
}
if(VCI_StartCAN(VCI_USBCAN2,0,1)!=1)
{
printf(">>Start can2 error\n");
VCI_CloseDevice(VCI_USBCAN2,0);
}
//需要发送的帧,结构体设置
VCI_CAN_OBJ send[1];
send[0].ID=0;
send[0].SendType=0;
send[0].RemoteFlag=0;
send[0].ExternFlag=0;
send[0].DataLen=8;
int i=0;
for(i = 0; i < send[0].DataLen; i++)
{
send[0].Data[i] = i;
}
int m_run0=1;
pthread_t threadid;
int ret;
ret=pthread_create(&threadid,NULL,receive_func,&m_run0);
int times = 0;
while(times--)
{
if(VCI_Transmit(VCI_USBCAN2, 0, 0, send, 1) == 1)
{
printf("Index:%04d ",count);count++;
printf("CAN1 TX ID:0x%08X",send[0].ID);
if(send[0].ExternFlag==0) printf(" Standard ");
if(send[0].ExternFlag==1) printf(" Extend ");
if(send[0].RemoteFlag==0) printf(" Data ");
if(send[0].RemoteFlag==1) printf(" Remote ");
printf("DLC:0x%02X",send[0].DataLen);
printf(" data:0x");
for(i=0;i<send[0].DataLen;i++)
{
printf(" %02X",send[0].Data[i]);
}
printf("\n");
send[0].ID+=1;
}
else
{
break;
}
if(VCI_Transmit(VCI_USBCAN2, 0, 1, send, 1) == 1)
{
print
qq_44894453
- 粉丝: 1
- 资源: 1
最新资源
- Python实现HTML压缩功能
- 完结26章Java主流分布式解决方案多场景设计与实战
- ECSHOP模板堂最新2017仿E宠物模板 整合ECTouch微分销商城
- Pear Admin 是 一 款 开 箱 即 用 的 前 端 开 发 模 板,提供便捷快速的开发方式,延续 Admin 的设计规范
- 51单片机仿真摇号抽奖机源程序12864液晶显示仿真+程序
- 家庭用具检测21-YOLO(v5至v11)、COCO、Paligemma、TFRecord、VOC数据集合集.rar
- Intel-633246-eASIC-PB-006-N5X-Product-Brief .pdf
- Avue.js是基于现有的element-plus库进行的二次封装,简化一些繁琐的操作,核心理念为数据驱动视图,主要的组件库针对table表格和form表单场景,同时衍生出更多企业常用的组件,达到高复
- STM32F401,使用ST-link时候,不能识别,显示ST-LINK USB communication error
- 快速排序算法Python实现:详解分治法原理与高效排序步骤
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈