1
|
// Modbus_COMS.cpp: implementation of the CModbus_TCPC class.
|
2
|
//
|
3
|
//////////////////////////////////////////////////////////////////////
|
4
|
|
5
|
//#include "stdafx.h"
|
6
|
#include "modbus_tcpc4.h"
|
7
|
#include "se_btl.h"
|
8
|
#include "se_log.h"
|
9
|
//#include <math.h>
|
10
|
|
11
|
#ifdef _DEBUG
|
12
|
#undef THIS_FILE
|
13
|
static char THIS_FILE[]=__FILE__;
|
14
|
#define new DEBUG_NEW
|
15
|
#endif
|
16
|
|
17
|
//////////////////////////////////////////////////////////////////////
|
18
|
// Construction/Destruction
|
19
|
//////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
CModbus_TCPC::~CModbus_TCPC()
|
22
|
{
|
23
|
}
|
24
|
|
25
|
CModbus_TCPC::CModbus_TCPC()
|
26
|
{
|
27
|
|
28
|
}
|
29
|
|
30
|
|
31
|
void CModbus_TCPC::Init(S_PROTOCOLCFG *pcfg)
|
32
|
{
|
33
|
PRawCtrl = pcfg->PRawCtrl;
|
34
|
pRxBuf = pcfg->pRxBuf;
|
35
|
pTxBuf = pcfg->pTxBuf;
|
36
|
pCmdMem = pcfg->pCmdMem;
|
37
|
pRtu = pcfg->pRtu;
|
38
|
pLink = pcfg->pLink;
|
39
|
pTable = pcfg->pTable;
|
40
|
pZfDataCtrl = pcfg->pZfDataCtrl;
|
41
|
// pHis = pcfg->pHis;
|
42
|
|
43
|
Policy =0;
|
44
|
m_xto16 = pLink->GetKwhScanInterval();
|
45
|
if (m_xto16 > 0){
|
46
|
if (3 != m_xto16 && 4 != m_xto16) m_xto16 = 3;
|
47
|
}
|
48
|
else m_xto16 = 0;
|
49
|
PrintLog(1, "m_xto16 = %d!", m_xto16);
|
50
|
m_func34 = m_xto16;
|
51
|
m_func12 = 1;
|
52
|
WillSendType = m_func12;
|
53
|
if (m_xto16) WillSendType = m_func34;
|
54
|
cnt_sequence =0;
|
55
|
m_RcvTimeOuts=pLink->GetRxTimeouts();
|
56
|
if(m_RcvTimeOuts<=0) m_RcvTimeOuts=10;
|
57
|
|
58
|
sint32 rtuno = pLink->GetRtuNo();
|
59
|
// m_SyncTime = pRtu->GetSyncTimeInt(rtuno)*60;
|
60
|
// if(m_SyncTime<=0)m_SyncTime=60;
|
61
|
m_LastSynctime = 0;
|
62
|
m_LastSendTime = 0;
|
63
|
addrDI = 0;
|
64
|
addrAI = 0;
|
65
|
addrDI = pRtu->GetSyncTimeInt(rtuno);//????RTU??ʱ??ͬ??????
|
66
|
addrAI = pRtu->GetKwhNum(rtuno);//????RTU??ACC?????
|
67
|
|
68
|
scopeDI = pRtu->GetYxNum(rtuno);
|
69
|
scopeAI = pRtu->GetYcNum(rtuno);
|
70
|
IndexDI = 0;
|
71
|
IndexAI = 0;
|
72
|
numDI = 0;
|
73
|
numAI = 0;
|
74
|
if (m_xto16)scopeDI = 0;
|
75
|
}
|
76
|
|
77
|
void CModbus_TCPC::GetWillSendType()
|
78
|
{
|
79
|
if (m_xto16){
|
80
|
Policy=1;
|
81
|
WillSendType = m_xto16; return;
|
82
|
}
|
83
|
|
84
|
if(Policy == 0) //Initial Step
|
85
|
{
|
86
|
if (WillSendType == m_func12)
|
87
|
{
|
88
|
if(IndexDI >= scopeDI)
|
89
|
{
|
90
|
WillSendType = m_func34;
|
91
|
IndexDI = 0;
|
92
|
}
|
93
|
|
94
|
return;
|
95
|
}
|
96
|
else if (WillSendType == m_func34)
|
97
|
{
|
98
|
if(IndexAI >= scopeAI)
|
99
|
{
|
100
|
WillSendType = m_func12;
|
101
|
IndexAI = 0;
|
102
|
Policy = 1;
|
103
|
}
|
104
|
|
105
|
return;
|
106
|
}
|
107
|
|
108
|
return;
|
109
|
}
|
110
|
|
111
|
if (WillSendType == m_func12)
|
112
|
WillSendType = m_func34;
|
113
|
else
|
114
|
WillSendType = m_func12;
|
115
|
}
|
116
|
|
117
|
//???ɶ?д???????Ĵ???????Ȧ״̬??
|
118
|
//[?豸??ַ] [?????01] [??ʼ?Ĵ?????ַ??8λ] [??8λ] [??ȡ?ļĴ???????8λ] [??8λ] [CRCУ??ĵ?8λ] [CRCУ??ĸ?8λ]
|
119
|
//??ֻ?ɶ????????Ĵ?????????״̬??
|
120
|
//[?豸??ַ] [?????02] [??ʼ?Ĵ?????ַ??8λ] [??8λ] [??ȡ?ļĴ???????8λ] [??8λ] [CRCУ??ĵ?8λ] [CRCУ??ĸ?8λ]
|
121
|
//д??????????Ȧ״̬??
|
122
|
//[?豸??ַ] [?????05] [?????õļĴ?????ַ??8λ] [??8λ] [???õ????ݸ?8λ] [??8λ] [CRCУ??ĵ?8λ] [CRCУ??ĸ?8λ]
|
123
|
//???ɶ?дģ?????Ĵ????????ּĴ?????
|
124
|
//[?豸??ַ] [?????03] [??ʼ?Ĵ?????ַ??8λ] [??8λ] [??ȡ?ļĴ???????8λ] [??8λ] [CRCУ??ĵ?8λ] [CRCУ??ĸ?8λ]
|
125
|
//??ֻ?ɶ?ģ?????Ĵ?????????Ĵ?????
|
126
|
//[?豸??ַ] [?????04] [??ʼ?Ĵ?????ַ??8λ] [??8λ] [??ȡ?ļĴ???????8λ] [??8λ] [CRCУ??ĵ?8λ] [CRCУ??ĸ?8λ]
|
127
|
//д????ģ?????Ĵ????????ּĴ?????
|
128
|
//[?豸??ַ] [?????06] [?????õļĴ?????ַ??8λ] [??8λ] [???õ????ݸ?8λ] [??8λ] [CRCУ??ĵ?8λ] [CRCУ??ĸ?8λ]
|
129
|
sint32 CModbus_TCPC::RxProc()
|
130
|
{
|
131
|
sint32 rtuno = pLink->GetRtuNo();
|
132
|
|
133
|
uint8 buf[1024];
|
134
|
int datalen = 0 ,datanum =0;
|
135
|
int i,j;
|
136
|
sint32 buflen=pRxBuf->GetReadableSize();
|
137
|
int nowtime=GetNowSecond();
|
138
|
|
139
|
E_RAW_ComStat commstatus = pLink->GetCommStatus();
|
140
|
if ( commstatus==CMST_RX_CNT && (nowtime-m_LastSendTime) >= m_RcvTimeOuts)
|
141
|
{
|
142
|
//??ʱ???ط???ֱ????????һ???ն?
|
143
|
pRxBuf->Move(buflen);
|
144
|
pLink->RegisterFrm(FRAME_RX_TIMEOUT);
|
145
|
pLink->SetCommStatus(CMST_TX_CNT);
|
146
|
PrintLog(LOG_VIOLATION, "RTUNO = %d ???ݳ?ʱ%d!",rtuno,m_RcvTimeOuts);
|
147
|
return -1;
|
148
|
}
|
149
|
if (buflen<7) //?????ݻ????ݲ??????ı???·ͨѶ״̬???????ȴ?
|
150
|
return -1;
|
151
|
datalen = pRxBuf->Read(buf,7,DEF_BUFF_NOMOVE);
|
152
|
|
153
|
/* int ret = CheckFrameHeader(buf,7);
|
154
|
if(ret<0)
|
155
|
{
|
156
|
pRxBuf->Move(buflen);
|
157
|
pLink->RegisterFrm(FRAME_RX_MISSTEP);
|
158
|
PrintLog(LOG_VIOLATION,"ModbusHead??һ??\n");
|
159
|
|
160
|
return -1;
|
161
|
}
|
162
|
else if( ret == 0)
|
163
|
return 0;
|
164
|
*/
|
165
|
|
166
|
uint16 d_len=buf[4]*256 + buf[5] + 6;
|
167
|
if(buflen<d_len)
|
168
|
return -1;
|
169
|
datalen = pRxBuf->Read(buf,d_len,DEF_BUFF_MOVE);
|
170
|
|
171
|
// PrintLog(LOG_INFORMATION,"<<<<< ReceiveType=%d, numbyte=%d",buf[7],buf[8]);
|
172
|
|
173
|
if(buf[7] & 0x80)
|
174
|
return ErrorHandle(buf[7]);
|
175
|
|
176
|
uint16 YxYcId=0;
|
177
|
switch(buf[7])
|
178
|
{
|
179
|
case 1:
|
180
|
case 2:
|
181
|
//if (WillSendType == m_func12)
|
182
|
{
|
183
|
//????ң??
|
184
|
for(i=0;i<buf[8];i++)
|
185
|
{
|
186
|
uint8 value=buf[9+i];
|
187
|
for(j=0;j<8;j++)
|
188
|
{
|
189
|
YxYcId=8*i+j+IndexDI;
|
190
|
if(YxYcId >= (IndexDI + numDI))
|
191
|
break;
|
192
|
|
193
|
PRawCtrl->PutAYx(rtuno,YxYcId+1,value&1);//YX NO +1
|
194
|
value>>=1;
|
195
|
}
|
196
|
}
|
197
|
//PrintLog(LOG_INFORMATION, "---- Receive RTU%d Yx data ",rtuno);
|
198
|
|
199
|
if(buf[8]*8 < numDI)
|
200
|
IndexDI += buf[8]*8;
|
201
|
else
|
202
|
IndexDI += numDI;
|
203
|
|
204
|
pLink->RegisterFrm(FRAME_RX_SUC);
|
205
|
pLink->SetCommStatus(CMST_NORMAL);
|
206
|
|
207
|
return 1;
|
208
|
}
|
209
|
break;
|
210
|
case 3:
|
211
|
case 4:
|
212
|
//????ң??
|
213
|
//if (WillSendType == m_func34)
|
214
|
{
|
215
|
for(i=0;i<buf[8]/2;i++)
|
216
|
{
|
217
|
YxYcId=i+IndexAI;
|
218
|
if(YxYcId >= (IndexAI + numAI))
|
219
|
break;
|
220
|
sint32 yc_value=256*buf[9+2*i]+buf[9+2*i+1];
|
221
|
PRawCtrl->PutAYc(rtuno,YxYcId+1,yc_value); //YC NO +1
|
222
|
|
223
|
}
|
224
|
|
225
|
//PrintLog(LOG_INFORMATION, ">>>> Receive RTU%d Yc data Func=%d", rtuno, m_func34);
|
226
|
pLink->RegisterFrm(FRAME_RX_SUC);
|
227
|
pLink->SetCommStatus(CMST_NORMAL);
|
228
|
|
229
|
if (m_xto16){
|
230
|
//????ң??
|
231
|
for (i = 0; i < buf[8] / 2; i++)
|
232
|
{
|
233
|
YxYcId = i + IndexAI;
|
234
|
if (YxYcId >= (IndexAI + numAI))
|
235
|
break;
|
236
|
uint8 value1 = buf[9 + 2 * i + 1];
|
237
|
uint8 value2 = buf[9 + 2 * i];
|
238
|
for (j = 0; j < 8; j++)
|
239
|
{
|
240
|
YxYcId = 16 * i + j + IndexDI;
|
241
|
PRawCtrl->PutAYx(rtuno, YxYcId + 1, value1 & 1);//YX NO +1
|
242
|
value1 >>= 1;
|
243
|
YxYcId = 16 * i + 8 + j + IndexDI;
|
244
|
PRawCtrl->PutAYx(rtuno, YxYcId + 1, value2 & 1);//YX NO +1
|
245
|
value2 >>= 1;
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
if(buf[8]/2 < numAI)
|
250
|
IndexAI += buf[8]/2;
|
251
|
else
|
252
|
IndexAI += numAI;
|
253
|
if (m_xto16){
|
254
|
if (buf[8] / 2 < numAI)
|
255
|
IndexDI += buf[8] * 8;
|
256
|
else
|
257
|
IndexDI += numDI;
|
258
|
}
|
259
|
|
260
|
return 1;
|
261
|
}
|
262
|
break;
|
263
|
case 5:
|
264
|
//ң?ط???
|
265
|
if(WillSendType == 5)
|
266
|
{
|
267
|
pLink->RegisterFrm(FRAME_RX_SUC);
|
268
|
pLink->SetCommStatus(CMST_NORMAL);
|
269
|
return 1;
|
270
|
}
|
271
|
break;
|
272
|
case 6:
|
273
|
//ң??????
|
274
|
if(WillSendType == 6)
|
275
|
{
|
276
|
pLink->RegisterFrm(FRAME_RX_SUC);
|
277
|
pLink->SetCommStatus(CMST_NORMAL);
|
278
|
return 1;
|
279
|
}
|
280
|
break;
|
281
|
case 16:
|
282
|
WillSendType = m_func34;
|
283
|
{
|
284
|
pLink->RegisterFrm(FRAME_RX_SUC);
|
285
|
pLink->SetCommStatus(CMST_NORMAL);
|
286
|
return 1;
|
287
|
}
|
288
|
break;
|
289
|
default:
|
290
|
return 0;
|
291
|
break;
|
292
|
}
|
293
|
|
294
|
// pLink->RegisterFrm(FRAME_RX_SUC);
|
295
|
// pLink->SetCommStatus(CMST_NORMAL);
|
296
|
|
297
|
return 1;
|
298
|
}
|
299
|
|
300
|
sint32 CModbus_TCPC::CheckFrameHeader(uint8* buff,sint32 len)
|
301
|
{
|
302
|
if(len<7)
|
303
|
return 0;
|
304
|
|
305
|
if(buff[0] != cnt_sequence/256 || buff[1] != cnt_sequence%256)
|
306
|
return -1;
|
307
|
|
308
|
sint32 rtuno = pLink->GetRtuNo();
|
309
|
uint8 rtuaddr=pRtu->GetRtuAddr(rtuno);
|
310
|
|
311
|
if(buff[2]!=0 || buff[3]!=0 || buff[6]!= rtuaddr)
|
312
|
return -2;
|
313
|
|
314
|
return 1;
|
315
|
}
|
316
|
|
317
|
uint8 CModbus_TCPC::ErrorHandle(uint8 errfunc)
|
318
|
{
|
319
|
uint8 retFunc = errfunc & 0x7f;
|
320
|
if(retFunc != WillSendType) //???????
|
321
|
return 0;
|
322
|
|
323
|
if (WillSendType == m_func12)
|
324
|
{
|
325
|
IndexDI = 0;
|
326
|
numDI = 0;
|
327
|
}
|
328
|
else if (WillSendType == m_func34)
|
329
|
{
|
330
|
IndexAI = 0;
|
331
|
numAI = 0;
|
332
|
IndexDI = 0;
|
333
|
numDI = 0;
|
334
|
}
|
335
|
|
336
|
pLink->RegisterFrm(FRAME_RX_SUC);
|
337
|
pLink->SetCommStatus(CMST_NORMAL);
|
338
|
|
339
|
return 1;
|
340
|
}
|
341
|
|
342
|
uint8 CModbus_TCPC::OperateRegister(uint8 FuncNo, uint16 Address, uint16 Value)
|
343
|
{
|
344
|
uint8 txbuff[256];
|
345
|
sint32 rtuno=pLink->GetRtuNo();
|
346
|
uint8 rtuadd=pRtu->GetRtuAddr(rtuno);
|
347
|
uint8 len=0;
|
348
|
if(cnt_sequence>=65535)
|
349
|
cnt_sequence=0;
|
350
|
else
|
351
|
cnt_sequence++;
|
352
|
//7bytes Head
|
353
|
txbuff[len++]=cnt_sequence/0x100;//H Transaction Identifier
|
354
|
txbuff[len++]=cnt_sequence%0x100;//L
|
355
|
txbuff[len++]=0;//Protocol Identifier "0 = MODBUS protocol"
|
356
|
txbuff[len++]=0;
|
357
|
txbuff[len++]=0;//len_H
|
358
|
txbuff[len++]=6;//len_L
|
359
|
txbuff[len++]=rtuadd;//Unit Identifier
|
360
|
|
361
|
txbuff[len++]=FuncNo;
|
362
|
txbuff[len++]=Address/0x100;//??
|
363
|
txbuff[len++]=Address%0x100;//??
|
364
|
txbuff[len++]=Value/0x100;//??
|
365
|
txbuff[len++]=Value%0x100;//??
|
366
|
|
367
|
// pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)txbuff,len);
|
368
|
pLink->RegisterFrm(FRAME_TX_SUC);
|
369
|
pTxBuf->Write(txbuff,len);
|
370
|
pLink->SetCommStatus(CMST_RX_CNT);
|
371
|
m_LastSendTime = GetNowSecond();
|
372
|
|
373
|
//PrintLog(LOG_INFORMATION,">>>>>>RTU%d SendType = %d ,start=%d ,num=%d",rtuno,WillSendType,Address,Value);
|
374
|
|
375
|
return len;
|
376
|
}
|
377
|
|
378
|
uint8 CModbus_TCPC::Operate_16(uint16 Address, uint16 num,uint16* pValue)
|
379
|
{
|
380
|
if(num>125 || pValue==NULL)
|
381
|
{
|
382
|
PrintLog(LOG_ERROR,">>>>>> Wrong Func_16 parameter");
|
383
|
return 0;
|
384
|
}
|
385
|
uint8 txbuff[256];
|
386
|
sint32 rtuno=pLink->GetRtuNo();
|
387
|
uint8 rtuadd=pRtu->GetRtuAddr(rtuno);
|
388
|
uint8 len=0;
|
389
|
if(cnt_sequence>=65535)
|
390
|
cnt_sequence=0;
|
391
|
else
|
392
|
cnt_sequence++;
|
393
|
//7bytes Head
|
394
|
txbuff[len++]=cnt_sequence/0x100;//H Transaction Identifier
|
395
|
txbuff[len++]=cnt_sequence%0x100;//L
|
396
|
txbuff[len++]=0;//Protocol Identifier "0 = MODBUS protocol"
|
397
|
txbuff[len++]=0;
|
398
|
txbuff[len++]=0;//len_H
|
399
|
txbuff[len++]=num*2+7;//len_L
|
400
|
txbuff[len++]=rtuadd;//Unit Identifier
|
401
|
|
402
|
txbuff[len++]=0x10;
|
403
|
txbuff[len++]=Address/0x100;//??
|
404
|
txbuff[len++]=Address%0x100;//??
|
405
|
txbuff[len++]=num/0x100;
|
406
|
txbuff[len++]=num%0x100;
|
407
|
txbuff[len++]=num*2;
|
408
|
uint8 i=0;
|
409
|
for(i=0; i<num; i++)
|
410
|
{
|
411
|
txbuff[len++]=pValue[i]/0x100;//??
|
412
|
txbuff[len++]=pValue[i]%0x100;//??
|
413
|
}
|
414
|
|
415
|
// pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)txbuff,len);
|
416
|
pLink->RegisterFrm(FRAME_TX_SUC);
|
417
|
pTxBuf->Write(txbuff,len);
|
418
|
pLink->SetCommStatus(CMST_RX_CNT);
|
419
|
m_LastSendTime = GetNowSecond();
|
420
|
|
421
|
//PrintLog(LOG_INFORMATION,">>>>>>RTU%d SendType = 16 ,start=%d ,num=%d",rtuno,Address,num);
|
422
|
|
423
|
return len;
|
424
|
}
|
425
|
|
426
|
sint32 CModbus_TCPC::TxProc()
|
427
|
{
|
428
|
E_RAW_ComStat commstatus = pLink->GetCommStatus();
|
429
|
if( commstatus !=CMST_TX_CNT && commstatus !=CMST_NORMAL)
|
430
|
return 0;
|
431
|
|
432
|
if(pRxBuf->GetReadableSize()>0)
|
433
|
{
|
434
|
PrintLog(LOG_ERROR, "------- ???? ????????!\n");
|
435
|
|
436
|
pRxBuf->Move(pRxBuf->GetReadableSize());
|
437
|
}
|
438
|
|
439
|
if(Policy && ProcCmd())
|
440
|
{
|
441
|
pLink->SetCommStatus(CMST_RX_CNT);
|
442
|
return 1; //?????ⲿ?·?????
|
443
|
}
|
444
|
|
445
|
/* sint32 nowtime = GetNowSecond();
|
446
|
if(m_SyncTime>0 && ((nowtime - m_LastSynctime) > m_SyncTime))
|
447
|
{
|
448
|
m_LastSynctime = nowtime;
|
449
|
return SendSyncTime(nowtime);
|
450
|
}
|
451
|
*/
|
452
|
//<xyanglee>temp
|
453
|
// if(( nowtime - m_LastSendTime)<1)
|
454
|
// return 0;
|
455
|
|
456
|
uint8 len=0;
|
457
|
GetWillSendType();
|
458
|
|
459
|
sint32 rtuno = pLink->GetRtuNo();
|
460
|
switch(WillSendType)
|
461
|
{
|
462
|
case 1://read DIO R/W
|
463
|
case 2:
|
464
|
if(scopeDI<1) break;
|
465
|
if(IndexDI >= scopeDI)
|
466
|
IndexDI = 0;
|
467
|
|
468
|
if((scopeDI - IndexDI)>MAX_BYTE_NUM*8)
|
469
|
numDI = MAX_BYTE_NUM*8;
|
470
|
else
|
471
|
numDI = scopeDI - IndexDI;
|
472
|
if(numDI>0)
|
473
|
len = OperateRegister(WillSendType,IndexDI+addrDI,numDI);
|
474
|
break;
|
475
|
case 3://read AIO R/W
|
476
|
case 4:
|
477
|
if(scopeAI<1) break;
|
478
|
if(IndexAI>=scopeAI)
|
479
|
{IndexAI = 0;
|
480
|
if(m_xto16)IndexDI = 0;}
|
481
|
|
482
|
if((scopeAI - IndexAI)>MAX_BYTE_NUM/2)
|
483
|
numAI = MAX_BYTE_NUM/2;
|
484
|
else
|
485
|
numAI = scopeAI - IndexAI;
|
486
|
if (m_xto16)numDI = numAI * 16;
|
487
|
if(numAI>0)
|
488
|
len = OperateRegister(WillSendType,IndexAI+addrAI,numAI);
|
489
|
break;
|
490
|
default:
|
491
|
break;
|
492
|
}
|
493
|
|
494
|
if(len>0)
|
495
|
pLink->SetCommStatus(CMST_RX_CNT);
|
496
|
|
497
|
return len;
|
498
|
}
|
499
|
|
500
|
uint8 CModbus_TCPC::SendSyncTime(sint32 nowtime)
|
501
|
{
|
502
|
uint16 buff[256];
|
503
|
|
504
|
SSE_CLOCK seclock;
|
505
|
CSeTime SeTime;
|
506
|
SeTime.CovertTime(nowtime,&seclock);
|
507
|
/*
|
508
|
buff[0] = 0xFF00;
|
509
|
buff[1] = seclock.year;
|
510
|
buff[2] = seclock.month;
|
511
|
buff[3] = seclock.day;
|
512
|
buff[4] = seclock.hour;
|
513
|
buff[5] = seclock.minute;
|
514
|
buff[6] = seclock.second;
|
515
|
*/
|
516
|
buff[0] = seclock.year/1000 * 0x1000 + seclock.year/100%10 *0x100+seclock.year/10%10 * 0x10 + seclock.year%10 ;
|
517
|
buff[1] = seclock.month/10%10 *0x1000 +seclock.month%10*0x100+seclock.day/10%10 *0x10 +seclock.day%10;
|
518
|
buff[2] = seclock.hour/10%10 *0x1000 +seclock.hour%10*0x100+seclock.minute/10%10 *0x10 +seclock.minute%10;
|
519
|
buff[3] = seclock.second/10%10 *0x10 +seclock.second%10;
|
520
|
|
521
|
//40080
|
522
|
Operate_16(79,4,buff);
|
523
|
WillSendType=16;
|
524
|
|
525
|
return 1;
|
526
|
}
|
527
|
|
528
|
BOOL CModbus_TCPC::ProcCmd()
|
529
|
{
|
530
|
BOOL b_ret=FALSE;
|
531
|
sint32 rtuno = pLink->GetRtuNo();
|
532
|
uint8 rtuaddr = pRtu->GetRtuAddr(rtuno);
|
533
|
|
534
|
if(pCmdMem->GetCmdNum(rtuno)<=0)
|
535
|
return FALSE;
|
536
|
S_RAWCMD rawcmd;
|
537
|
if(pCmdMem->GetACmd(rtuno,&rawcmd)==0)
|
538
|
return FALSE;
|
539
|
PrintLog(LOG_INFORMATION,"?յ?RTU%d???ն? rtuaddr:%d ????:%d\n",rtuno,rtuaddr,rawcmd.type);
|
540
|
uint8 len=0;
|
541
|
m_cmdaddr =rawcmd.src;
|
542
|
switch(rawcmd.type)
|
543
|
{
|
544
|
case DC_K_CMD_DOOPER: //ң??
|
545
|
{
|
546
|
S_DO_CTL *cmdinfo;
|
547
|
cmdinfo=(S_DO_CTL *)(&rawcmd.data);
|
548
|
uint16 ykno = (uint16)cmdinfo->ptAddr;//ң?غ?
|
549
|
switch(cmdinfo->funcCode)
|
550
|
{
|
551
|
case DC_K_CTL_DOSEL://ѡ??
|
552
|
//ֱ?ӷ?У
|
553
|
rawcmd.type=DC_K_CMDRET_DOOPER;
|
554
|
rawcmd.len=sizeof(cmdinfo);
|
555
|
rawcmd.src = m_cmdaddr;
|
556
|
cmdinfo->retCode=1;
|
557
|
|
558
|
pCmdMem->RptAResult(rtuno,rawcmd);
|
559
|
break;
|
560
|
case DC_K_CTL_DOEXE://ִ??
|
561
|
{
|
562
|
uint16 AoNo=(ykno+15)/16;
|
563
|
uint16 bit=(ykno-1)%16;
|
564
|
float32 yctmp=0;
|
565
|
uint16 ytVal=1<<bit;
|
566
|
PRawCtrl->GetAYc(rtuno,AoNo,&yctmp);
|
567
|
uint16 ycVal=(uint16)yctmp;
|
568
|
|
569
|
uint16 ykvalue;
|
570
|
if(cmdinfo->ctlVal==0){//??բ
|
571
|
ykvalue = 0x0000;
|
572
|
ycVal = ycVal & (~ytVal);
|
573
|
}
|
574
|
if(cmdinfo->ctlVal==1){//??բ
|
575
|
ykvalue = 0xff00;
|
576
|
ycVal = ycVal | (ytVal);
|
577
|
}
|
578
|
|
579
|
if(ykno>=1 )
|
580
|
{
|
581
|
WillSendType = 5;
|
582
|
len=OperateRegister(5,ykno-1+addrDI-1,ykvalue);
|
583
|
b_ret=TRUE;
|
584
|
}
|
585
|
|
586
|
/* if(ykno>=1 )
|
587
|
{
|
588
|
WillSendType = 6;
|
589
|
len=OperateRegister(6,AoNo-1+addrAI,ycVal);
|
590
|
b_ret=TRUE;
|
591
|
}
|
592
|
*/ //<xyanglee>
|
593
|
PrintLog(LOG_WARNING,"<<<<<RTU%d YK no=%d val=%d\n",rtuno,ykno,cmdinfo->ctlVal);
|
594
|
}
|
595
|
break;
|
596
|
case DC_K_CTL_DODEL://????
|
597
|
break;
|
598
|
}
|
599
|
}
|
600
|
break;
|
601
|
case DC_K_CMD_AOOPER:
|
602
|
{
|
603
|
S_AO_CTL * aoinfo;
|
604
|
aoinfo = (S_AO_CTL*)(&rawcmd.data);
|
605
|
|
606
|
switch(aoinfo->funcCode)
|
607
|
{
|
608
|
case DC_K_CTL_AOSEL://ѡ??
|
609
|
//ֱ?ӷ?У
|
610
|
rawcmd.type=DC_K_CMDRET_AOOPER;
|
611
|
rawcmd.len=sizeof(aoinfo);
|
612
|
rawcmd.src = m_cmdaddr;
|
613
|
aoinfo->retCode=1;
|
614
|
|
615
|
pCmdMem->RptAResult(rtuno,rawcmd);
|
616
|
break;
|
617
|
case DC_K_CTL_AOEXE://ִ??
|
618
|
{
|
619
|
sint32 ytvalue=(sint32)(aoinfo->ptVal);
|
620
|
uint16 ptno = aoinfo->ptAddr;
|
621
|
if(ptno>=1)
|
622
|
{
|
623
|
WillSendType = 6;
|
624
|
len=OperateRegister(6,ptno-1+addrAI,ytvalue);
|
625
|
b_ret=TRUE;
|
626
|
}
|
627
|
//<xyanglee>
|
628
|
PrintLog(LOG_WARNING,"<<<<<<RTU%d YT no=%d val=%d\n",rtuno,ptno,ytvalue);
|
629
|
}
|
630
|
break;
|
631
|
case DC_K_CTL_AODEL://????
|
632
|
break;
|
633
|
|
634
|
default:
|
635
|
break;
|
636
|
}
|
637
|
}
|
638
|
break;
|
639
|
default: //?????????·????????
|
640
|
break;
|
641
|
}
|
642
|
|
643
|
pCmdMem->DelACmd(rtuno);
|
644
|
return b_ret;
|
645
|
}
|
646
|
|
647
|
sint32 CModbus_TCPC::GetNowSecond()
|
648
|
{
|
649
|
CSeTime SE_T;
|
650
|
TCriterionTime tmptime;//sint32
|
651
|
SE_T.GetNow(&tmptime);
|
652
|
return (sint32)tmptime;
|
653
|
}
|
654
|
|
655
|
void CModbus_TCPC::SetExtendedPara(uint16 no, sint32 val)
|
656
|
{
|
657
|
if(no == 0){//3to16or4to16
|
658
|
if (val){
|
659
|
m_xto16 = val;
|
660
|
if (3 != val && 4!=val) m_xto16 = 3;
|
661
|
}
|
662
|
else m_xto16 = 0;
|
663
|
}
|
664
|
if(no == 1){//func1,2
|
665
|
if (1 == val || 2 == val)
|
666
|
m_func12 = val;
|
667
|
else m_func12 = 1;
|
668
|
}
|
669
|
if(no == 2){//func3,4
|
670
|
if (3 == val || 4 == val)
|
671
|
m_func34 = val;
|
672
|
else m_func34 = 3;
|
673
|
}
|
674
|
if(no == 3)//DIO-fromAdd
|
675
|
addrDI = val;
|
676
|
if(no == 4)//AIO-fromAdd
|
677
|
addrAI = val;
|
678
|
if (m_xto16)WillSendType = m_func34;
|
679
|
else{
|
680
|
WillSendType = m_func12;
|
681
|
sint32 rtuno = pLink->GetRtuNo();
|
682
|
scopeDI = pRtu->GetYxNum(rtuno);
|
683
|
}
|
684
|
PrintLog(LOG_WARNING, "SetExtendedPara no=%d val=%d \n", no, val);
|
685
|
}
|
686
|
|
687
|
//??Լ????????
|
688
|
#ifdef __unix
|
689
|
extern "C" CProtocol* CreateProtocol(char *defpara)
|
690
|
#else
|
691
|
extern "C" __declspec(dllexport) CProtocol* CreateProtocol(char *defpara)
|
692
|
#endif
|
693
|
{
|
694
|
CProtocol *pEpv = new CModbus_TCPC;
|
695
|
return pEpv;
|
696
|
}
|
697
|
|