data_frame.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include <qsettings.h>
  2. #include <qfiledialog.h>
  3. #include <qtextstream.h>
  4. #include <qfile.h>
  5. #include <QTextCodec>
  6. #include <QCryptographicHash>
  7. #include "data_frame.h"
  8. char ConvertHexChar(char c)
  9. {
  10. if((c >= '0') && (c <= '9'))
  11. return c - 0x30;
  12. else if((c >= 'A') && (c <= 'F'))
  13. return c - 'A' + 10;//'A' = 65;
  14. else if((c >= 'a') && (c <= 'f'))
  15. return c - 'a' + 10;
  16. else
  17. return -1;
  18. }
  19. void frame_decode_jtt808_msg(QString in,QString *out)
  20. {
  21. QString f3_in = in.mid(2,in.length()-4);
  22. frame_decode_jtt808(f3_in,out);
  23. }
  24. void frame_decode_jtt808(QString in,QString *out)
  25. {
  26. //in.toLower();
  27. QByteArray bytes = in.toLatin1();
  28. MsgHead_txt_2013 head;
  29. memcpy( &head, bytes.data(), sizeof(head) );
  30. QString tmp ;
  31. out->append("\r\n数据帧头:\r\n");
  32. out->append("消息ID:");
  33. tmp = QString(QLatin1String((char *)head.msg_id));
  34. out->append(tmp.mid(0,sizeof(head.msg_id)));
  35. out->append("\r\n");
  36. out->append("消息体属性:");
  37. tmp = QString(QLatin1String((char *)head.attr));
  38. out->append(tmp.mid(0,sizeof(head.attr)));
  39. out->append("\r\n");
  40. out->append("终端手机号:");
  41. tmp = QString(QLatin1String((char *)head.phone));
  42. out->append(tmp.mid(0,sizeof(head.phone)));
  43. out->append("\r\n");
  44. out->append("消息流水号:");
  45. tmp = QString(QLatin1String((char *)head.sn));
  46. out->append(tmp.mid(0,sizeof(head.sn)));
  47. out->append("\r\n");
  48. QByteArray msg_data;
  49. QByteArray msg_crc;
  50. msg_crc = bytes.right(2);
  51. uint32_t head_len = sizeof(head);
  52. if(1)
  53. {
  54. head_len = head_len - 8;
  55. }
  56. msg_data = bytes.mid(head_len,bytes.length() - head_len - 2);
  57. out->append("消息体:");
  58. tmp = QString(msg_data);
  59. out->append(tmp);
  60. out->append("\r\n");
  61. out->append("校验值:");
  62. tmp = QString(msg_crc);
  63. out->append(tmp);
  64. out->append("\r\n");
  65. }
  66. void frame_decode_T2C(QString in,QString *out)
  67. {
  68. QByteArray bytes = in.toLatin1();
  69. T2C_Header_txt head;
  70. memcpy( &head, bytes.data(), sizeof(head) );
  71. QString tmp ;
  72. out->append("\r\n远程标定数据帧\r\n数据帧头:\r\n");
  73. out->append("帧头:");
  74. tmp = QString(QLatin1String((char *)head.frameflag));
  75. out->append(tmp.mid(0,sizeof(head.frameflag)));
  76. out->append("\r\n");
  77. out->append("扩展地址:");
  78. tmp = QString(QLatin1String((char *)head.deviceaddr));
  79. out->append(tmp.mid(0,sizeof(head.deviceaddr)));
  80. out->append("\r\n");
  81. out->append("读写功能码:");
  82. tmp = QString(QLatin1String((char *)head.functioncode));
  83. out->append(tmp.mid(0,sizeof(head.functioncode)));
  84. out->append("\r\n");
  85. out->append("命令码(寄存器地址):");
  86. tmp = QString(QLatin1String((char *)head.cmdcode));
  87. out->append(tmp.mid(0,sizeof(head.cmdcode)));
  88. out->append("\r\n");
  89. out->append("数据长度:");
  90. tmp = QString(QLatin1String((char *)head.datalength));
  91. out->append(tmp.mid(0,sizeof(head.datalength)));
  92. out->append("\r\n");
  93. out->append("保留字4字节:");
  94. tmp = QString(QLatin1String((char *)head.reserved));
  95. out->append(tmp.mid(0,sizeof(head.reserved)));
  96. out->append("\r\n");
  97. QByteArray msg_data;
  98. QByteArray msg_crc;
  99. msg_crc = bytes.right(4);
  100. msg_data = bytes.mid(sizeof(head),bytes.length() - sizeof(head) - msg_crc.length());
  101. out->append("消息体:");
  102. tmp = QString(msg_data);
  103. out->append(tmp);
  104. out->append("\r\n");
  105. out->append("校验值:");
  106. tmp = QString(msg_crc);
  107. out->append(tmp);
  108. out->append("\r\n");
  109. }
  110. //输入ASCII码,转换为数值
  111. void frame_decode_ascii(QString in,QString *out)
  112. {
  113. out->append("\r\nASCII码数据帧\r\n终端《========》控制器\r\n");
  114. in = in.mid(2,in.length() - 4 - 2);
  115. QByteArray bytes = in.toLatin1();
  116. bytes =bytes.fromHex(bytes);
  117. //QByteArray bytes_out;
  118. out->append(bytes);
  119. }
  120. void frame_decode_ascii_old(QString in,QString *out)
  121. {
  122. out->append("\r\nASCII码数据帧\r\n终端《========》控制器\r\n");
  123. in = in.mid(2,in.length() - 4 - 2);
  124. QByteArray bytes = in.toLatin1();
  125. QByteArray bytes_out ;
  126. QString data;
  127. int i = 0;
  128. for(i = 0; i < in.length() ; i++)
  129. {
  130. bytes_out.append( ConvertHexChar(bytes.at(i)));
  131. }
  132. uint8_t hex_h = 0;
  133. uint8_t hex_l = 0;
  134. char hex[512] = {0};
  135. QString tmp ;
  136. for(i = 0; i < in.length()/2; i++)
  137. {
  138. hex_h = bytes_out[2*i];
  139. hex_l = bytes_out[2*i+1];
  140. hex[i] = (hex_h&0x0f)*16 + (hex_l&0x0f);
  141. }
  142. out->append(hex);
  143. //out->append(bytes_out);
  144. }
  145. void frame_to_ascii(QString in,QString *out)
  146. {
  147. out->append("\r\nASCII转换为HEX\r\n");
  148. QByteArray bytes = in.toLatin1();
  149. //QByteArray bytes_out ;
  150. QByteArray bytes_hex ;
  151. // bytes_hex = bytes.toHex();
  152. //toHex转换为字符串输出
  153. //out->append(bytes_hex);
  154. bytes_hex = bytes.fromHex(bytes);
  155. //bytes_out = bytes_hex.fromHex(bytes_hex);
  156. out->append(bytes_hex);
  157. }
  158. void frame_to_ascii_old(QString in,QString *out)
  159. {
  160. out->append("\r\nASCII转换为HEX\r\n");
  161. QByteArray bytes = in.toLatin1();
  162. QByteArray bytes_out ;
  163. QString data;
  164. int i = 0;
  165. for(i = 0; i < in.length() ; i++)
  166. {
  167. bytes_out.append( ConvertHexChar(bytes.at(i)));
  168. }
  169. uint8_t hex_h = 0;
  170. uint8_t hex_l = 0;
  171. char hex[512] = {0};
  172. QString tmp ;
  173. for(i = 0; i < in.length()/2; i++)
  174. {
  175. hex_h = bytes_out[2*i];
  176. hex_l = bytes_out[2*i+1];
  177. hex[i] = (hex_h&0x0f)*16 + (hex_l&0x0f);
  178. }
  179. out->append(hex);
  180. //out->append(bytes_out);
  181. }
  182. void frame_decode_recognize(QString in,QString *out)
  183. {
  184. in = in.toLower();
  185. if((in.indexOf("3901") == 0)||
  186. (in.indexOf("3801") == 0)||
  187. (in.indexOf("4701") == 0))
  188. {
  189. frame_decode_T2C(in,out);
  190. }
  191. else if( (in.indexOf("3a") == 0) &&
  192. (in.indexOf("0d0a") == (in.length() - 4))
  193. )
  194. {
  195. frame_decode_ascii(in,out);
  196. }
  197. else if( (in.indexOf("7e") == 0) &&
  198. (in.lastIndexOf("7e") == (in.length() - 2))
  199. )
  200. {
  201. frame_decode_jtt808_msg(in,out);
  202. }
  203. else
  204. {
  205. //frame_decode_jtt808(in,out);
  206. }
  207. }