usbcan_0">一.usbcan环境搭建
可以参照我的这篇博客:USBCAN-II/II+使用方法以及qt操作介绍
二.项目效果展示
三.项目代码
- 这部分代码仅仅展示了部分功能,仅供参考。
#include"ControlCAN.h"
#include<QDebug>
#include <windows.h>
#include<stdio.h>
#include"reveivethread.h"
#include<QVector>#define DEVICE_TYPE 4 // 设备类型
#define DEVICE_INDEX 0 // 设备索引
#define CAN_INDEX 0 // CAN 通道索引1
#define CAN_INDEX2 1 // CAN 通道索引2MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);Init_Connect();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_start_clicked()
{ui->textEdit->clear();// 初始化CAN设备//VCI_OpenDevice(DWORD DeviceType,DWORD DeviceInd,DWORD Reserved)if (VCI_OpenDevice(DEVICE_TYPE, DEVICE_INDEX, 0) == 0){qDebug()<<"Device open fail!";ui->textEdit->append("Device open fail!");return ;}else{qDebug()<<"Device open successful!";ui->textEdit->append("Device open successful!");}// 初始化配置结构VCI_INIT_CONFIG initConfig;initConfig.AccCode = 0x00000000; // 接收过滤器initConfig.AccMask = 0xFFFFFFFF; // 接收掩码initConfig.Filter = 0; // 过滤器类型initConfig.Timing0 = 0x00; // 时间参数initConfig.Timing1 = 0x14; // 时间参数initConfig.Mode = 0; // 工作模式// 设置CAN通道1//VCI_InitCAN(DWORD DeviceType, DWORD DeviceInd, DWORD CANInd, PVCI_INIT_CONFIG pInitConfig);if (VCI_InitCAN(DEVICE_TYPE, DEVICE_INDEX, CAN_INDEX, &initConfig) == 0) {//printf("初始化CAN通道失败!\n");qDebug()<<"InitCAN 1 fail!";ui->textEdit->append("InitCAN 1 fail!");return ;}else{qDebug()<<"InitCAN 1 successful!";ui->textEdit->append("InitCAN 1 successful!");}// 设置CAN通道2//VCI_InitCAN(DWORD DeviceType, DWORD DeviceInd, DWORD CANInd, PVCI_INIT_CONFIG pInitConfig);if (VCI_InitCAN(DEVICE_TYPE, DEVICE_INDEX, CAN_INDEX2, &initConfig) == 0) {//printf("初始化CAN通道失败!\n");qDebug()<<"InitCAN 2 fail!";ui->textEdit->append("InitCAN 2 fail!");return ;}else{qDebug()<<"InitCAN 2 successful!";ui->textEdit->append("InitCAN 2 successful!");}// 启动CAN通道1if (VCI_StartCAN(DEVICE_TYPE, DEVICE_INDEX, CAN_INDEX) == 0) {//printf("启动CAN通道失败!\n ");qDebug()<<"StartCAN 1 fail!";ui->textEdit->append("StartCAN 1 fail!");return ;}else{qDebug()<<"StartCAN 1 successful!";ui->textEdit->append("StartCAN 1 successful!");}// 启动CAN通道2if (VCI_StartCAN(DEVICE_TYPE, DEVICE_INDEX, CAN_INDEX2) == 0) {//printf("启动CAN通道失败!\n ");qDebug()<<"StartCAN 2 fail!";ui->textEdit->append("StartCAN 2 fail!");return ;}else{qDebug()<<"StartCAN 2 successful!";ui->textEdit->append("StartCAN 2 successful!");}}void MainWindow::on_pushButton_close_clicked()
{// 关闭设备if(VCI_CloseDevice(DEVICE_TYPE, DEVICE_INDEX)){qDebug()<<"CloseDevice successful!";ui->textEdit->append("CloseDevice successful!");}else{qDebug()<<"CloseDevice fail!";ui->textEdit->append("CloseDevice fail!");}//关闭线程//rev.wait();
}void MainWindow::on_pushButton_clear_clicked()
{ui->textEdit->clear();
}void MainWindow::on_pushButton_send_clicked()
{//QString str=("01 02 03 04 05 06 07 08");//现在需要的请求是需要将每个字节都正确的发送出来以0x01的形式// 输入串QString str = ui->lineEdit->text();QStringList hexValues = str.split(" "); // 使用空格分割字符串//存放16进制数字QVector<int>Int_16;// 遍历每个十六进制值并格式化输出for (const QString &value : hexValues){bool ok;int number = value.toInt(&ok, 16); // 将十六进制字符串转换为整数Int_16.push_back(number);if (ok){qDebug() << QString("0x%1").arg(number, 2, 16, QLatin1Char('0')).toUpper(); // 格式化为0x形式并打印} else{qDebug() << "Invalid hex value:" << value; // 错误处理}}VCI_CAN_OBJ vco;// 定义两帧的结构体数组// 中间略去其他函数代码vco.ID = 0x00000001;// 填写第一帧的IDvco.SendType = 0;// 正常发送模式vco.RemoteFlag = 0;// 数据帧vco.ExternFlag = 0;// 标准帧vco.DataLen = 8;// 数据长度1个字节//vco.Data[0] = number;// 数据0为0x66,十进制102//将结果存入到Data中for(int i=0;i<8;i++){vco.Data[i]=Int_16[i];}DWORD dwRel = VCI_Transmit(DEVICE_TYPE, DEVICE_INDEX, CAN_INDEX, &vco, 1); // 发送1帧if(dwRel){qDebug()<<"nCANInd 1 transmit successful,dwRel="<<dwRel;ui->textEdit->append("nCANInd 1 transmit successful,dwRel= "+QString::number(dwRel));}else{qDebug()<<"nCANInd 1 transmit fail";ui->textEdit->append("nCANInd 1 transmit fail");}rev.start();rev.current_strart();//rev.sleep(1);rev.wait();//rev.stop();
}