-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_mb_master_trans.cpp
378 lines (360 loc) · 13.5 KB
/
c_mb_master_trans.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
#include "c_mb_master_trans.h"
#include <QDebug>
#include "c_endian.h"
int C_MB_master_trans::m_indexID = 0;
C_MB_master_trans::C_MB_master_trans(QObject *parent) : QObject(parent)
{
this->m_transID = C_MB_master_trans::m_indexID;
C_MB_master_trans::m_indexID+=1;
connect(&this->m_timer,&QTimer::timeout,this,&C_MB_master_trans::slot_timer);
}
// 事务轮询
void C_MB_master_trans::slot_timer()
{
MBRequestTransEx trans;
trans.trans = this->m_trans.transInfo.trans;
trans.transID = this->m_transID;
if(MB_func01==trans.trans.funcCode ||
MB_func02==trans.trans.funcCode ||
MB_func03==trans.trans.funcCode ||
MB_func04==trans.trans.funcCode)
{
; //nothing
}else if(MB_func0F==trans.trans.funcCode)
{
int sumPara = this->m_trans.transInfo.trans.paraSum;
for(int i=0;i<sumPara;i++)
{
trans.swtArray.append(char(0));
}
foreach(MBOutputParaCFG cfg,this->m_trans.outListpara)
{
MBOutputParaV dataV;
if(!this->getMonData(cfg.unitGUID,cfg.collGUID,cfg.monInName,dataV)) // 向其他模块获取源数据值
{
continue;
}
if(mbPara_SWT==dataV.paraType)
{
continue;
}
int dex = cfg.paraADC.paraAdr-this->m_trans.transInfo.trans.beginAdr;
if(dex>=0 && dex<sumPara)
{
trans.swtArray[dex] = dataV.dataSWT.swt;
}
}
}else if(MB_func10==trans.trans.funcCode)// 写操作
{
int sumPara = this->m_trans.transInfo.trans.paraSum;
for(int i=0;i<sumPara;i++)
{
MBregister reg;
reg.byte_0=0;
reg.byte_1=0;
trans.regList.append(reg);
}
foreach(MBOutputParaCFG cfg,this->m_trans.outListpara)
{
MBOutputParaV dataV;
if(!this->getMonData(cfg.unitGUID,cfg.collGUID,cfg.monInName,dataV)) // 向其他模块获取源数据值
{
continue;
}
if(mbPara_SWT==dataV.paraType) // 如果是开关量 转化为 0,1 !!!
{
continue; //不作处理
}
int dex = cfg.paraADC.paraAdr-this->m_trans.transInfo.trans.beginAdr;
if(dex<0)
{
continue;
}
// ADC类型判断
if(dataV.paraType!=mbPara_ADC)
{
continue;
}
// 线性变换
dataV.dataADC.fData64 = dataV.dataADC.fData64 * cfg.paraADC.factor +cfg.paraADC.offset;
if(MB_paraREAL32 == cfg.paraADC.dataType)
{
if(trans.regList.size()>=dex+2)
{
float temp = dataV.dataADC.fData64;
quint8 by0,by1,by2,by3;
C_endian::float32(cfg.paraADC.boType,temp,by0,by1,by2,by3);
trans.regList[dex+0].byte_0=by0;
trans.regList[dex+0].byte_1=by1;
trans.regList[dex+1].byte_0=by2;
trans.regList[dex+1].byte_1=by3;
}
}else if(MB_paraREAL64==cfg.paraADC.dataType)
{
if(trans.regList.size()>=dex+4)
{
double temp = dataV.dataADC.fData64;
quint8 by0,by1,by2,by3,by4,by5,by6,by7;
C_endian::float64(cfg.paraADC.boType,temp,by0,by1,by2,by3,by4,by5,by6,by7);
trans.regList[dex+0].byte_0=by0;
trans.regList[dex+0].byte_1=by1;
trans.regList[dex+1].byte_0=by2;
trans.regList[dex+1].byte_1=by3;
trans.regList[dex+3].byte_0=by4;
trans.regList[dex+3].byte_1=by5;
trans.regList[dex+4].byte_0=by6;
trans.regList[dex+4].byte_1=by7;
}
}else if(MB_paraINT32==cfg.paraADC.dataType)
{
if(trans.regList.size()>=dex+2)
{
qint32 temp = dataV.dataADC.fData64;
quint8 by0,by1,by2,by3;
C_endian::int32(cfg.paraADC.boType,temp,by0,by1,by2,by3);
trans.regList[dex+0].byte_0=by0;
trans.regList[dex+0].byte_1=by1;
trans.regList[dex+1].byte_0=by2;
trans.regList[dex+1].byte_1=by3;
}
}else if(MB_paraUINT32==cfg.paraADC.dataType)
{
if(trans.regList.size()>=dex+2)
{
quint32 temp = dataV.dataADC.fData64;
quint8 by0,by1,by2,by3;
C_endian::uint32(cfg.paraADC.boType,temp,by0,by1,by2,by3);
trans.regList[dex+0].byte_0=by0;
trans.regList[dex+0].byte_1=by1;
trans.regList[dex+1].byte_0=by2;
trans.regList[dex+1].byte_1=by3;
}
}else if(MB_paraINT16==cfg.paraADC.dataType)
{
if(trans.regList.size()>=dex+1)
{
qint16 temp = dataV.dataADC.fData64;
quint8 by0,by1;
C_endian::int16(cfg.paraADC.boType,temp,by0,by1);
trans.regList[dex+0].byte_0=by0;
trans.regList[dex+0].byte_1=by1;
}
}else if(MB_paraUINT16==cfg.paraADC.dataType)
{
if(trans.regList.size()>=dex+1)
{
quint16 temp = dataV.dataADC.fData64;
quint8 by0,by1;
C_endian::uint16(cfg.paraADC.boType,temp,by0,by1);
trans.regList[dex+0].byte_0=by0;
trans.regList[dex+0].byte_1=by1;
}
}else
{
continue;
}
}
}
emit sig_reuest(trans); // 定时发送总线请求信号
}
bool C_MB_master_trans::getMonData(QString unitGUID, QString collGUID, QString monInName, MBOutputParaV &data)
{
unitGUID = unitGUID;
collGUID = collGUID;
monInName =monInName;
data = data;
return false;
}
// 设置定时事务查询信息
void C_MB_master_trans::setQueryTrans(const confMasterTrans &trans)
{
this->m_trans = trans;
}
// 启动定时事务查询
void C_MB_master_trans::start()
{
if(!this->m_timer.isActive())
{
this->m_timer.start(this->m_trans.transInfo.timeGap*1000);
}
}
// 停止定时事务查询
void C_MB_master_trans::stop()
{
if(this->m_timer.isActive())
{
this->m_timer.stop();
}
}
// 返回 transID值
int C_MB_master_trans::transID()
{
return this->m_transID;
}
//正常应答
void C_MB_master_trans::replyProc(int transID, quint8 slaveAdr, enumMB_FuncCode fcode, MB_ReplyBody body)
{
qDebug()<<"slave:"<<slaveAdr<<"fCode:"<<fcode;
if(transID!=this->m_transID)
{
return;
}
if(slaveAdr!=this->m_trans.transInfo.trans.slaveAdr)
{
return;
}
if(fcode!=this->m_trans.transInfo.trans.funcCode)
{
return;
}
// ... 解析数据
if(MB_func03==fcode || MB_func04==fcode)
{
QList<MBInputParaV>paraList;
int sum = this->m_trans.listPara.size();
QList<MBregister>reg = body.regList; // 寄存器列表
int regSUM = reg.size();
for(int i=0;i<sum;i++)
{
MBInputParaCFG para = this->m_trans.listPara.at(i);
MBInputParaV data;
data.name = para.name;
data.inName = para.inName;
data.paraType = para.paraType;
if(mbPara_ADC==data.paraType)
{
emumDataType dataType = para.paraADC.dataType;
enumByteOrderType ordertype = para.paraADC.boType;
int dex = para.paraADC.paraAdr-this->m_trans.transInfo.trans.beginAdr; //计算地址->游标
if(dex<0)
{
continue;
}
if(MB_paraREAL32 == dataType)
{
if(regSUM>=dex+2)
{
float temp = C_endian::float32(ordertype,reg.at(dex+0).byte_0,reg.at(dex+0).byte_1,
reg.at(dex+1).byte_0,reg.at(dex+1).byte_1);
data.dataADC.fData64 = temp*para.paraADC.factor +para.paraADC.offset; // 线性变换
paraList.append(data);
}
}else if(MB_paraREAL64 == dataType)
{
if(regSUM>=dex+4)
{
double temp = C_endian::float64(ordertype,reg.at(dex+0).byte_0,reg.at(dex+0).byte_1
,reg.at(dex+1).byte_0,reg.at(dex+1).byte_1
,reg.at(dex+2).byte_0,reg.at(dex+2).byte_1
,reg.at(dex+3).byte_0,reg.at(dex+3).byte_1);
data.dataADC.fData64 = temp*para.paraADC.factor +para.paraADC.offset; // 线性变换
paraList.append(data);
}
}else if(MB_paraINT16 == dataType)
{
if(regSUM>=dex+1)
{
qint16 temp = C_endian::int16(ordertype,reg.at(dex+0).byte_0,reg.at(dex+0).byte_1);
data.dataADC.fData64 = temp*para.paraADC.factor +para.paraADC.offset; // 线性变换
paraList.append(data);
}
}else if(MB_paraUINT16 == dataType)
{
if(regSUM>=dex+1)
{
qint16 temp = C_endian::int16(ordertype,reg.at(dex+0).byte_0,reg.at(dex+0).byte_1);
data.dataADC.fData64 = temp*para.paraADC.factor +para.paraADC.offset; // 线性变换
paraList.append(data);
}
}else if(MB_paraINT32 ==dataType)
{
if(regSUM>=dex+2)
{
qint32 temp = C_endian::int32(ordertype,reg.at(dex+0).byte_0,reg.at(dex+0).byte_1
,reg.at(dex+1).byte_0,reg.at(dex+1).byte_1);
data.dataADC.fData64 = temp*para.paraADC.factor +para.paraADC.offset; // 线性变换
paraList.append(data);
}
}else if(MB_paraUINT32 == dataType)
{
if(regSUM>=dex+2)
{
quint32 temp = C_endian::uint32(ordertype,reg.at(dex+0).byte_0,reg.at(dex+0).byte_1
,reg.at(dex+1).byte_0,reg.at(dex+1).byte_1);
data.dataADC.fData64 = temp*para.paraADC.factor +para.paraADC.offset; // 线性变换
paraList.append(data);
}
}
}
}
// 测试输出 参数-值
foreach (MBInputParaV data, paraList)
{
qDebug()<<data.name<<QString::number(data.dataADC.fData64,'f',6);
}
}else if(MB_func01==fcode || MB_func02==fcode) // 开关量
{
QList<MBInputParaV>paraList;
int sum = this->m_trans.listPara.size();
int regSum = body.swtList.size();
qDebug()<<"regSum"<<regSum;
for(int i=0;i<sum;i++)
{
MBInputParaCFG para = this->m_trans.listPara.at(i);
MBInputParaV data;
data.name = para.name;
data.inName = para.inName;
data.paraType = para.paraType;
if(mbPara_SWT==data.paraType)
{
int dex = para.paraSWT.paraAdr-this->m_trans.transInfo.trans.beginAdr; //计算地址->游标
if(dex<0)
{
continue;
}
if(regSum>dex)
{
data.dataSWT.swt = (quint8)(body.swtList.at(dex));
paraList.append(data);
}
}
}
foreach (MBInputParaV data, paraList)
{
QString str;
if(data.dataSWT.swt!=0)
{
str = "开";
}else
{
str = "关";
}
qDebug()<<data.name<<QString::number(data.dataSWT.swt)<<str;
}
}else if(MB_func05==fcode)
{
}else if(MB_func06==fcode)
{
}else if(MB_func0F==fcode||MB_func10==fcode)
{
qDebug()<<this->m_trans.transInfo.name<<QString(" 写保持寄存器 返回: 地址 %1 数目%2").arg(body.wMulR.adr).arg(body.wMulR.sum);
}
}
//错误应答 RTUmaster
void C_MB_master_trans::replyErr_RTU(int transID, quint8 slaveAdr, enumMB_FuncCode fcode, RTU_Master_ErrCode errcode)
{
transID =transID;
slaveAdr =slaveAdr;
fcode = fcode;
errcode = errcode;
qDebug()<<QString("RTU错误返回:从机:%1 功能码: %2 错误码:%3").arg(slaveAdr).arg(fcode).arg(errcode);
}
//错误应答 TCPmaster
void C_MB_master_trans::replyErr_TCP(int transID, quint8 slaveAdr, enumMB_FuncCode fcode, TCP_Master_ErrCode errcode)
{
transID =transID;
slaveAdr =slaveAdr;
fcode = fcode;
errcode = errcode;
qDebug()<<QString("TCP错误返回:从机:%1 功能码: %2 错误码:%3").arg(slaveAdr).arg(fcode).arg(errcode);
}