serialinfo.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "serialinfo.h"
  2. SerialInfo::SerialInfo()
  3. {
  4. }
  5. void SerialInfo::on_startWork()
  6. {
  7. timer = new QTimer(this);
  8. connect(timer, &QTimer::timeout, this, &SerialInfo::on_Check_PDUStatus);
  9. timer->start(2000); // Start the timer
  10. }
  11. void SerialInfo::on_closeWork()
  12. {
  13. if(timer)timer->stop();
  14. deleteLater();
  15. }
  16. void SerialInfo::on_Check_PDUStatus()
  17. {
  18. //轮询PDU信息
  19. char OrderArray[8];
  20. uint16_t crc=0;
  21. OrderArray[0]=0x01;
  22. OrderArray[1]=0x03;
  23. //查询设备类型及通道数
  24. OrderArray[2] = (_SWITCH_T_AC_TYPE_CHN_INFO >> 8) & 0xFF; // High byte
  25. OrderArray[3] = _SWITCH_T_AC_TYPE_CHN_INFO & 0xFF; // Low byte
  26. OrderArray[4]=0x00;
  27. OrderArray[5]=0x01;
  28. crc = crc16_ccitt(OrderArray, 6);
  29. OrderArray[6] = crc & 0xFF; // Low byte
  30. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  31. SendString=on_SendInfo(OrderArray,8);
  32. RecString=on_Rec_PDUStatus();
  33. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_TYPE_CHN_INFO);
  34. QThread::msleep(QUERYSTAMP);
  35. //查询L1信息
  36. OrderArray[2] = (_SWITCH_T_AC_IN_L1_INFO >> 8) & 0xFF; // High byte
  37. OrderArray[3] = _SWITCH_T_AC_IN_L1_INFO & 0xFF; // Low byte
  38. OrderArray[4]=0x00;
  39. OrderArray[5]=0x08;
  40. crc = crc16_ccitt(OrderArray, 6);
  41. OrderArray[6] = crc & 0xFF; // Low byte
  42. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  43. SendString=on_SendInfo(OrderArray,8);
  44. RecString=on_Rec_PDUStatus();
  45. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_IN_L1_INFO);
  46. QThread::msleep(QUERYSTAMP);
  47. //查询L2信息
  48. OrderArray[2] = (_SWITCH_T_AC_IN_L2_INFO >> 8) & 0xFF; // High byte
  49. OrderArray[3] = _SWITCH_T_AC_IN_L2_INFO & 0xFF; // Low byte
  50. OrderArray[4]=0x00;
  51. OrderArray[5]=0x08;
  52. crc = crc16_ccitt(OrderArray, 6);
  53. OrderArray[6] = crc & 0xFF; // Low byte
  54. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  55. SendString=on_SendInfo(OrderArray,8);
  56. RecString=on_Rec_PDUStatus();
  57. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_IN_L2_INFO);
  58. QThread::msleep(QUERYSTAMP);
  59. //查询L3信息
  60. OrderArray[2] = (_SWITCH_T_AC_IN_L3_INFO >> 8) & 0xFF; // High byte
  61. OrderArray[3] = _SWITCH_T_AC_IN_L3_INFO & 0xFF; // Low byte
  62. OrderArray[4]=0x00;
  63. OrderArray[5]=0x08;
  64. crc = crc16_ccitt(OrderArray, 6);
  65. OrderArray[6] = crc & 0xFF; // Low byte
  66. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  67. SendString=on_SendInfo(OrderArray,8);
  68. RecString=on_Rec_PDUStatus();
  69. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_IN_L3_INFO);
  70. QThread::msleep(QUERYSTAMP);
  71. //查询相信息1
  72. OrderArray[2] = (_SWITCH_T_AC_CH_PHASE_INFO >> 8) & 0xFF; // High byte
  73. OrderArray[3] = _SWITCH_T_AC_CH_PHASE_INFO & 0xFF; // Low byte
  74. OrderArray[4]=0x00;
  75. OrderArray[5]=0x96;
  76. crc = crc16_ccitt(OrderArray, 6);
  77. OrderArray[6] = crc & 0xFF; // Low byte
  78. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  79. SendString=on_SendInfo(OrderArray,8);
  80. RecString=on_Rec_PDUStatus();
  81. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_CH_PHASE_INFO);
  82. QThread::msleep(QUERYSTAMP);
  83. //查询相信息2
  84. OrderArray[2] = ((_SWITCH_T_AC_CH_PHASE_INFO + _SWITCH_CH_DIV)>> 8) & 0xFF; // High byte
  85. OrderArray[3] = (_SWITCH_T_AC_CH_PHASE_INFO + _SWITCH_CH_DIV) & 0xFF; // Low byte
  86. OrderArray[4]=0x00;
  87. OrderArray[5]=0x96;
  88. crc = crc16_ccitt(OrderArray, 6);
  89. OrderArray[6] = crc & 0xFF; // Low byte
  90. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  91. SendString=on_SendInfo(OrderArray,8);
  92. RecString=on_Rec_PDUStatus();
  93. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_CH_PHASE_INFO+_SWITCH_CH_DIV);
  94. QThread::msleep(QUERYSTAMP);
  95. //查询相信息3
  96. OrderArray[2] = ((_SWITCH_T_AC_CH_PHASE_INFO + _SWITCH_CH_DIV*2 )>> 8) & 0xFF; // High byte
  97. OrderArray[3] = (_SWITCH_T_AC_CH_PHASE_INFO + _SWITCH_CH_DIV*2 ) & 0xFF; // Low byte
  98. OrderArray[4]=0x00;
  99. OrderArray[5]=0x96;
  100. crc = crc16_ccitt(OrderArray, 6);
  101. OrderArray[6] = crc & 0xFF; // Low byte
  102. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  103. SendString=on_SendInfo(OrderArray,8);
  104. RecString=on_Rec_PDUStatus();
  105. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_CH_PHASE_INFO+_SWITCH_CH_DIV*2);
  106. QThread::msleep(QUERYSTAMP);
  107. //查询通道状态
  108. OrderArray[2] = (_SWITCH_T_AC_CH_STATUS >> 8) & 0xFF; // High byte
  109. OrderArray[3] = _SWITCH_T_AC_CH_STATUS & 0xFF; // Low byte
  110. OrderArray[4]=0x00;
  111. OrderArray[5]=0x0E;
  112. crc = crc16_ccitt(OrderArray, 6);
  113. OrderArray[6] = crc & 0xFF; // Low byte
  114. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  115. SendString=on_SendInfo(OrderArray,8);
  116. RecString=on_Rec_PDUStatus();
  117. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_CH_STATUS);
  118. QThread::msleep(QUERYSTAMP);
  119. //查询modbus传感器
  120. OrderArray[2] = (_SWITCH_T_AC_SENSOR_INFO >> 8) & 0xFF; // High byte
  121. OrderArray[3] = _SWITCH_T_AC_SENSOR_INFO & 0xFF; // Low byte
  122. OrderArray[4]=0x00;
  123. OrderArray[5]=0x10;
  124. crc = crc16_ccitt(OrderArray, 6);
  125. OrderArray[6] = crc & 0xFF; // Low byte
  126. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  127. SendString=on_SendInfo(OrderArray,8);
  128. RecString=on_Rec_PDUStatus();
  129. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_SENSOR_INFO);
  130. QThread::msleep(QUERYSTAMP);
  131. //查询干节点传感器
  132. OrderArray[2] =(_SWITCH_T_AC_SENSOR_INFO1 >> 8) & 0xFF; // High byte
  133. OrderArray[3] = _SWITCH_T_AC_SENSOR_INFO1 & 0xFF; // Low byte
  134. OrderArray[4]=0x00;
  135. OrderArray[5]=0x04;
  136. crc = crc16_ccitt(OrderArray, 6);
  137. OrderArray[6] = crc & 0xFF; // Low byte
  138. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  139. SendString=on_SendInfo(OrderArray,8);
  140. RecString=on_Rec_PDUStatus();
  141. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_SENSOR_INFO1);
  142. QThread::msleep(QUERYSTAMP);
  143. }
  144. void SerialInfo::on_Parse_PDUStatus(const QString &Sendstr,const QString &Recstr,uint16_t infoType)
  145. {
  146. if(Recstr=="") return;
  147. if(_SWITCH_T_AC_TYPE_CHN_INFO==infoType)
  148. {
  149. QVector<quint16> pRecVector=parseModbusResponse(Recstr,1);
  150. if(pRecVector.size()<1) return;
  151. m_TotalCHTemp = pRecVector[0] & 0xFF; // Low byte
  152. m_PDUType = (pRecVector[0] >> 8) & 0xFF; // High byte
  153. }
  154. else if(_SWITCH_T_AC_IN_L1_INFO==infoType)
  155. {
  156. QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
  157. if(pRecVector.size()<8) return;
  158. m_PDUAllInfo.AllVoltage_L1 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
  159. m_PDUAllInfo.AllCurrent_L1 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
  160. m_PDUAllInfo.AllPower_L1 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
  161. m_PDUAllInfo.AllConsumption_L1 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
  162. }
  163. else if(_SWITCH_T_AC_IN_L2_INFO==infoType)
  164. {
  165. QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
  166. if(pRecVector.size()<8) return;
  167. m_PDUAllInfo.AllVoltage_L2 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
  168. m_PDUAllInfo.AllCurrent_L2 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
  169. m_PDUAllInfo.AllPower_L2 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
  170. m_PDUAllInfo.AllConsumption_L2 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
  171. }
  172. else if(_SWITCH_T_AC_IN_L3_INFO==infoType)
  173. {
  174. QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
  175. if(pRecVector.size()<8) return;
  176. m_PDUAllInfo.AllVoltage_L3 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
  177. m_PDUAllInfo.AllCurrent_L3 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
  178. m_PDUAllInfo.AllPower_L3 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
  179. m_PDUAllInfo.AllConsumption_L3 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
  180. }
  181. else if(_SWITCH_T_AC_CH_PHASE_INFO==infoType)
  182. {
  183. QVector<quint16> pRecVector=parseModbusResponse(Recstr,150);
  184. if(pRecVector.size()<150) return;
  185. for(int i=0;i<5;i++)
  186. {
  187. bool L1flag=pRecVector[i*30+9] & 0xFFFF; // Low byte
  188. bool L2flag=pRecVector[i*30+19] & 0xFFFF; // Low byte
  189. bool L3flag=pRecVector[i*30+29] & 0xFFFF; // Low byte
  190. if(L1flag)
  191. {
  192. m_PDUCHInfo[i].CHVoltage=((pRecVector[1] << 16) + pRecVector[0] )/1000.0f;
  193. m_PDUCHInfo[i].CHCurrent=((pRecVector[3] << 16) + pRecVector[2] )/1000.0f;
  194. m_PDUCHInfo[i].CHPower=((pRecVector[5] << 16) + pRecVector[4] )/1000.0f;
  195. m_PDUCHInfo[i].CHConsumption=((pRecVector[7] << 16) + pRecVector[6] )/1000.0f;
  196. }
  197. else if(L2flag)
  198. {
  199. m_PDUCHInfo[i].CHVoltage=((pRecVector[11] << 16) + pRecVector[10] )/1000.0f;
  200. m_PDUCHInfo[i].CHCurrent=((pRecVector[13] << 16) + pRecVector[12] )/1000.0f;
  201. m_PDUCHInfo[i].CHPower=((pRecVector[15] << 16) + pRecVector[14] )/1000.0f;
  202. m_PDUCHInfo[i].CHConsumption=((pRecVector[17] << 16) + pRecVector[16] )/1000.0f;
  203. }
  204. else if(L3flag)
  205. {
  206. m_PDUCHInfo[i].CHVoltage=((pRecVector[21] << 16) + pRecVector[20] )/1000.0f;
  207. m_PDUCHInfo[i].CHCurrent=((pRecVector[23] << 16) + pRecVector[22] )/1000.0f;
  208. m_PDUCHInfo[i].CHPower=((pRecVector[25] << 16) + pRecVector[24] )/1000.0f;
  209. m_PDUCHInfo[i].CHConsumption=((pRecVector[27] << 16) + pRecVector[26] )/1000.0f;
  210. }
  211. }
  212. }
  213. else if((_SWITCH_T_AC_CH_PHASE_INFO+_SWITCH_CH_DIV)==infoType)
  214. {
  215. QVector<quint16> pRecVector=parseModbusResponse(Recstr,150);
  216. if(pRecVector.size()<150) return;
  217. for(int i=0;i<5;i++)
  218. {
  219. bool L1flag=pRecVector[i*30+9] & 0xFFFF; // Low byte
  220. bool L2flag=pRecVector[i*30+19] & 0xFFFF; // Low byte
  221. bool L3flag=pRecVector[i*30+29] & 0xFFFF; // Low byte
  222. if(L1flag)
  223. {
  224. m_PDUCHInfo[i+5].CHVoltage=((pRecVector[1] << 16) + pRecVector[0] )/1000.0f;
  225. m_PDUCHInfo[i+5].CHCurrent=((pRecVector[3] << 16) + pRecVector[2] )/1000.0f;
  226. m_PDUCHInfo[i+5].CHPower=((pRecVector[5] << 16) + pRecVector[4] )/1000.0f;
  227. m_PDUCHInfo[i+5].CHConsumption=((pRecVector[7] << 16) + pRecVector[6] )/1000.0f;
  228. }
  229. else if(L2flag)
  230. {
  231. m_PDUCHInfo[i+5].CHVoltage=((pRecVector[11] << 16) + pRecVector[10] )/1000.0f;
  232. m_PDUCHInfo[i+5].CHCurrent=((pRecVector[13] << 16) + pRecVector[12] )/1000.0f;
  233. m_PDUCHInfo[i+5].CHPower=((pRecVector[15] << 16) + pRecVector[14] )/1000.0f;
  234. m_PDUCHInfo[i+5].CHConsumption=((pRecVector[17] << 16) + pRecVector[16] )/1000.0f;
  235. }
  236. else if(L3flag)
  237. {
  238. m_PDUCHInfo[i+5].CHVoltage=((pRecVector[21] << 16) + pRecVector[20] )/1000.0f;
  239. m_PDUCHInfo[i+5].CHCurrent=((pRecVector[23] << 16) + pRecVector[22] )/1000.0f;
  240. m_PDUCHInfo[i+5].CHPower=((pRecVector[25] << 16) + pRecVector[24] )/1000.0f;
  241. m_PDUCHInfo[i+5].CHConsumption=((pRecVector[27] << 16) + pRecVector[26] )/1000.0f;
  242. }
  243. }
  244. }
  245. else if((_SWITCH_T_AC_CH_PHASE_INFO+_SWITCH_CH_DIV*2)==infoType)
  246. {
  247. QVector<quint16> pRecVector=parseModbusResponse(Recstr,150);
  248. if(pRecVector.size()<150) return;
  249. for(int i=0;i<5;i++)
  250. {
  251. bool L1flag=pRecVector[i*30+9] & 0xFFFF; // Low byte
  252. bool L2flag=pRecVector[i*30+19] & 0xFFFF; // Low byte
  253. bool L3flag=pRecVector[i*30+29] & 0xFFFF; // Low byte
  254. if(L1flag)
  255. {
  256. m_PDUCHInfo[i+10].CHVoltage=((pRecVector[1] << 16) + pRecVector[0] )/1000.0f;
  257. m_PDUCHInfo[i+10].CHCurrent=((pRecVector[3] << 16) + pRecVector[2] )/1000.0f;
  258. m_PDUCHInfo[i+10].CHPower=((pRecVector[5] << 16) + pRecVector[4] )/1000.0f;
  259. m_PDUCHInfo[i+10].CHConsumption=((pRecVector[7] << 16) + pRecVector[6] )/1000.0f;
  260. }
  261. else if(L2flag)
  262. {
  263. m_PDUCHInfo[i+10].CHVoltage=((pRecVector[11] << 16) + pRecVector[10] )/1000.0f;
  264. m_PDUCHInfo[i+10].CHCurrent=((pRecVector[13] << 16) + pRecVector[12] )/1000.0f;
  265. m_PDUCHInfo[i+10].CHPower=((pRecVector[15] << 16) + pRecVector[14] )/1000.0f;
  266. m_PDUCHInfo[i+10].CHConsumption=((pRecVector[17] << 16) + pRecVector[16] )/1000.0f;
  267. }
  268. else if(L3flag)
  269. {
  270. m_PDUCHInfo[i+10].CHVoltage=((pRecVector[21] << 16) + pRecVector[20] )/1000.0f;
  271. m_PDUCHInfo[i+10].CHCurrent=((pRecVector[23] << 16) + pRecVector[22] )/1000.0f;
  272. m_PDUCHInfo[i+10].CHPower=((pRecVector[25] << 16) + pRecVector[24] )/1000.0f;
  273. m_PDUCHInfo[i+10].CHConsumption=((pRecVector[27] << 16) + pRecVector[26] )/1000.0f;
  274. }
  275. }
  276. }
  277. else if(_SWITCH_T_AC_CH_STATUS==infoType)
  278. {
  279. QVector<quint16> pRecVector=parseModbusResponse(Recstr,14);
  280. if(pRecVector.size()<14) return;
  281. for(int i=0;i<14;i++)
  282. {
  283. m_PDUCHInfo[i].CHStatus=pRecVector[i] & 0xFFFF; // Low byte
  284. }
  285. }
  286. else if(_SWITCH_T_AC_SENSOR_INFO==infoType)
  287. {
  288. QVector<quint16> pRecVector=parseModbusResponse(Recstr,16);
  289. if(pRecVector.size()<16) return;
  290. m_PDUAllInfo.Sensor_Temperature = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f;
  291. m_PDUAllInfo.Sensor_Humidity = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f;
  292. m_PDUAllInfo.Sensor_CO2 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f;
  293. m_PDUAllInfo.Sensor_INTemperature = ((pRecVector[9] << 16)+ pRecVector[8])/1000.0f;
  294. m_PDUAllInfo.Sensor_OUTTemperature = ((pRecVector[13] << 16)+ pRecVector[12])/1000.0f;
  295. }
  296. else if(_SWITCH_T_AC_SENSOR_INFO1==infoType)
  297. {
  298. QVector<quint16> pRecVector=parseModbusResponse(Recstr,4);
  299. if(pRecVector.size()<4) return;
  300. m_PDUAllInfo.Sensor_WaterImmersion = pRecVector[0] & 0xFFFF; // Low byte
  301. }
  302. if(m_printflag) emit addPDUInfo(Sendstr, Recstr);
  303. }
  304. /*发送数据*/
  305. QString SerialInfo::on_SendInfo(char* OrderArray,int sendsize)
  306. {
  307. if(!m_openflag) return "";
  308. QString hexString="";
  309. // 创建一个JNI环境并调用Java静态方法
  310. QAndroidJniEnvironment env;
  311. jbyteArray byteArray = env->NewByteArray(sendsize);
  312. env->SetByteArrayRegion(byteArray, 0, sendsize, reinterpret_cast<const jbyte*>(OrderArray));
  313. QAndroidJniObject sendbacktring = QAndroidJniObject::callStaticObjectMethod(
  314. "usb/USBListActivity",
  315. "writeData",
  316. "([B)Ljava/lang/String;", // 方法签名:接收字节数组并返回字符串
  317. byteArray // 参数
  318. );
  319. QString recsendinfo=sendbacktring.toString();
  320. // 发送字节计数并显示
  321. if(recsendinfo!="")
  322. {
  323. QByteArray byteArray = recsendinfo.toLatin1(); // 将 QString 转换为 QByteArray,使用 UTF-8 编码
  324. for (unsigned char byte : byteArray) {
  325. // 使用 printf 格式化输出或 qDebug() 输出每个字节的十六进制表示
  326. hexString += QString("%1 ").arg(static_cast<unsigned char>(byte), 2, 16, QChar('0'));
  327. }
  328. }
  329. return hexString;
  330. }
  331. QString SerialInfo::on_Rec_PDUStatus()
  332. {
  333. if(!m_openflag) return "";
  334. // 如接收成功,会返回接收的字符串,失败返回空字符串
  335. QString Allrecinfo="";
  336. for (int i=0;i<10;i++)
  337. {
  338. QAndroidJniObject recbacktring = QAndroidJniObject::callStaticObjectMethod(
  339. "usb/USBListActivity",
  340. "readData",
  341. "()Ljava/lang/String;");
  342. QString recinfo=recbacktring.toString();
  343. // 发送字节计数并显示
  344. if(recinfo!="")
  345. {
  346. QString hexString;
  347. QByteArray byteArray = recinfo.toLatin1(); // 将 QString 转换为 QByteArray,使用 UTF-8 编码
  348. for (char byte : byteArray) {
  349. // 使用 printf 格式化输出或 qDebug() 输出每个字节的十六进制表示
  350. hexString += QString("%1 ").arg(static_cast<unsigned char>(byte), 2, 16, QChar('0'));
  351. }
  352. Allrecinfo+=hexString;
  353. }
  354. else
  355. {
  356. break;
  357. }
  358. }
  359. return Allrecinfo;
  360. }
  361. uint16_t SerialInfo::crc16_ccitt(char* data, size_t length) {
  362. uint16_t crc = 0xFFFF;
  363. uint16_t i, j;
  364. for (i = 0; i < length; i++) {
  365. crc ^= data[i];
  366. for (j = 0; j < 8; j++) {
  367. if (crc & 0x0001) {
  368. crc >>= 1;
  369. crc ^= 0xA001;
  370. } else {
  371. crc >>= 1;
  372. }
  373. }
  374. }
  375. // data[7] = crc & 0xFF; // Low byte
  376. //data[6] = (crc >> 8) & 0xFF; // High byte
  377. return crc; // No final XOR value for CRC16-CCITT (XMODEM)
  378. }
  379. QVector<quint16> SerialInfo::parseModbusResponse(const QString &hexString, int registerCount) {
  380. QVector<quint16> registers;
  381. QString TempHexString=hexString;
  382. // Remove spaces from the input string for easier parsing
  383. QString cleanHexString = TempHexString.remove(QRegularExpression("\\s"));
  384. // Verify the length of the cleaned string matches what we expect
  385. if (cleanHexString.length() != (registerCount * 4 + 5 * 2)) { // +5*2 for the modubus 03 code head 3 byte +tail 2 byte = 5 byte
  386. qWarning() << "Invalid response length.";
  387. return registers; // Return empty vector on error
  388. }
  389. // Skip the first byte which is the byte count in a Modbus TCP response
  390. QTextStream stream(&cleanHexString);
  391. stream.skipWhiteSpace();
  392. stream.seek(6); // Skip the head byte count
  393. for (int i = 0; i < registerCount; ++i) {
  394. QString byteStr = stream.read(4); // Each register is represented by 4 hex chars (2 bytes)
  395. if (byteStr.length() != 4) {
  396. qWarning() << "Unexpected end of data at register" << i;
  397. break;
  398. }
  399. // Convert the 4 hex chars into a quint16 value
  400. bool ok1, ok2;
  401. quint8 byte1 = byteStr.left(2).toInt(&ok1, 16); // 前两位转换为整数
  402. quint8 byte2 = byteStr.right(2).toInt(&ok2, 16); // 后两位转换为整数
  403. quint16 regValue = (byte1 << 8) | byte2;
  404. registers.append(regValue);
  405. }
  406. return registers;
  407. }