-
Notifications
You must be signed in to change notification settings - Fork 15
/
SCStatusTcp.cpp
361 lines (331 loc) · 12.4 KB
/
SCStatusTcp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "SCStatusTcp.h"
#include "SCHeadData.h"
#include <QDateTime>
#include <QNetworkProxy>
SCStatusTcp::SCStatusTcp(QObject *parent) : QObject(parent),
_tcpSocket(Q_NULLPTR)
{
}
SCStatusTcp::~SCStatusTcp()
{
releaseTcpSocket();
if(_tcpSocket){
delete _tcpSocket;
}
}
/** 释放tcpSocket
* @brief SCStatusTcp::releaseTcpSocket
*/
void SCStatusTcp::releaseTcpSocket()
{
if(_tcpSocket){
if(_tcpSocket->isOpen()){
_tcpSocket->close();
}
_tcpSocket->abort();
}
}
/** 连接
* @brief SCStatusTcp::connectHost
* @param ip
* @param port
* @return
*/
int SCStatusTcp::connectHost(const QString&ip,quint16 port)
{
int ret = 0;
if(!_tcpSocket){
_tcpSocket = new QTcpSocket(this);
//无代理,不加这行会导致开启 vpn 无法连接局域网设备.
_tcpSocket->setProxy(QNetworkProxy::NoProxy);
connect(_tcpSocket, SIGNAL(readyRead()), this, SLOT(receiveTcpReadyRead()));
connect(_tcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this->parent(), SLOT(stateChanged(QAbstractSocket::SocketState)));
connect(_tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this->parent(),
SLOT(receiveTcpError(QAbstractSocket::SocketError)));
}
if(_tcpSocket->isOpen()
&&(_tcpSocket->state()==QAbstractSocket::ConnectedState
||_tcpSocket->state()==QAbstractSocket::ConnectingState)){
_tcpSocket->close();
_tcpSocket->abort();
qDebug()<<"----close _tcpSocket----\n";
ret = 1;
}else{
_tcpSocket->connectToHost(ip,port,QTcpSocket::ReadWrite,QTcpSocket::IPv4Protocol);
_ip = ip;
_port = port;
ret = 0;
}
//清空缓冲区
if(_tcpSocket->isOpen())
_tcpSocket->readAll();
return ret;
}
/** TCP请求
* @brief SCStatusTcp::writeTcpData
* @param sendCommand 报文类型.
* @param sendData 数据区数据.
* @param jsonData Json区数据.
* @param number 序号.
* @return
*/
bool SCStatusTcp::writeTcpData(uint16_t sendCommand,
const QByteArray &jsonData,
const QByteArray &sendData,
uint16_t &number,
uint8_t byte15)
{
//已发送.
_oldSendCommand = sendCommand;
_oldNumber = number;
//数据区长度.
int size = 0;
//报文头部数据.
uint8_t* headBuf = Q_NULLPTR;
int headSize = 0;
//发送的全部数据.
SeerData* seerData = Q_NULLPTR;
//开始计时.
_time.start();
//json数据
uint16_t jsonSize = 0;
if(!jsonData.isEmpty()){
jsonSize = jsonData.toStdString().length();
}
QByteArray totalData = jsonData + sendData;
//根据数据区数据进行数据转换.
if(totalData.isEmpty()){
headSize = sizeof(SeerHeader);
headBuf = new uint8_t[headSize];
seerData = (SeerData*)headBuf;
size = seerData->setData(sendCommand,Q_NULLPTR,0,0,number,byte15);
}else{
std::string totalDataStr = totalData.toStdString();
headSize = sizeof(SeerHeader) + totalDataStr.length();
headBuf = new uint8_t[headSize];
seerData = (SeerData*)headBuf;
size = seerData->setData(sendCommand,
(uint8_t*)totalDataStr.data(),
totalDataStr.length(),
jsonSize,
number,byte15);
}
//---------------------------------------
//发送的所有数据.
QByteArray tempA = QByteArray::fromRawData((char*)seerData,size);
qDebug()<<"send:"<<QString(tempA)<<" Hex:"<<tempA.toHex()<<"seerData:size:"<<size;
QString dataHex = "";
if(size<=2048){
dataHex = hexToQString(sendData.toHex());
}else{
dataHex = tr("If the data region length is larger than 2048 bytes, it will not be printed");
}
//打印信息.
QString info = tr("\n%1---------[Request] ---------\n"
"Type:%2 (0x%3) \n"
"Port: %4\n"
"Number: %5 (0x%6)\n"
"Head hex: %7 \n"
"Data[size:%8 (0x%9)]: %10 \n"
"Data hex: %11 \n"
"JSON[size:%12]: %13")
.arg(getCurrentDateTime())
.arg(sendCommand)
.arg(QString::number(sendCommand,16))
.arg(_port)
.arg(number)
.arg(QString::number(number,16))
.arg(hexToQString(tempA.left(16).toHex()))
.arg(sendData.size())
.arg(QString::number(sendData.size(),16))
.arg(QString(sendData))
.arg(dataHex)
.arg(jsonSize)
.arg(QString(jsonData));
emit sigPrintInfo(info);
//---------------------------------------
_tcpSocket->write((char*)seerData, size);
delete[] headBuf;
//-------------
qDebug()<<"TCP:_timeOut:"<<_timeOut;
//如果_timeOut = 0表示不监听超时.
if(0 == _timeOut){
return true;
}
//等待写入.
if(!_tcpSocket->waitForBytesWritten(_timeOut)){
_lastError = tr("waitForBytesWritten: time out!");
return false;
}
//等待读取.
if(!_tcpSocket->waitForReadyRead(_timeOut)){
_lastError = tr("waitForReadyRead: time out!");
return false;
}
return true;
}
void SCStatusTcp::receiveTcpReadyRead()
{
//读取所有数据.
//返回的数据大小不定,需要使用_lastMessage成员变量存放多次触发槽读取的数据.
QByteArray message = _tcpSocket->readAll();
message = _lastMessage + message;
int size = message.size();
while(size > 0){
char a0 = message.at(0);
if (uint8_t(a0) == 0x5A){//检测第一位是否为0x5A.
if (size >= 16) {//返回的数据最小长度为16位,如果大小小于16则数据不完整等待再次读取.
SeerHeader* header = new SeerHeader;
memcpy(header, message.data(), 16);
uint32_t data_size;//返回所有数据总长值.
uint16_t revCommand;//返回报文数据类型.
uint16_t number;//返回序号.
qToBigEndian(header->m_length,(uint8_t*)&(data_size));
qToBigEndian(header->m_type, (uint8_t*)&(revCommand));
qToBigEndian(header->m_number, (uint8_t*)&(number));
//json区长度.
uint16_t jsonSize = 0;
{
uint8_t u2 = header->m_reserved[2];
uint8_t u3 = header->m_reserved[3];
uint16_t tempJsonSize = u2;
tempJsonSize = tempJsonSize << 8;
tempJsonSize = tempJsonSize | u3;
if(tempJsonSize <0 || tempJsonSize > 65535){
tempJsonSize = 0;
}
jsonSize = tempJsonSize;
}
delete header;
uint32_t remaining_size = size - 16;//所有数据总长度 - 头部总长度16 = 数据区长度.
//如果返回数据长度值 大于 已读取数据长度 表示数据还未读取完整,跳出循环等待再次读取..
if (data_size > remaining_size) {
_lastMessage = message;
break;
}else{//返回数据长度值 大于等于 已读取数据,开始解析.
QByteArray tempMessage;
if(_lastMessage.isEmpty()){
tempMessage = message;
}else{
tempMessage = _lastMessage;
}
QByteArray headB = message.left(16);
QByteArray jsonData;
QByteArray byteData;
//截取报头16位后面的数据区数据.
{
QByteArray totalData = message.mid(16,data_size);
if(jsonSize > 0){
jsonData = totalData.mid(0,jsonSize);
qDebug()<<">>>>>>>>>>>>>>>>>>>>>>jsonSize:"<<jsonSize
<<"tempJsonData:"<<QString("[%1]").arg(QString(jsonData));
}
byteData = totalData.mid(jsonSize,data_size - jsonSize);
qDebug()<<"jsonData:"<<QString(jsonData)<<"revByteData:"<<QString(byteData)<<" Hex:"<<byteData.toHex();
}
//--------------------------------------
QString dataHex = "";
if(size<=2048){
dataHex = hexToQString(byteData.toHex());
}else{
dataHex = tr("If the data region length is larger than 2048 bytes, it will not be printed");
}
//输出打印信息.
QString info = QString("%1---------[Response]---------\n"
"Type:%2 (%3) \n"
"Number: %4 (0x%5)\n"
"Head hex: %6\n"
"Data[size:%7 (0x%8)]: %9 \n"
"Data hex: %10 \n"
"JSON[size:%11]: %12\n")
.arg(getCurrentDateTime())
.arg(revCommand)
.arg(QString::number(revCommand,16))
.arg(number)
.arg(QString::number(number,16))
.arg(hexToQString(headB.toHex()))
.arg(byteData.size())
.arg(QString::number(byteData.size(),16))
.arg(QString(byteData))
.arg(dataHex)
.arg(jsonData.size())
.arg(QString(jsonData));
emit sigPrintInfo(info);
int msTime = _time.elapsed();
//----------------------------------------
//输出返回信息.
emit sigChangedText(true,revCommand,
byteData,byteData.toHex(),
number,msTime);
//截断message,清空_lastMessage.
message = message.right(remaining_size - data_size);
size = message.size();
_lastMessage.clear();
}
} else{
_lastMessage = message;
break;
}
}else{
//报头数据错误.
setLastError("Header Error !!!");
message = message.right(size - 1);
size = message.size();
int msTime = _time.elapsed();
emit sigChangedText(false,_oldSendCommand,
_lastMessage,_lastMessage.toHex(),
0,msTime);
}
}
}
int SCStatusTcp::getTimeOut() const
{
return _timeOut;
}
void SCStatusTcp::setTimeOut(int timeOut)
{
_timeOut = timeOut;
}
QTcpSocket *SCStatusTcp::tcpSocket() const
{
return _tcpSocket;
}
QElapsedTimer SCStatusTcp::time() const
{
return _time;
}
void SCStatusTcp::setLastError(const QString &lastError)
{
_lastError = lastError;
}
QString SCStatusTcp::lastError() const
{
return _lastError;
}
/** 获取当前时间
* @brief SCStatusTcp::getCurrentDateTime
* @return
*/
QString SCStatusTcp::getCurrentDateTime()const
{
return QDateTime::currentDateTime().toString("[yyyyMMdd|hh:mm:ss:zzz]:");
}
/** 16进制全部显示大写
* @brief SCStatusTcp::hexToQString
* @param b
* @return
*/
QString SCStatusTcp::hexToQString(const QByteArray &b)
{
QString str;
for(int i=0;i<b.size();++i){
//// //每2位加入 空格0x
//// if((!(i%2)&&i/2)||0==i){
//// str+= QString(" 0x");
//// }
str +=QString("%1").arg(b.at(i));
}
str = str.toUpper();
return str;
}