赞
踩
这个CSDN里到处都是,想安装什么版本都可以,建议5.0以上。
首先你需要知道你是基于什么协议开发的上位机,我这里是以CAN通讯协议建立一个上位机用于摩托车ABS系统的烧录以及数据的采集等功能。
一般来说你所使用的烧录仪器有很多种,我只接触过CANalyst-2和PCAN分析仪,所需要的开发文件是一般官网会提供相应的开发文件,有二次开发的文件示例,很多说明文件,以及软件应用教程,还有最最重要的就是他们提供的库文件。这些准备好了先需要将他给的库文件导入到你所建立的Qt项目中,你可以先将dll文件,.lib文件和库文件(PCANBasic.h)放进你所建立的项目文件夹中,注意看清楚你所需要开发的下位机是什么型号选择正确的文件(例如什么ARM,X86,X64)不然会出错。
然后你的pro文件配置需要注意,引入PCANBasic.h,下面是我的配置,供参考:
- QT += core gui
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- CONFIG += c++11
-
- # You can make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
-
- SOURCES += \
- main.cpp \
- mainwindow.cpp \
- pcan_thread.cpp \
- protocolthread.cpp
-
- HEADERS += \
- PCANBasic.h \
- mainwindow.h \
- pcan_thread.h \
- protocolthread.h
-
- FORMS += \
- mainwindow.ui
-
- LIBS += -L$$PWD/./ -lPCANBasic
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
-
- RESOURCES += \
- QSS.qrc
-
- RC_ICONS = ar.ico
-
- DISTFILES += \
- explain

