当前位置:   article > 正文

Qt上位机开发入门(PCAN)_pcan qt

pcan qt

一.安装Qt开发环境与创建项目

        这个CSDN里到处都是,想安装什么版本都可以,建议5.0以上。

二.准备工作

        首先你需要知道你是基于什么协议开发的上位机,我这里是以CAN通讯协议建立一个上位机用于摩托车ABS系统的烧录以及数据的采集等功能。

        一般来说你所使用的烧录仪器有很多种,我只接触过CANalyst-2和PCAN分析仪,所需要的开发文件是一般官网会提供相应的开发文件,有二次开发的文件示例,很多说明文件,以及软件应用教程,还有最最重要的就是他们提供的库文件。这些准备好了先需要将他给的库文件导入到你所建立的Qt项目中,你可以先将dll文件,.lib文件和库文件(PCANBasic.h)放进你所建立的项目文件夹中,注意看清楚你所需要开发的下位机是什么型号选择正确的文件(例如什么ARM,X86,X64)不然会出错。

        然后你的pro文件配置需要注意,引入PCANBasic.h,下面是我的配置,供参考:

  1. QT += core gui
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  3. CONFIG += c++11
  4. # You can make your code fail to compile if it uses deprecated APIs.
  5. # In order to do so, uncomment the following line.
  6. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  7. SOURCES += \
  8. main.cpp \
  9. mainwindow.cpp \
  10. pcan_thread.cpp \
  11. protocolthread.cpp
  12. HEADERS += \
  13. PCANBasic.h \
  14. mainwindow.h \
  15. pcan_thread.h \
  16. protocolthread.h
  17. FORMS += \
  18. mainwindow.ui
  19. LIBS += -L$$PWD/./ -lPCANBasic
  20. # Default rules for deployment.
  21. qnx: target.path = /tmp/$${TARGET}/bin
  22. else: unix:!android: target.path = /opt/$${TARGET}/bin
  23. !isEmpty(target.path): INSTALLS += target
  24. RESOURCES += \
  25. QSS.qrc
  26. RC_ICONS = ar.ico
  27. DISTFILES += \
  28. explain

 其他的基本不需要什么操作,主要是配置.h文件路径

LIBS += -L$$PWD/./ -lPCANBasic

 到这里差不多可以开始你的开发了。

三.你的上位机开发逻辑

1,首先要知道你的CAN的.h文件中的结构体以及函数等,整个通讯过程分为打开CAN通道,初始化CAN通道,发送信息,接受消息,一般来说,你的发送消息需要根据你的下位机接受逻辑来发送,例如发送顺序为43218765,需要你通过算式转换一下。然后就是简单的调用发送和接受函数进行通讯。

