-
Notifications
You must be signed in to change notification settings - Fork 0
/
innfosproxy.cpp
644 lines (602 loc) · 16.2 KB
/
innfosproxy.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#include "innfosproxy.h"
#include "mediator.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include "communication.h"
#include <QMutexLocker>
#include "autorecoginze.h"
#include "msgbox.h"
#include "innfoschartwidget.h"
//#define DEVICEID_127 //the max device id is 127
InnfosProxy::InnfosProxy(const int nId, const quint8 nDeviceId):
m_nId(nId),
m_pData(nullptr)
{
m_pData = new IData();
m_pData->AddProxyBegin(nId,nDeviceId);
}
InnfosProxy::~InnfosProxy()
{
if (m_pData)
{
delete m_pData;
}
}
void InnfosProxy::Send()
{
if (/*CanSend()*/true)
{
Mediator::getInstance()->SendRequest(m_pData->GetBuffer());
}
}
void InnfosProxy::decode(QByteArray &buf)
{
IData data(buf);
if (buf.size() < 6/* || !data.CheckData()*/)
{
return;
}
bool bRet = false;
quint8 nMode = buf.at(2);
quint8 nDeviceId = buf.at(1);
#ifdef DEVICEID_127
nDeviceId -= 128;
#endif
switch (nMode)
{
case D_READ_CUR_POSITION:
{
data.Skip(5);
qint32 nValue = data.ReadInt();
qreal real = nValue;
real = (real) / (1 << 24);
Mediator::getInstance()->SetCurParam(nDeviceId,real,nMode);
}
break;
case D_READ_CUR_CURRENT:
{
data.Skip(5);
qint32 nValue = data.ReadInt();
qreal real = nValue;
real = (real)*Mediator::getInstance()->currentScale(nDeviceId) / (1 << 24);
Mediator::getInstance()->SetCurParam(nDeviceId,real,nMode);
}
break;
case D_READ_CUR_VELOCITY:
case D_READ_CURRENT_MAX_VEL:
{
data.Skip(5);
qint32 nValue = data.ReadInt();
qreal real = nValue;
real = (real)*Mediator::getInstance()->velocityScale(nDeviceId) / (1 << 24);
Mediator::getInstance()->SetCurParam(nDeviceId,real,nMode);
}
break;
case D_READ_CHANNEL_1:
{
data.Skip(5);
qint32 nValue = data.ReadShort();
qreal real = nValue;
real = (real)/**curScale*/ / (1 << 15);
Mediator::getInstance()->chartVauleChange(InnfosChartWidget::channel_1,real);
}
break;
case D_READ_CHANNEL_2:
{
data.Skip(5);
qint32 nValue = data.ReadShort();
qreal real = nValue;
real = (real)/**curScale*/ / (1 << 15);
Mediator::getInstance()->chartVauleChange(InnfosChartWidget::channel_2,real);
}
break;
case D_READ_CHANNEL_3:
{
data.Skip(5);
qint32 nValue = data.ReadShort();
qreal real = nValue;
real = (real)/**velScale*/ / (1 << 15);
Mediator::getInstance()->chartVauleChange(InnfosChartWidget::channel_3,real);
}
break;
case D_READ_CHANNEL_4:
{
data.Skip(5);
qint32 nValue = data.ReadShort();
qreal real = nValue;
real = (real) / (1 << 15);
Mediator::getInstance()->chartVauleChange(InnfosChartWidget::channel_4,/*real*16*/real);
}
break;
case D_READ_CHART_FREQUENCY:
{
data.Skip(5);
qint32 nValue = data.ReadShort();
qreal real = nValue;
//real = (real) / (1 << 15);
Mediator::getInstance()->SetCurParam(nDeviceId,real,nMode);
}
break;
case D_READ_CHART_THRESHOLD:
{
data.Skip(5);
qint32 nValue = data.ReadShort();
qreal real = nValue;
real = (real) / (1 << 15);
Mediator::getInstance()->SetCurParam(nDeviceId,real,nMode);
}
break;
case D_READ_POS_P:
case D_READ_POS_I:
case D_READ_POS_D:
case D_READ_CUR_P:
case D_READ_CUR_I:
case D_READ_VEL_P:
case D_READ_VEL_I:
case D_READ_INERTIA:
{
data.Skip(5);
qint32 nValue = data.ReadInt();
qreal real = nValue;
real = (real) / (1 << 24);
Mediator::getInstance()->SetCurParam(nDeviceId,real,nMode);
}
break;
case D_READ_VOLTAGE:
{
data.Skip(5);
quint16 nValue = data.ReadUShort();
qreal real = nValue;
real = (real) / 1024;
Mediator::getInstance()->SetCurParam(nDeviceId,real,nMode);
}
break;
case D_READ_CURRENT_PID_MIN:
case D_READ_CURRENT_PID_MAX:
case D_READ_VELOCITY_PID_MIN:
case D_READ_VELOCITY_PID_MAX:
case D_READ_POSITION_PID_MIN:
case D_READ_POSITION_PID_MAX:
case D_READ_PROFILE_POS_ACC:
case D_READ_PROFILE_POS_DEC:
case D_READ_PROFILE_POS_MAX_SPEED:
case D_READ_PROFILE_VEL_ACC:
case D_READ_PROFILE_VEL_DEC:
case D_READ_PROFILE_VEL_MAX_SPEED:
case D_READ_POS_OFFSET:
case D_READ_HOMING_CUR_MAX:
case D_READ_HOMING_CUR_MIN:
case D_READ_CALIBRATION_ANGLE:
{
data.Skip(5);
qreal maximum = data.ReadInt();
maximum = maximum / (1 << 24);
Mediator::getInstance()->SetCurParam(nDeviceId,maximum , nMode);
}
break;
case D_READ_MAX_POS:
case D_READ_MIN_POS:
{
data.Skip(5);
qreal maximum = data.ReadInt();
maximum = maximum / (1 << 24);
Mediator::getInstance()->SetCurParam(nDeviceId,maximum , nMode);
}
break;
case D_SET_MODE:
{
if (buf.at(5) == '\1')
{
Mediator::getInstance()->activeModeSuccessfully(nDeviceId);
}
}
break;
case D_ERROR:
{
data.Skip(5);
quint16 nErrorId = data.ReadShort();
if(nErrorId > 0)
{
emit Mediator::getInstance()->ErrorMsg(nDeviceId,nErrorId);
}
}
break;
case D_READ_ADDRESS:
{
data.Skip(5);
int nMac = data.ReadInt();
// quint32 nMac = 1000;
Mediator::getInstance()->reciveMotorInfo(nMac,nDeviceId);
}
break;
case D_READ_HOMING_LIMIT:
{
data.Skip(4);
quint8 lenth = data.ReadByte();//check data lenth, compat different proxy version
if(lenth == 1)
{
quint8 nValue = data.ReadByte();
Mediator::getInstance()->SetCurParam(nDeviceId,nValue,nMode);
}
else
{
qint16 nValue = data.ReadShort();
Mediator::getInstance()->SetCurParam(nDeviceId,nValue,nMode);
}
}
break;
case D_READ_CURRENT_SCALE:
{
data.Skip(5);
qreal maximum = data.ReadShort();
maximum = maximum / (1 << 8);
if(maximum>=8 && maximum <= 35)//to confirm that current is valid.
{
Mediator::getInstance()->SetCurParam(nDeviceId,maximum , nMode);
}
else
{
MsgBox::Tip(nullptr,tr("Tip"),"Current maximum is error!");
}
}
break;
case D_READ_FILTER_C_STATUS:
case D_READ_FILTER_V_STATUS:
case D_READ_FILTER_P_STATUS:
{
data.Skip(5);
qint8 status = data.ReadByte();
Mediator::getInstance()->SetCurParam(nDeviceId,status,nMode);
}
break;
case D_READ_FILTER_C_VALUE:
case D_READ_FILTER_V_VALUE:
case D_READ_FILTER_P_VALUE:
case D_READ_TEMP_INVERTER:
case D_READ_TEMP_MOTOR:
case D_READ_TEMP_PROTECT:
case D_READ_TEMP_RECOVERY:
{
data.Skip(5);
qreal value = data.ReadUShort();
value = value / (1 << 8);
Mediator::getInstance()->SetCurParam(nDeviceId,value , nMode);
}
break;
case D_READ_LOCK_ENERGY:
{
data.Skip(5);
qreal value = data.ReadInt();
value = value / 75.225;
Mediator::getInstance()->SetCurParam(nDeviceId,value , nMode);
}
break;
case D_READ_CALIBRATION_SWITCH:
case D_READ_MOTORS_SWITCH:
{
data.Skip(5);
qint8 status = data.ReadByte();
Mediator::getInstance()->SetCurParam(nDeviceId,status,nMode);
}
break;
case D_CRC_ERROR:
{
int a = 0;
}
break;
case D_READ_VERSION:
{
data.Skip(5);
quint32 version = data.ReadUByte()*1000000+data.ReadUByte()*1000+data.ReadUByte();
Mediator::getInstance()->SetCurParam(nDeviceId,version,nMode);
}
break;
default:
bRet = true;
break;
}
}
void InnfosProxy::SendProxy(const quint8 nDeviceId, const int nIdx)
{
NoDataProxy proxy(nIdx,nDeviceId);
proxy.m_pData->WriteShort(0);
proxy.m_pData->AddProxyEnd();
proxy.Send();
}
void InnfosProxy::SendProxy(const quint8 nDeviceId, const quint32 nDeviceMac, const int nIdx, const quint8 newDeviceId)
{
InnfosProxy proxy(nIdx,nDeviceId);
proxy.m_pData->WriteShort(5);
proxy.m_pData->WriteData(nDeviceMac);
proxy.m_pData->WriteData(newDeviceId);
proxy.m_pData->AddCRCCode(5);
proxy.m_pData->AddProxyEnd();
proxy.Send();
}
void InnfosProxy::SendQrealProxy(const quint8 nDeviceId, const int nIdx, qreal data)
{
switch (nIdx) {
case D_SET_CHART_THRESHOLD:
{
qint16 nScaleValue = data*(1<<15);
SendProxy(nDeviceId,nIdx,nScaleValue);
}
break;
case D_SET_CURRENT:
case D_SET_CURRENT_ID:
{
int nScaleValue = data/Mediator::getInstance()->currentScale(nDeviceId)*(1<<24);
SendProxy(nDeviceId,nIdx,nScaleValue);
}
break;
case D_SET_VELOCITY:
case D_SET_CURRENT_MAX_VEL:
{
int nScaleValue = data/Mediator::getInstance()->velocityScale(nDeviceId)*(1<<24);
SendProxy(nDeviceId,nIdx,nScaleValue);
}
break;
default:
{
int nScaleValue = data*(1<<24);
SendProxy(nDeviceId,nIdx,nScaleValue);
}
break;
}
}
//template<class T>
//void SendProxy(const int nDeviceId, const int nIdx, T data)
//{
// InnfosProxy proxy(nIdx,nDeviceId);
// proxy.m_pData->WriteShort(sizeof(T));
// proxy.m_pData->WriteData(data);
// proxy.m_pData->AddCRCCode(sizeof(T));
// proxy.m_pData->AddProxyEnd();
//}
/*无数据协议*/
NoDataProxy::NoDataProxy(const int nId, const quint8 nDeviceId):
InnfosProxy(nId,nDeviceId)
{
}
void NoDataProxy::decode(QByteArray &buf)
{
if (buf.size() < 6)
{
return;
}
// bool bRet = false;
// quint8 nMode = buf.at(2);
// switch (nMode)
// {
// default:
// bRet = true;
// break;
// }
}
/*有数据无校验协议*/
NoCrcProxy::NoCrcProxy(const int nId, const quint8 nDeviceId) :
InnfosProxy(nId,nDeviceId)
{
}
void NoCrcProxy::decode(QByteArray &buf)
{
IData data(buf);
if (buf.size() < 6)
{
return;
}
bool bRet = false;
quint8 nMode = buf.at(2);
quint8 nDeviceId = buf.at(1);
#ifdef DEVICEID_127
nDeviceId -= 128;
#endif
switch (nMode)
{
case D_HANDSHAKE:
//IData::deviceId = buf.at(1);
if (buf.at(5) == '\0')
{
Mediator::getInstance()->Handshake(nDeviceId,false);//
}
else
{
Mediator::getInstance()->Handshake(nDeviceId,true);
}
break;
case D_SET_POSITION_P:
case D_SET_POSITION_I:
case D_SET_POSITION_D:
case D_SET_CURRENT_P:
case D_SET_CURRENT_I:
case D_SET_VELOCITY_P:
case D_SET_VELOCITY_I:
case D_SET_CURRENT:
case D_SET_POSITION:
case D_SET_VELOCITY:
case D_SET_DEVICE_ID:
if (buf.at(5) == '\x1')
{
Mediator::getInstance()->SetSucceed(nDeviceId,nMode);
bRet = true;
}
else
{
Mediator::getInstance()->SetFailed(nMode);
}
break;
case D_CLEAR_ERROR:
if (buf.at(5) == '\x1')
{
//错误清除成功
emit Mediator::getInstance()->clearErrorSuccess(nDeviceId);
bRet = true;
}
break;
case D_CLEAR_HOMING:
if (buf.at(5) == '\x1')
{
//homing清除成功
emit Mediator::getInstance()->clearHomingSuccessfully();
}
break;
case D_SET_PAIRS:
if (buf.at(5) == '\x1')
{
//极对数设置成功,获取各个模式参数
}
break;
case D_CHART_DATA_STATR:
Mediator::getInstance()->receiveNoDataProxy(nMode);
break;
case D_CAN_CONNECT:
{
//InnfosProxy::SendProxy(0,D_READ_ADDRESS);
AutoRecoginze::getInstance()->setConnectStatus(AutoRecoginze::USB_CONNECTED);
}
break;
case D_SAVE_PARAM:
if (buf.at(5) == '\x1')
{
//保存成功
MsgBox::Tip(nullptr,tr("Tip"),"Download params sucessfully!");
}
else
{
MsgBox::Tip(nullptr,tr("Tip"),"Download params failed!");
}
break;
case D_READ_LAST_STATE:
if (buf.at(5) == '\x0')
{
MsgBox::Tip(nullptr,tr("Tip"),tr("Last abnormal shutdown of the device ID %1, please check \n the current location of each device is normal!").arg(nDeviceId));
}
break;
case D_SET_SWITCH_MOTORS:
{
quint8 nValue = buf.at(5);
if(nValue == 0)//如果设置失败了,保持原来的值不变
{
MsgBox::Tip(nullptr,tr("Tip"),tr("Failed to shutdown the device ID %1, please try again!").arg(nDeviceId));
Mediator::getInstance()->revertSwitch(nDeviceId);
}
else
{
Mediator::getInstance()->SetSucceed(nDeviceId,nMode);
}
}
break;
case D_READ_MOTORS_SWITCH://暂时处理
{
data.Skip(5);
qint8 status = data.ReadByte();
Mediator::getInstance()->SetCurParam(nDeviceId,status,nMode);
}
break;
case D_READ_MOTOR_MODE://暂时处理
{
data.Skip(5);
quint8 status = data.ReadUByte();
Mediator::getInstance()->SetCurParam(nDeviceId,status,nMode);
}
break;
default:
bRet = true;
break;
}
}
ProxyWatcher * ProxyWatcher::m_pWatcher = nullptr;
ProxyWatcher *ProxyWatcher::getInstance()
{
if(!m_pWatcher)
m_pWatcher = new ProxyWatcher();
return m_pWatcher;
}
void ProxyWatcher::autoDestroy()
{
if(m_pWatcher)
{
m_pWatcher->checkTimeout();
delete m_pWatcher;
}
m_pWatcher = nullptr;
}
void ProxyWatcher::addSendItem(const QByteArray &buf)
{
checkTimeout();
if(buf.size() >3 && buf.at(2)==2)
return;
logStruct * pStruct = new logStruct();
pStruct->sendTime = QTime::currentTime();
pStruct->sendContent = buf;
m_sendContents.push_back(pStruct);
}
void ProxyWatcher::reciveItem(const QByteArray &buf)
{
checkTimeout();
QMutexLocker locker(&m_qmMutex);
foreach (logStruct * pStruct, m_sendContents) {
QByteArray content = pStruct->sendContent;
if((content.at(1)==buf.at(1) || content.at(1)=='\0') && content.at(2)==buf.at(2))
{
m_sendContents.removeOne(pStruct);
delete pStruct;
break;
}
}
}
ProxyWatcher::~ProxyWatcher()
{
for (int i=m_sendContents.size();--i>=0;)
{
delete m_sendContents.at(i);
}
//delete m_pLogStream;
}
void ProxyWatcher::checkTimeout()
{
QMutexLocker locker(&m_qmMutex);
QTime curTime = QTime::currentTime();
for (int i=m_sendContents.size();--i>=0;)
{
logStruct * pStruct = m_sendContents.at(i);
if(pStruct->sendTime.msecsTo(curTime) > 1000)
{
m_sendContents.removeOne(pStruct);
QString str;
str = QString(pStruct->sendContent.toHex()) + QString(" ")+pStruct->sendTime.toString();
if(m_pLogFile)
{
QTextStream out(m_pLogFile);
out<<"Fail "<< str/*QString(pStruct->sendContent.toHex()) << " " <<pStruct->sendTime.toString()*/;
endl(out);
}
//qDebug()<<"Fail "<< pStruct->sendContent.toHex() << pStruct->sendTime;
delete pStruct;
#ifdef WARNING_DEBUG
//Communication::getInstance()->stop();
//int nRet = QMessageBox::warning(nullptr,tr("Warning"),str,QMessageBox::Ok);
// if(nRet)
// Communication::getInstance()->reStart();
#endif
}
}
}
ProxyWatcher::ProxyWatcher(QObject *parent):
QObject(parent),
m_pLogFile(nullptr),
m_pLogStream(nullptr)
{
m_pLogFile = new QFile("./logFile.txt",this);
if(m_pLogFile->open(QFile::WriteOnly|QFile::Text|QFile::Truncate))
{
//m_pLogStream = new QTextStream(m_pLogFile);
}
else
{
m_pLogFile->close();
m_pLogFile = nullptr;
}
}