其他的基本不需要什么操作,主要是配置.h文件路径
LIBS += -L$$PWD/./ -lPCANBasic
到这里差不多可以开始你的开发了。
1,首先要知道你的CAN的.h文件中的结构体以及函数等,整个通讯过程分为打开CAN通道,初始化CAN通道,发送信息,接受消息,一般来说,你的发送消息需要根据你的下位机接受逻辑来发送,例如发送顺序为43218765,需要你通过算式转换一下。然后就是简单的调用发送和接受函数进行通讯。
2.我的示例代码及注释如下:
- #include "pcan_thread.h"
- #include "PCANBasic.h"
- #include <QTime>
- #include <QCoreApplication>
- #include <QMetaType>
- #include <string.h>
- #include <QDebug>
- #include <QString>
-
- TPCANChannelInformation vbi;
- PCANThread::PCANThread()
- {
- m_stopped = false;
- qRegisterMetaType<TPCANMsg>("TPCANMsg");
- qRegisterMetaType<DWORD>("DWORD");
- }
-
- void PCANThread::stop()
- {
- m_stopped = true; //标志位
- }
-
- bool PCANThread::openPCAN()
- {
- TPCANStatus status;
- TPCANHandle channel = m_channel; // 这应当取决于您具体使用哪个通道
- TPCANBaudrate baudrate = m_baudrate; // 使用的波特率
- // 初始化CAN通道
- status = CAN_Initialize(channel, baudrate);
- if (status != PCAN_ERROR_OK)
- {
- // 初始化失败的处理
- qDebug() << "Initialize CAN channel failed";
- return false;
- }
- // 开始CAN通讯
- status = CAN_Reset(channel);
- if (status != PCAN_ERROR_OK)
- {
- // 重置CAN通道失败的处理
- qDebug() << "Reset CAN channel failed";
- return false;
- }
- return true;
- }
-
- void PCANThread::ClosePCAN()
- {
- TPCANStatus status = CAN_Uninitialize(PCAN_USBBUS1);
- if (status != PCAN_ERROR_OK)
- {
- //连接已关闭
- qDebug() << "PCAN channel 已关闭";
- }
- }
-
- //发送数据--用于发送一帧数据
- void PCANThread::SendOnePCANData(int ID, QString str) // 发送数据,ID 表示 CAN 帧的标识符
- {
- TPCANMsg msg; // 定义 CAN 消息结构体
- DWORD status; // 用于储存 PCAN 函数的返回状态
- int count = 0;
- msg.ID = ID; // 标识符
- msg.MSGTYPE = PCAN_MESSAGE_STANDARD; // 设置为标准帧
- msg.LEN = (BYTE)8; // 数据长度
-
- // 处理数据并将数据填充到 CAN 帧的数据区中
- QString str1;
- for (int j = 0; j < 8; j++)
- {
- str1 = str.mid(j * 2, 2); // 提取字符串str中的每两个字符(一个十六进制数)
- bool ok; // 一个标志,用于检查转换是否成功
- uint hexValue = str1.toUInt(&ok, 16); // 将提取的两个字符的十六进制表示转换为对应的整数
-
- if(ok) // 如果转换成功
- msg.DATA[j] = hexValue; // 将转换后的整数存储到 CAN 消息的数据段j位置
- else
- qDebug() << "转换错误: 无法转换" << str1 << "到十六进制值";
- }
-
- // 发送 CAN 消息
- status = CAN_Write(PCAN_USBBUS1, &msg);
- if(status == PCAN_ERROR_OK)
- {
- count++;
- }
- if(count>0)
- qDebug()<<"发送帧数:"<<count;
- else
- qDebug()<<"发送错误:"<<count;
-
- }
-
- // CAN通信线程的发送多帧数据函数的PCAN版本
- void PCANThread::SendMorePCANData(int ID, QString& str) {
- // 字符串大小表示字节的两倍,因为一个字节由两个十六进制字符表示
- int count = 0;
- int i;
- int byteCount = str.size() / 2; //共多少个字节4096
- int fullFrames = byteCount / 8; // 计算完整的8字节CAN消息的数量
- int remainingBytes = byteCount % 8; // 处理最后一帧的字节数
-
- if(remainingBytes>0)
- {
- byteCount = fullFrames + 1;
- }
- else
- {
- byteCount = fullFrames;
- }
- qDebug() << "共多少个字节:"<< byteCount;
- TPCANMsg canMsg[25000];
- DWORD status;
-
- QString byteStr;
- // 按照原始传输的定制模式填充满8字节的数据帧
- for (i = 0; i < fullFrames; ++i) {
- canMsg[i].ID = ID;
- canMsg[i].MSGTYPE = PCAN_MESSAGE_STANDARD;
- canMsg[i].LEN = 8;
-
- // 填充前四个字节,按照4321的顺序
- for (int j = 0; j < 4; ++j) {
- byteStr = str.mid(((3 - j)*2 + i*16), 2);
- bool k;
- unsigned int c = byteStr.toUInt(&k, 16);
- canMsg[i].DATA[j] = (char)c;
- }
-
- // 填充后四个字节,按照8765的顺序
- for (int j = 4; j < 8; ++j) {
- byteStr = str.mid((11 - j)*2 + i*16, 2);
- bool k;
- unsigned int c = byteStr.toUInt(&k, 16);
- canMsg[i].DATA[j] = (char)c;
- }
- }
-
- // 处理最后一帧不满8字节的情况
- if (remainingBytes > 0)
- {
- canMsg[i].ID = ID;
- canMsg[i].MSGTYPE = PCAN_MESSAGE_STANDARD; // 根据需要调整为 PCAN_MESSAGE_EXTENDED
- canMsg[i].LEN = remainingBytes; // 设置实际的剩余字节长度
-
- // 按照定制模式填充不完整的数据帧
- if (remainingBytes < 5)
- {
- // 如果剩余字节数小于5,则只填充数据帧前半部分
- for (int j = 0; j < remainingBytes; ++j)
- {
- byteStr = str.mid((((remainingBytes - 1) - j)*2 + i*16), 2);
- bool k;
- unsigned int c = byteStr.toUInt(&k, 16);
- canMsg[i].DATA[j] = (char)c;
- }
- // 剩余的部分填充为0xFF
- for (int j = remainingBytes; j < 8; ++j)
- {
- canMsg[i].DATA[j] = 0xFF;
- }
- }
- else
- {
- // 如果剩余字节数大于或等于5,则分两部分填充
- // 先填前四个字节
- for (int j = 0; j < 4; ++j) {
- QString byteStr = str.mid(((3 - j)*2 + i*16), 2);
- bool k;
- unsigned int c = byteStr.toUInt(&k, 16);
- canMsg[i].DATA[j] = (char)c;
- }
- // 然后填剩下的字节
- for (int j = 4; j < remainingBytes; ++j) {
- QString byteStr = str.mid((remainingBytes + 3 - j)*2 + i*16, 2);
- bool k;
- unsigned int c = byteStr.toUInt(&k, 16);
- canMsg[i].DATA[j] = (char)c;
- }
- // 剩余的部分填充为0xFF
- for (int j = remainingBytes; j < 8; ++j) {
- canMsg[i].DATA[j] = 0xFF;
- }
- }
- }
- // 发送
- for(i = 0;i < byteCount;i++)
- {
- status = CAN_Write(PCAN_USBBUS1, &canMsg[i]);
- if(status == PCAN_ERROR_OK)
- {
- count++;
- }
- if(count>0)
- qDebug()<<"发送帧数:"<<count;
- else
- qDebug()<<"发送错误:"<<count;
-
- }
- sleep(500);
-
- }
-
- void PCANThread::run()
- {
- int i = 0;
- TPCANTimestamp pcanTimestamp;
- TPCANMsg Message[1];
- TPCANStatus status;
- while(!m_stopped)
- {
- memset(Message, 0, sizeof(Message));
- status = CAN_Read(m_channel, &Message[i], &pcanTimestamp);
- if(status == PCAN_ERROR_OK)
- {
- if((Message[i].ID == 0x133 || Message[i].ID == 0x135 || Message[i].ID == 0x223)) // 成功接收
- {
- emit getPCANdatafordownload(&Message[i]); // 发送烧录信号到槽函数
- msleep(800);
- }
- else if(Message[i].ID != 0x133 && Message[i].ID != 0x135 && Message[i].ID != 0x223)
- {
- emit getPCANdataforcollect(&Message[i]);// 发送采集及其他信号到槽函数
- //采集需不需要
- msleep(5);
- }
- }
- else
- {
- // 对应CAN设备失效,重新打开设备
- qDebug()<<"设备不存在或USB掉线";
- CAN_Uninitialize(m_channel);
- CAN_Initialize(m_channel, PCAN_BAUD_500K); // 这个函数需要手动输入波特率
- }
- sleep(1000);
- }
- m_stopped = false;
- }
-
- void PCANThread::sleep(unsigned int msec)
- {
- QTime dieTime = QTime::currentTime().addMSecs(msec);
- while( QTime::currentTime() < dieTime )
- QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
- }

3.后面就是通过你的信号与槽,实现接受信息并且处理信息,以及建立一些界面的交互了,这些就不放代码了自行理会吧。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。