2.我的示例代码及注释如下:

  1. #include "pcan_thread.h"
  2. #include "PCANBasic.h"
  3. #include <QTime>
  4. #include <QCoreApplication>
  5. #include <QMetaType>
  6. #include <string.h>
  7. #include <QDebug>
  8. #include <QString>
  9. TPCANChannelInformation vbi;
  10. PCANThread::PCANThread()
  11. {
  12. m_stopped = false;
  13. qRegisterMetaType<TPCANMsg>("TPCANMsg");
  14. qRegisterMetaType<DWORD>("DWORD");
  15. }
  16. void PCANThread::stop()
  17. {
  18. m_stopped = true; //标志位
  19. }
  20. bool PCANThread::openPCAN()
  21. {
  22. TPCANStatus status;
  23. TPCANHandle channel = m_channel; // 这应当取决于您具体使用哪个通道
  24. TPCANBaudrate baudrate = m_baudrate; // 使用的波特率
  25. // 初始化CAN通道
  26. status = CAN_Initialize(channel, baudrate);
  27. if (status != PCAN_ERROR_OK)
  28. {
  29. // 初始化失败的处理
  30. qDebug() << "Initialize CAN channel failed";
  31. return false;
  32. }
  33. // 开始CAN通讯
  34. status = CAN_Reset(channel);
  35. if (status != PCAN_ERROR_OK)
  36. {
  37. // 重置CAN通道失败的处理
  38. qDebug() << "Reset CAN channel failed";
  39. return false;
  40. }
  41. return true;
  42. }
  43. void PCANThread::ClosePCAN()
  44. {
  45. TPCANStatus status = CAN_Uninitialize(PCAN_USBBUS1);
  46. if (status != PCAN_ERROR_OK)
  47. {
  48. //连接已关闭
  49. qDebug() << "PCAN channel 已关闭";
  50. }
  51. }
  52. //发送数据--用于发送一帧数据
  53. void PCANThread::SendOnePCANData(int ID, QString str) // 发送数据,ID 表示 CAN 帧的标识符
  54. {
  55. TPCANMsg msg; // 定义 CAN 消息结构体
  56. DWORD status; // 用于储存 PCAN 函数的返回状态
  57. int count = 0;
  58. msg.ID = ID; // 标识符
  59. msg.MSGTYPE = PCAN_MESSAGE_STANDARD; // 设置为标准帧
  60. msg.LEN = (BYTE)8; // 数据长度
  61. // 处理数据并将数据填充到 CAN 帧的数据区中
  62. QString str1;
  63. for (int j = 0; j < 8; j++)
  64. {
  65. str1 = str.mid(j * 2, 2); // 提取字符串str中的每两个字符(一个十六进制数)
  66. bool ok; // 一个标志,用于检查转换是否成功
  67. uint hexValue = str1.toUInt(&ok, 16); // 将提取的两个字符的十六进制表示转换为对应的整数
  68. if(ok) // 如果转换成功
  69. msg.DATA[j] = hexValue; // 将转换后的整数存储到 CAN 消息的数据段j位置
  70. else
  71. qDebug() << "转换错误: 无法转换" << str1 << "到十六进制值";
  72. }
  73. // 发送 CAN 消息
  74. status = CAN_Write(PCAN_USBBUS1, &msg);
  75. if(status == PCAN_ERROR_OK)
  76. {
  77. count++;
  78. }
  79. if(count>0)
  80. qDebug()<<"发送帧数:"<<count;
  81. else
  82. qDebug()<<"发送错误:"<<count;
  83. }
  84. // CAN通信线程的发送多帧数据函数的PCAN版本
  85. void PCANThread::SendMorePCANData(int ID, QString& str) {
  86. // 字符串大小表示字节的两倍,因为一个字节由两个十六进制字符表示
  87. int count = 0;
  88. int i;
  89. int byteCount = str.size() / 2; //共多少个字节4096
  90. int fullFrames = byteCount / 8; // 计算完整的8字节CAN消息的数量
  91. int remainingBytes = byteCount % 8; // 处理最后一帧的字节数
  92. if(remainingBytes>0)
  93. {
  94. byteCount = fullFrames + 1;
  95. }
  96. else
  97. {
  98. byteCount = fullFrames;
  99. }
  100. qDebug() << "共多少个字节:"<< byteCount;
  101. TPCANMsg canMsg[25000];
  102. DWORD status;
  103. QString byteStr;
  104. // 按照原始传输的定制模式填充满8字节的数据帧
  105. for (i = 0; i < fullFrames; ++i) {
  106. canMsg[i].ID = ID;
  107. canMsg[i].MSGTYPE = PCAN_MESSAGE_STANDARD;
  108. canMsg[i].LEN = 8;
  109. // 填充前四个字节,按照4321的顺序
  110. for (int j = 0; j < 4; ++j) {
  111. byteStr = str.mid(((3 - j)*2 + i*16), 2);
  112. bool k;
  113. unsigned int c = byteStr.toUInt(&k, 16);
  114. canMsg[i].DATA[j] = (char)c;
  115. }
  116. // 填充后四个字节,按照8765的顺序
  117. for (int j = 4; j < 8; ++j) {
  118. byteStr = str.mid((11 - j)*2 + i*16, 2);
  119. bool k;
  120. unsigned int c = byteStr.toUInt(&k, 16);
  121. canMsg[i].DATA[j] = (char)c;
  122. }
  123. }
  124. // 处理最后一帧不满8字节的情况
  125. if (remainingBytes > 0)
  126. {
  127. canMsg[i].ID = ID;
  128. canMsg[i].MSGTYPE = PCAN_MESSAGE_STANDARD; // 根据需要调整为 PCAN_MESSAGE_EXTENDED
  129. canMsg[i].LEN = remainingBytes; // 设置实际的剩余字节长度
  130. // 按照定制模式填充不完整的数据帧
  131. if (remainingBytes < 5)
  132. {
  133. // 如果剩余字节数小于5,则只填充数据帧前半部分
  134. for (int j = 0; j < remainingBytes; ++j)
  135. {
  136. byteStr = str.mid((((remainingBytes - 1) - j)*2 + i*16), 2);
  137. bool k;
  138. unsigned int c = byteStr.toUInt(&k, 16);
  139. canMsg[i].DATA[j] = (char)c;
  140. }
  141. // 剩余的部分填充为0xFF
  142. for (int j = remainingBytes; j < 8; ++j)
  143. {
  144. canMsg[i].DATA[j] = 0xFF;
  145. }
  146. }
  147. else
  148. {
  149. // 如果剩余字节数大于或等于5,则分两部分填充
  150. // 先填前四个字节
  151. for (int j = 0; j < 4; ++j) {
  152. QString byteStr = str.mid(((3 - j)*2 + i*16), 2);
  153. bool k;
  154. unsigned int c = byteStr.toUInt(&k, 16);
  155. canMsg[i].DATA[j] = (char)c;
  156. }
  157. // 然后填剩下的字节
  158. for (int j = 4; j < remainingBytes; ++j) {
  159. QString byteStr = str.mid((remainingBytes + 3 - j)*2 + i*16, 2);
  160. bool k;
  161. unsigned int c = byteStr.toUInt(&k, 16);
  162. canMsg[i].DATA[j] = (char)c;
  163. }
  164. // 剩余的部分填充为0xFF
  165. for (int j = remainingBytes; j < 8; ++j) {
  166. canMsg[i].DATA[j] = 0xFF;
  167. }
  168. }
  169. }
  170. // 发送
  171. for(i = 0;i < byteCount;i++)
  172. {
  173. status = CAN_Write(PCAN_USBBUS1, &canMsg[i]);
  174. if(status == PCAN_ERROR_OK)
  175. {
  176. count++;
  177. }
  178. if(count>0)
  179. qDebug()<<"发送帧数:"<<count;
  180. else
  181. qDebug()<<"发送错误:"<<count;
  182. }
  183. sleep(500);
  184. }
  185. void PCANThread::run()
  186. {
  187. int i = 0;
  188. TPCANTimestamp pcanTimestamp;
  189. TPCANMsg Message[1];
  190. TPCANStatus status;
  191. while(!m_stopped)
  192. {
  193. memset(Message, 0, sizeof(Message));
  194. status = CAN_Read(m_channel, &Message[i], &pcanTimestamp);
  195. if(status == PCAN_ERROR_OK)
  196. {
  197. if((Message[i].ID == 0x133 || Message[i].ID == 0x135 || Message[i].ID == 0x223)) // 成功接收
  198. {
  199. emit getPCANdatafordownload(&Message[i]); // 发送烧录信号到槽函数
  200. msleep(800);
  201. }
  202. else if(Message[i].ID != 0x133 && Message[i].ID != 0x135 && Message[i].ID != 0x223)
  203. {
  204. emit getPCANdataforcollect(&Message[i]);// 发送采集及其他信号到槽函数
  205. //采集需不需要
  206. msleep(5);
  207. }
  208. }
  209. else
  210. {
  211. // 对应CAN设备失效,重新打开设备
  212. qDebug()<<"设备不存在或USB掉线";
  213. CAN_Uninitialize(m_channel);
  214. CAN_Initialize(m_channel, PCAN_BAUD_500K); // 这个函数需要手动输入波特率
  215. }
  216. sleep(1000);
  217. }
  218. m_stopped = false;
  219. }
  220. void PCANThread::sleep(unsigned int msec)
  221. {
  222. QTime dieTime = QTime::currentTime().addMSecs(msec);
  223. while( QTime::currentTime() < dieTime )
  224. QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
  225. }

 3.后面就是通过你的信号与槽,实现接受信息并且处理信息,以及建立一些界面的交互了,这些就不放代码了自行理会吧。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/133034
推荐阅读
相关标签
  

闽ICP备14008679号