123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #include <qsettings.h>
- #include <qfiledialog.h>
- #include <qtextstream.h>
- #include <qfile.h>
- #include <QTextCodec>
- #include <QCryptographicHash>
- #include "data_frame.h"
- char ConvertHexChar(char c)
- {
- if((c >= '0') && (c <= '9'))
- return c - 0x30;
- else if((c >= 'A') && (c <= 'F'))
- return c - 'A' + 10;//'A' = 65;
- else if((c >= 'a') && (c <= 'f'))
- return c - 'a' + 10;
- else
- return -1;
- }
- void frame_decode_jtt808_msg(QString in,QString *out)
- {
- QString f3_in = in.mid(2,in.length()-4);
- frame_decode_jtt808(f3_in,out);
- }
- void frame_decode_jtt808(QString in,QString *out)
- {
- //in.toLower();
- QByteArray bytes = in.toLatin1();
- MsgHead_txt_2013 head;
- memcpy( &head, bytes.data(), sizeof(head) );
- QString tmp ;
- out->append("\r\n数据帧头:\r\n");
- out->append("消息ID:");
- tmp = QString(QLatin1String((char *)head.msg_id));
- out->append(tmp.mid(0,sizeof(head.msg_id)));
- out->append("\r\n");
- out->append("消息体属性:");
- tmp = QString(QLatin1String((char *)head.attr));
- out->append(tmp.mid(0,sizeof(head.attr)));
- out->append("\r\n");
- out->append("终端手机号:");
- tmp = QString(QLatin1String((char *)head.phone));
- out->append(tmp.mid(0,sizeof(head.phone)));
- out->append("\r\n");
- out->append("消息流水号:");
- tmp = QString(QLatin1String((char *)head.sn));
- out->append(tmp.mid(0,sizeof(head.sn)));
- out->append("\r\n");
- QByteArray msg_data;
- QByteArray msg_crc;
- msg_crc = bytes.right(2);
- uint32_t head_len = sizeof(head);
- if(1)
- {
- head_len = head_len - 8;
- }
- msg_data = bytes.mid(head_len,bytes.length() - head_len - 2);
- out->append("消息体:");
- tmp = QString(msg_data);
- out->append(tmp);
- out->append("\r\n");
- out->append("校验值:");
- tmp = QString(msg_crc);
- out->append(tmp);
- out->append("\r\n");
- }
- void frame_decode_T2C(QString in,QString *out)
- {
- QByteArray bytes = in.toLatin1();
- T2C_Header_txt head;
- memcpy( &head, bytes.data(), sizeof(head) );
- QString tmp ;
- out->append("\r\n远程标定数据帧\r\n数据帧头:\r\n");
- out->append("帧头:");
- tmp = QString(QLatin1String((char *)head.frameflag));
- out->append(tmp.mid(0,sizeof(head.frameflag)));
- out->append("\r\n");
- out->append("扩展地址:");
- tmp = QString(QLatin1String((char *)head.deviceaddr));
- out->append(tmp.mid(0,sizeof(head.deviceaddr)));
- out->append("\r\n");
- out->append("读写功能码:");
- tmp = QString(QLatin1String((char *)head.functioncode));
- out->append(tmp.mid(0,sizeof(head.functioncode)));
- out->append("\r\n");
- out->append("命令码(寄存器地址):");
- tmp = QString(QLatin1String((char *)head.cmdcode));
- out->append(tmp.mid(0,sizeof(head.cmdcode)));
- out->append("\r\n");
- out->append("数据长度:");
- tmp = QString(QLatin1String((char *)head.datalength));
- out->append(tmp.mid(0,sizeof(head.datalength)));
- out->append("\r\n");
- out->append("保留字4字节:");
- tmp = QString(QLatin1String((char *)head.reserved));
- out->append(tmp.mid(0,sizeof(head.reserved)));
- out->append("\r\n");
- QByteArray msg_data;
- QByteArray msg_crc;
- msg_crc = bytes.right(4);
- msg_data = bytes.mid(sizeof(head),bytes.length() - sizeof(head) - msg_crc.length());
- out->append("消息体:");
- tmp = QString(msg_data);
- out->append(tmp);
- out->append("\r\n");
- out->append("校验值:");
- tmp = QString(msg_crc);
- out->append(tmp);
- out->append("\r\n");
- }
- //输入ASCII码,转换为数值
- void frame_decode_ascii(QString in,QString *out)
- {
- out->append("\r\nASCII码数据帧\r\n终端《========》控制器\r\n");
- in = in.mid(2,in.length() - 4 - 2);
- QByteArray bytes = in.toLatin1();
- bytes =bytes.fromHex(bytes);
- //QByteArray bytes_out;
- out->append(bytes);
- }
- void frame_decode_ascii_old(QString in,QString *out)
- {
- out->append("\r\nASCII码数据帧\r\n终端《========》控制器\r\n");
- in = in.mid(2,in.length() - 4 - 2);
- QByteArray bytes = in.toLatin1();
- QByteArray bytes_out ;
- QString data;
- int i = 0;
- for(i = 0; i < in.length() ; i++)
- {
- bytes_out.append( ConvertHexChar(bytes.at(i)));
- }
- uint8_t hex_h = 0;
- uint8_t hex_l = 0;
- char hex[512] = {0};
- QString tmp ;
- for(i = 0; i < in.length()/2; i++)
- {
- hex_h = bytes_out[2*i];
- hex_l = bytes_out[2*i+1];
- hex[i] = (hex_h&0x0f)*16 + (hex_l&0x0f);
- }
- out->append(hex);
- //out->append(bytes_out);
- }
- void frame_to_ascii(QString in,QString *out)
- {
- out->append("\r\nASCII转换为HEX\r\n");
- QByteArray bytes = in.toLatin1();
- //QByteArray bytes_out ;
- QByteArray bytes_hex ;
- // bytes_hex = bytes.toHex();
- //toHex转换为字符串输出
- //out->append(bytes_hex);
- bytes_hex = bytes.fromHex(bytes);
- //bytes_out = bytes_hex.fromHex(bytes_hex);
- out->append(bytes_hex);
- }
- void frame_to_ascii_old(QString in,QString *out)
- {
- out->append("\r\nASCII转换为HEX\r\n");
- QByteArray bytes = in.toLatin1();
- QByteArray bytes_out ;
- QString data;
- int i = 0;
- for(i = 0; i < in.length() ; i++)
- {
- bytes_out.append( ConvertHexChar(bytes.at(i)));
- }
- uint8_t hex_h = 0;
- uint8_t hex_l = 0;
- char hex[512] = {0};
- QString tmp ;
- for(i = 0; i < in.length()/2; i++)
- {
- hex_h = bytes_out[2*i];
- hex_l = bytes_out[2*i+1];
- hex[i] = (hex_h&0x0f)*16 + (hex_l&0x0f);
- }
- out->append(hex);
- //out->append(bytes_out);
- }
- void frame_decode_recognize(QString in,QString *out)
- {
- in = in.toLower();
- if((in.indexOf("3901") == 0)||
- (in.indexOf("3801") == 0)||
- (in.indexOf("4701") == 0))
- {
- frame_decode_T2C(in,out);
- }
- else if( (in.indexOf("3a") == 0) &&
- (in.indexOf("0d0a") == (in.length() - 4))
- )
- {
- frame_decode_ascii(in,out);
- }
- else if( (in.indexOf("7e") == 0) &&
- (in.lastIndexOf("7e") == (in.length() - 2))
- )
- {
- frame_decode_jtt808_msg(in,out);
- }
- else
- {
- //frame_decode_jtt808(in,out);
- }
- }
|