serialinfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /*
  72. //查询相信息
  73. OrderArray[2] = (_SWITCH_T_AC_CH_PHASE_INFO >> 8) & 0xFF; // High byte
  74. OrderArray[3] = _SWITCH_T_AC_CH_PHASE_INFO & 0xFF; // Low byte
  75. OrderArray[4]=0x01;
  76. OrderArray[5]=0xA4;
  77. crc = crc16_ccitt(OrderArray, 6);
  78. OrderArray[6] = crc & 0xFF; // Low byte
  79. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  80. SendString=on_SendInfo(OrderArray,8);
  81. RecString=on_Rec_PDUStatus();
  82. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_CH_PHASE_INFO);
  83. QThread::msleep(QUERYSTAMP);
  84. */
  85. //查询通道状态
  86. OrderArray[2] = (_SWITCH_T_AC_CH_STATUS >> 8) & 0xFF; // High byte
  87. OrderArray[3] = _SWITCH_T_AC_CH_STATUS & 0xFF; // Low byte
  88. OrderArray[4]=0x00;
  89. OrderArray[5]=0x0E;
  90. crc = crc16_ccitt(OrderArray, 6);
  91. OrderArray[6] = crc & 0xFF; // Low byte
  92. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  93. SendString=on_SendInfo(OrderArray,8);
  94. RecString=on_Rec_PDUStatus();
  95. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_CH_STATUS);
  96. QThread::msleep(QUERYSTAMP);
  97. /*
  98. //查询modbus传感器
  99. OrderArray[2] = (_SWITCH_T_AC_SENSOR_INFO >> 8) & 0xFF; // High byte
  100. OrderArray[3] = _SWITCH_T_AC_SENSOR_INFO & 0xFF; // Low byte
  101. OrderArray[4]=0x00;
  102. OrderArray[5]=0x10;
  103. crc = crc16_ccitt(OrderArray, 6);
  104. OrderArray[6] = crc & 0xFF; // Low byte
  105. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  106. SendString=on_SendInfo(OrderArray,8);
  107. RecString=on_Rec_PDUStatus();
  108. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_SENSOR_INFO);
  109. QThread::msleep(QUERYSTAMP);
  110. //查询干节点传感器
  111. OrderArray[2] =(_SWITCH_T_AC_SENSOR_INFO1 >> 8) & 0xFF; // High byte
  112. OrderArray[3] = _SWITCH_T_AC_SENSOR_INFO1 & 0xFF; // Low byte
  113. OrderArray[4]=0x00;
  114. OrderArray[5]=0x04;
  115. crc = crc16_ccitt(OrderArray, 6);
  116. OrderArray[6] = crc & 0xFF; // Low byte
  117. OrderArray[7] = (crc >> 8) & 0xFF; // High byte
  118. SendString=on_SendInfo(OrderArray,8);
  119. RecString=on_Rec_PDUStatus();
  120. on_Parse_PDUStatus(SendString,RecString,_SWITCH_T_AC_SENSOR_INFO1);
  121. QThread::msleep(QUERYSTAMP);
  122. */
  123. }
  124. void SerialInfo::on_Parse_PDUStatus(const QString &Sendstr,const QString &Recstr,uint16_t infoType)
  125. {
  126. if(Recstr=="") return;
  127. if(_SWITCH_T_AC_TYPE_CHN_INFO==infoType)
  128. {
  129. QVector<quint16> pRecVector=parseModbusResponse(Recstr,1);
  130. if(pRecVector.size()<1) return;
  131. m_TotalCHTemp = pRecVector[0] & 0xFF; // Low byte
  132. m_PDUType = (pRecVector[0] >> 8) & 0xFF; // High byte
  133. }
  134. else if(_SWITCH_T_AC_IN_L1_INFO==infoType)
  135. {
  136. QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
  137. if(pRecVector.size()<8) return;
  138. m_PDUAllInfo.AllVoltage_L1 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
  139. m_PDUAllInfo.AllCurrent_L1 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
  140. m_PDUAllInfo.AllPower_L1 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
  141. m_PDUAllInfo.AllConsumption_L1 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
  142. }
  143. else if(_SWITCH_T_AC_IN_L2_INFO==infoType)
  144. {
  145. QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
  146. if(pRecVector.size()<8) return;
  147. m_PDUAllInfo.AllVoltage_L2 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
  148. m_PDUAllInfo.AllCurrent_L2 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
  149. m_PDUAllInfo.AllPower_L2 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
  150. m_PDUAllInfo.AllConsumption_L2 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
  151. }
  152. else if(_SWITCH_T_AC_IN_L3_INFO==infoType)
  153. {
  154. QVector<quint16> pRecVector=parseModbusResponse(Recstr,8);
  155. if(pRecVector.size()<8) return;
  156. m_PDUAllInfo.AllVoltage_L3 = ((pRecVector[1] << 16) + pRecVector[0] )/1000.0f; // Low byte
  157. m_PDUAllInfo.AllCurrent_L3 = ((pRecVector[3] << 16) + pRecVector[2] )/1000.0f; // Low byte
  158. m_PDUAllInfo.AllPower_L3 = ((pRecVector[5] << 16) + pRecVector[4] )/1000.0f; // Low byte
  159. m_PDUAllInfo.AllConsumption_L3 = ((pRecVector[7] << 16)+ pRecVector[6])/1000.0f; // Low byte
  160. }
  161. else if(_SWITCH_T_AC_CH_PHASE_INFO==infoType)
  162. {
  163. }
  164. else if(_SWITCH_T_AC_CH_STATUS==infoType)
  165. {
  166. QVector<quint16> pRecVector=parseModbusResponse(Recstr,14);
  167. if(pRecVector.size()<14) return;
  168. for(int i=0;i<14;i++)
  169. {
  170. m_PDUCHInfo[i].CHStatus=pRecVector[i] & 0xFFFF; // Low byte
  171. }
  172. }
  173. else if(_SWITCH_T_AC_SENSOR_INFO==infoType)
  174. {
  175. }
  176. else if(_SWITCH_T_AC_SENSOR_INFO1==infoType)
  177. {
  178. }
  179. if(m_printflag) emit addPDUInfo(Sendstr, Recstr);
  180. }
  181. /*发送数据*/
  182. QString SerialInfo::on_SendInfo(char* OrderArray,int sendsize)
  183. {
  184. if(!m_openflag) return "";
  185. QString hexString="";
  186. // 创建一个JNI环境并调用Java静态方法
  187. QAndroidJniEnvironment env;
  188. jbyteArray byteArray = env->NewByteArray(sendsize);
  189. env->SetByteArrayRegion(byteArray, 0, sendsize, reinterpret_cast<const jbyte*>(OrderArray));
  190. QAndroidJniObject sendbacktring = QAndroidJniObject::callStaticObjectMethod(
  191. "usb/USBListActivity",
  192. "writeData",
  193. "([B)Ljava/lang/String;", // 方法签名:接收字节数组并返回字符串
  194. byteArray // 参数
  195. );
  196. QString recsendinfo=sendbacktring.toString();
  197. // 发送字节计数并显示
  198. if(recsendinfo!="")
  199. {
  200. QByteArray byteArray = recsendinfo.toLatin1(); // 将 QString 转换为 QByteArray,使用 UTF-8 编码
  201. for (unsigned char byte : byteArray) {
  202. // 使用 printf 格式化输出或 qDebug() 输出每个字节的十六进制表示
  203. hexString += QString("%1 ").arg(static_cast<unsigned char>(byte), 2, 16, QChar('0'));
  204. }
  205. }
  206. return hexString;
  207. }
  208. QString SerialInfo::on_Rec_PDUStatus()
  209. {
  210. if(!m_openflag) return "";
  211. // 如接收成功,会返回接收的字符串,失败返回空字符串
  212. QString Allrecinfo="";
  213. for (int i=0;i<10;i++)
  214. {
  215. QAndroidJniObject recbacktring = QAndroidJniObject::callStaticObjectMethod(
  216. "usb/USBListActivity",
  217. "readData",
  218. "()Ljava/lang/String;");
  219. QString recinfo=recbacktring.toString();
  220. // 发送字节计数并显示
  221. if(recinfo!="")
  222. {
  223. QString hexString;
  224. QByteArray byteArray = recinfo.toLatin1(); // 将 QString 转换为 QByteArray,使用 UTF-8 编码
  225. for (char byte : byteArray) {
  226. // 使用 printf 格式化输出或 qDebug() 输出每个字节的十六进制表示
  227. hexString += QString("%1 ").arg(static_cast<unsigned char>(byte), 2, 16, QChar('0'));
  228. }
  229. Allrecinfo+=hexString;
  230. }
  231. else
  232. {
  233. break;
  234. }
  235. }
  236. return Allrecinfo;
  237. }
  238. uint16_t SerialInfo::crc16_ccitt(char* data, size_t length) {
  239. uint16_t crc = 0xFFFF;
  240. uint16_t i, j;
  241. for (i = 0; i < length; i++) {
  242. crc ^= data[i];
  243. for (j = 0; j < 8; j++) {
  244. if (crc & 0x0001) {
  245. crc >>= 1;
  246. crc ^= 0xA001;
  247. } else {
  248. crc >>= 1;
  249. }
  250. }
  251. }
  252. // data[7] = crc & 0xFF; // Low byte
  253. //data[6] = (crc >> 8) & 0xFF; // High byte
  254. return crc; // No final XOR value for CRC16-CCITT (XMODEM)
  255. }
  256. QVector<quint16> SerialInfo::parseModbusResponse(const QString &hexString, int registerCount) {
  257. QVector<quint16> registers;
  258. QString TempHexString=hexString;
  259. // Remove spaces from the input string for easier parsing
  260. QString cleanHexString = TempHexString.remove(QRegularExpression("\\s"));
  261. // Verify the length of the cleaned string matches what we expect
  262. if (cleanHexString.length() != (registerCount * 4 + 5 * 2)) { // +5*2 for the modubus 03 code head 3 byte +tail 2 byte = 5 byte
  263. qWarning() << "Invalid response length.";
  264. return registers; // Return empty vector on error
  265. }
  266. // Skip the first byte which is the byte count in a Modbus TCP response
  267. QTextStream stream(&cleanHexString);
  268. stream.skipWhiteSpace();
  269. stream.seek(6); // Skip the head byte count
  270. for (int i = 0; i < registerCount; ++i) {
  271. QString byteStr = stream.read(4); // Each register is represented by 4 hex chars (2 bytes)
  272. if (byteStr.length() != 4) {
  273. qWarning() << "Unexpected end of data at register" << i;
  274. break;
  275. }
  276. // Convert the 4 hex chars into a quint16 value
  277. bool ok1, ok2;
  278. quint8 byte1 = byteStr.left(2).toInt(&ok1, 16); // 前两位转换为整数
  279. quint8 byte2 = byteStr.right(2).toInt(&ok2, 16); // 后两位转换为整数
  280. quint16 regValue = (byte1 << 8) | byte2;
  281. registers.append(regValue);
  282. }
  283. return registers;
  284. }