Project

General

Profile

Feature #2954 » L02ATS_modbus_tcps.cpp

yan hong, 04/18/2023 11:18 AM

 
1
// CModbus_TcpS.cpp: implementation of the CModbus_TcpS class.
2
//
3
//MODBUS_tcp  ת????Լ
4

    
5
//#include "stdafx.h"
6
#include "se_log.h"
7
#include "se_btl.h"
8

    
9
#include "L02ATS_modbus_tcps.h"
10
//#include <windows.h>
11

    
12
#ifdef _DEBUG
13
#undef THIS_FILE
14
static char THIS_FILE[]=__FILE__;
15
#define new DEBUG_NEW
16
#endif
17

    
18
//////////////////////////////////////////////////////////////////////
19
// Construction/Destruction
20
//////////////////////////////////////////////////////////////////////
21
CModbus_TcpS::~CModbus_TcpS()
22
{
23
}
24

    
25
void CModbus_TcpS::Init(S_PROTOCOLCFG * pcfg )
26
{
27
	PRawCtrl	=	pcfg->PRawCtrl;
28
	pRxBuf		=	pcfg->pRxBuf;
29
	pTxBuf		=	pcfg->pTxBuf;
30
	pCmdMem		=	pcfg->pCmdMem;
31
	pRtu		=	pcfg->pRtu;
32
	pLink		=	pcfg->pLink;
33
	pTable		=	pcfg->pTable;
34
	pZfDataCtrl	=	pcfg->pZfDataCtrl;
35
//	pHis		=	pcfg->pHis;
36

    
37
	m_RcvTimeOuts = pLink->GetRxTimeouts();		//?ȴ????
38
	if (m_RcvTimeOuts <= 0)	m_RcvTimeOuts = 5;
39
	m_LastRecvTime = GetNowSecond();
40
}
41

    
42
//??Լ???͹???
43
sint32	CModbus_TcpS::TxProc()
44
{
45
	ProcCmd();
46
	return 1;
47
}
48

    
49
/***********************************************************
50
?????
51
***********************************************************/
52
uint8 CModbus_TcpS::ProcCmd()
53
{
54
	sint32 rtuno			=	pLink->GetRtuNo();
55
	unsigned char rtuaddr	=	pRtu->GetRtuAddr(rtuno);
56
	//??????????򵥰?????ɾ??????Ϊû??ң?ط?У
57
	if(pCmdMem->GetResultNum(rtuno)<=0)
58
		return FALSE;
59
	
60
	S_RAWCMD rawcmd;
61
	if(pCmdMem->GetAResult(rtuno,&rawcmd)==0)
62
		return FALSE;
63
	
64

    
65
	while(pCmdMem->GetResultNum(rtuno)>0)
66
	{
67
		if(pCmdMem->GetAResult(rtuno,&rawcmd)==0)
68
			break;
69
		switch(rawcmd.type)
70
		{
71
		case DC_K_CMDRET_DOOPER:
72
		default: //?????????·????????
73
			pCmdMem->DelAResult(rtuno);
74
			break;
75
		}
76
	}
77
	return FALSE;
78
}
79

    
80
//ң??
81
void CModbus_TcpS::ProcYKCmd(uint16 YaoKongNo,uint16 YkStatus)
82
{
83
	sint32 rtuno=pLink->GetRtuNo();
84
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
85
	//??????ֱ??ѹ??????ң?ض???
86
	S_DO_CTL cmdinfo;
87
	S_RAWCMD  rawcmd;
88
	memset(&rawcmd,0,sizeof(S_RAWCMD));
89

    
90
	cmdinfo.funcCode = DC_K_CTL_DOEXE;
91
	cmdinfo.ptAddr = YaoKongNo+1;
92
	cmdinfo.ctlVal = YkStatus;
93
	cmdinfo.retCode = 0;
94

    
95
	rawcmd.type=DC_K_CMD_DOOPER;
96
	rawcmd.len=sizeof(cmdinfo);
97

    
98
	memcpy(rawcmd.data,&cmdinfo,sizeof(cmdinfo));
99
	if(pCmdMem->RptAZfCmd(rtuno,rawcmd)==0)
100
	{
101
		ReturnException(0x05,0x02);
102
		PrintLog(LOG_INFORMATION,"Ӧ??ң?????? ERROR (%d:%d)",YaoKongNo,YkStatus);
103
		return;
104
	}
105

    
106
	//?򵥷???һ??һ????????
107
	unsigned char buf[1024];
108
	
109
	int buflen =0;
110
    buf[buflen]=TransIdHi;  buflen++;
111
    buf[buflen]=TransIdLo;  buflen++;
112
    buf[buflen]=0; buflen++;
113
    buf[buflen]=0;  buflen++;
114
	buf[buflen]=0; buflen++;
115
	buf[buflen]=6; buflen++;
116
	buf[buflen]=rtuaddr; buflen++;
117
	buf[buflen]=5;  buflen++;//fun
118
	buf[buflen]=HIBYTE(YaoKongNo); buflen++; 
119
	buf[buflen]=LOBYTE(YaoKongNo); buflen++; 
120
	buf[buflen]=HIBYTE(YkStatus); buflen++; 
121
	buf[buflen]=LOBYTE(YkStatus); buflen++; 
122

    
123
	PrintLog(LOG_INFORMATION,"Ӧ??ң?????? (%d:%d)",YaoKongNo,YkStatus);
124
	
125
	pTxBuf->Write(buf,buflen);
126
	pLink->SetCommStatus(CMST_NORMAL);
127

    
128
}
129

    
130
void CModbus_TcpS::ReturnException(uint8 func,uint8 expCode)		//func????????????  expCode???????ص??쳣??
131
{
132
	sint32 rtuno			=	pLink->GetRtuNo();
133
	unsigned char rtuadd	=	pRtu->GetRtuAddr(rtuno);
134

    
135
	unsigned char buf[20]={0};
136
	
137
	sint32 buflen =0;
138
    buf[buflen]=TransIdHi;  buflen++;
139
    buf[buflen]=TransIdLo;  buflen++;
140
    buf[buflen]=0; buflen++;
141
    buf[buflen]=0;  buflen++;
142
	buf[buflen]=0; buflen++;
143
	buf[buflen]=3; buflen++;
144
	buf[buflen]=rtuadd; buflen++;		//??Ԫ??ʶ????rtuվַ
145
	buf[buflen]=func|0x80;  buflen++;//fun	??Ӧ??????=????????+0x80
146
	buf[buflen]=expCode; buflen++;		//??Ӧ?ĵ??е??쳣???????쳣??
147

    
148
	pTxBuf->Write(buf,buflen);
149
	pLink->SetCommStatus(CMST_NORMAL);
150
}
151

    
152
//????AI????
153
sint32	CModbus_TcpS::SendMeasure(uint16 RegOffset,uint16 RegNum)
154
{
155
	sint32 rtuno	=	pLink->GetRtuNo();
156
	uint8 rtuadd	=	pRtu->GetRtuAddr(rtuno);
157

    
158
	sint32 startno = RegOffset+1;
159

    
160
	S_RDB_ZFYc *ycinfo;
161
	CEasyList *yclist=pZfDataCtrl->GetYcStructList();
162
	sint32 ycnum=yclist->GetListLen();
163
	if(yclist==NULL || ycnum<0 || RegNum*2>250)
164
	{
165
		ReturnException(0x03,0x02);
166
		return 0;
167
	}
168

    
169
	ycinfo = (S_RDB_ZFYc *)((*yclist)[ycnum-1]);
170
	if(ycinfo->YcNo < startno || ycinfo->YcNo<(RegOffset+RegNum))
171
	{
172
		PrintLog(LOG_ERROR,"ת?????ݴ???, MAX YCNO=%d",ycinfo->YcNo);
173
		ReturnException(0x03,0x02);
174
		return 0;
175
	}
176

    
177
	uint8 buf[300]={0};
178
	
179
	uint16 dl = RegNum*2+3;
180
	sint32 buflen =0;
181
    buf[buflen]=TransIdHi;  buflen++;
182
    buf[buflen]=TransIdLo;  buflen++;
183
    buf[buflen]=0; buflen++;
184
    buf[buflen]=0;  buflen++;
185
	buf[buflen]=dl/256; buflen++;
186
	buf[buflen]=dl%256; buflen++;
187
	buf[buflen]=rtuadd; buflen++;
188
	buf[buflen]=3;  buflen++;//fun
189
	buf[buflen] = RegNum * 2; buflen++;
190
	
191
	sint32 j;
192
	for(j=0;j<yclist->GetListLen();j++)
193
	{
194
		ycinfo=(S_RDB_ZFYc *)((*yclist)[j]);
195
		if(ycinfo->YcNo> (RegNum+RegOffset))break;
196
		if(ycinfo->YcNo<startno)
197
			continue;
198

    
199
		buf[(ycinfo->YcNo-startno)*2+buflen] = ((sint32)(ycinfo->Val))/256; 
200
		buf[(ycinfo->YcNo-startno)*2+1+buflen] = ((sint32)(ycinfo->Val))%256; 
201
		ycinfo->OldVal=ycinfo->Val;///**Modified by WYF20191112 ???Ӹ?????ֵ+??仯??־**/
202
		UNSETBIT(ycinfo->qds, PTMSK_AI_CHANGE );//FEP	
203
//		ycinfo->chgFlag = 0x00;//WIN		
204
	}
205

    
206
	PrintLog(LOG_INFORMATION,"Ӧ??????03: ??ʼ??ַ%d - ????%d)",RegOffset,RegNum);
207
	
208
	pTxBuf->Write(buf,buflen+RegNum*2);
209
	pLink->SetCommStatus(CMST_NORMAL);
210

    
211
	return 1;
212
}
213

    
214
//????DI????
215
sint32	CModbus_TcpS::SendDiOne(uint16 startAddr,uint16 Num)		
216
{
217
	sint32 rtuno	=	pLink->GetRtuNo();				//??ȡrtu???
218
	uint8 rtuadd	=	pRtu->GetRtuAddr(rtuno);		//??ȡrtuվַ
219

    
220
	sint32 startno = startAddr+1;
221

    
222
	S_RDB_ZFYx *yxinfo;
223
	CEasyList *yxlist=pZfDataCtrl->GetYxStructList();	//??ȡǰ??ת??????.ң?Žṹ?б?
224
	if(yxlist == NULL || Num>2000)//250*8		ǰ??ת??ң?Žṹ?б?Ϊ??||????ļĴ???????????
225
	{
226
		ReturnException(0x01,0x02);		//0x01:????????  0x02:???ص??쳣??
227
		return 0;
228
	}
229

    
230
	sint32 yxnum=yxlist->GetListLen();
231
	if(yxnum<0 )
232
	{
233
		ReturnException(0x01,0x02);
234
		return 0;
235
	}
236

    
237
	yxinfo = (S_RDB_ZFYx *)((*yxlist)[yxnum-1]);
238
	if(yxinfo->YxNo < startno || yxinfo->YxNo<(startAddr+Num))		//ǰ??ת??ң???б???????ң?ű??С????ʼ?????????ַ
239
	{
240
		PrintLog(LOG_ERROR,"ת?????ݴ???, MAX YXNO=%d,startno=%d,Num=%d",yxinfo->YxNo,startno,Num);
241
		ReturnException(0x01,0x02);
242
		return 0;
243
	}
244
	
245
	uint8 buf[300]={0};
246
	
247
	sint32 buflen =0;
248
	sint32 bytecount = (Num+7)/8;
249
	uint16 dl = bytecount+3;
250
    buf[buflen]=TransIdHi;  buflen++;
251
    buf[buflen]=TransIdLo;  buflen++;
252
    buf[buflen]=0; buflen++;
253
    buf[buflen]=0;  buflen++;
254
	buf[buflen]=dl/256; buflen++;				//Length????(Length?????ֶκ???İ????ֽڡ?Ҳ????3 + 2 * N Data(as requested)?ֶΰ??????ֽ?)
255
	buf[buflen]=dl%256; buflen++;				//Length????
256
	buf[buflen]=rtuadd; buflen++;				//??Ԫ??ʶ(RTUվַ)
257
	buf[buflen]=0x01;  buflen++;//fun			//??????
258
	buf[buflen]=bytecount; buflen++;			//Data?????ݳ???
259

    
260

    
261
	sint32 j,byteIdx,bitIdx;		
262
	for(j=0;j<yxlist->GetListLen();j++)
263
	{
264
		yxinfo=(S_RDB_ZFYx *)((*yxlist)[j]);
265
		if(yxinfo->YxNo> (Num+startAddr))break;
266
		if(yxinfo->YxNo<startno)	
267
			continue;
268
		
269
		if(yxinfo->Val == 1)
270
		{
271
			byteIdx = (yxinfo->YxNo-startno)/8 + buflen;
272
			bitIdx  = (yxinfo->YxNo-startno)%8;
273

    
274
			buf[byteIdx] |= 1<<bitIdx; 
275
		}
276
		else{
277
			byteIdx = (yxinfo->YxNo-startno)/8 + buflen;
278
			bitIdx  = (yxinfo->YxNo-startno)%8;
279
			buf[byteIdx] &= (~(1<<bitIdx)) & 0xff; ///**Modified by WYF20180616 ???ƶ???ȡ??**/
280
//			buf[byteIdx] &= (~1<<bitIdx & 0xff); 
281
		}
282
		yxinfo->OldVal=yxinfo->Val;
283
		UNSETBIT(yxinfo->qds, PTMSK_DI_CHANGE );//FEP
284
//		yxinfo->chgFlag = 0x00;//WIN
285
	}
286

    
287
	PrintLog(LOG_INFORMATION,"Ӧ??????01: ??ʼ??ַ%d - ????%d)",startAddr,Num);
288
	
289
	pTxBuf->Write(buf,buflen+bytecount);
290
	pLink->SetCommStatus(CMST_NORMAL);
291

    
292
	return 1;
293
}
294

    
295
//??Լ???պ???
296
sint32	CModbus_TcpS::RxProc()
297
{
298
	sint32 rtuno		=	pLink->GetRtuNo();
299
	uint8 rtuaddr		=	pRtu->GetRtuAddr(rtuno);		//RTUվַ
300
	
301
	uint8  buf[512];
302
	sint32 buflen = 0 ,datalen = 0,datanum =0;
303
	sint32 nowTime = GetNowSecond();
304
	buflen=pRxBuf->GetReadableSize();
305
	if(buflen< 12)
306
	{
307
		if ((nowTime - m_LastRecvTime) >= m_RcvTimeOuts)
308
		{
309
			pLink->RegisterFrm(FRAME_RX_TIMEOUT);
310
			m_LastRecvTime = nowTime;
311
		}
312

    
313
		return 0;
314
	}
315

    
316
	m_LastRecvTime = nowTime;
317
	pLink->RegisterFrm(FRAME_RX_SUC);
318

    
319
	if(buflen>500)
320
		buflen=500;
321

    
322
	datalen = pRxBuf->Read(buf, buflen, DEF_BUFF_NOMOVE );
323
	sint32 framelen = buf[4]*256 + buf[5];
324
	if( (framelen+6)<=datalen) //?????㹻
325
	{
326
		if (buf[6]!= rtuaddr)//վַ????
327
		{
328
			pRxBuf->Move(datalen);
329
			return 0;
330
		}
331

    
332
		TransIdHi = buf[0];		//header??ͷ  ?????ʶ
333
		TransIdLo = buf[1];		//header??ͷ  ?????ʶ
334

    
335
		//????????????????
336
		if(buf[7] == 0x01)//????DI????
337
		{
338
			uint16 startAddr = buf[8]*256 + buf[9];		//??ʼ??ַ
339
			uint16 pntNum = buf[10]*256 + buf[11];		//?Ĵ???????
340
			SendDiOne(startAddr,pntNum);
341
		}
342
		else if (buf[7] == 0x02)//????????????
343
		{
344
			uint16 startAddr = buf[8] * 256 + buf[9];
345
			uint16 pntNum = buf[10] * 256 + buf[11];
346
			SendSwitch(startAddr, pntNum);
347
		}
348
		else if(buf[7]==0x03)//????AI????
349
		{
350
			uint16 startAddr = buf[8]*256 + buf[9];		
351
			uint16 pntNum	= buf[10]*256 + buf[11];	
352
			SendMeasure(startAddr,pntNum);
353
		}
354
		else if (buf[7] == 0x04) {	//??????CIOS?Ĵ???
355
			uint16 startAddr = buf[8] * 256 + buf[9];
356
			uint16 pntNum = buf[10] * 256 + buf[11];
357
			SendMeasureOne(startAddr,pntNum);
358
		}
359
		else if(buf[7]==0x05)//ң??
360
		{
361
			uint16 m_YaoKongNo = buf[8]*256 + buf[9];
362
			uint16 m_YkStatus	= buf[10]*256 + buf[11];
363
			ProcYKCmd(m_YaoKongNo,m_YkStatus);
364
		}
365
		else if(buf[7]==0x06)//ң??//WYF 20190110
366
		{
367
			uint16 m_YaoKongNo = buf[8]*256 + buf[9];
368
			uint16 m_YkStatus	= buf[10]*256 + buf[11];
369
			ProcYTCmd(m_YaoKongNo,m_YkStatus);
370
		}//WYF 20190110
371
		else
372
			ReturnException(buf[7],0x01);//illegal function
373

    
374
		pRxBuf->Move(framelen+6);
375
	}
376

    
377
	return 1;
378
}
379

    
380

    
381
//ң??	//WYF 20190110
382
void CModbus_TcpS::ProcYTCmd(uint16 YaoKongNo,uint16 YkStatus)
383
{
384
	sint32 rtuno=pLink->GetRtuNo();
385
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
386
	//??????ֱ??ѹ??????ң?ض???
387
	S_AO_CTL cmdinfo;
388
	S_RAWCMD  rawcmd;
389
	memset(&rawcmd,0,sizeof(S_RAWCMD));
390

    
391
	cmdinfo.funcCode = DC_K_CTL_AOEXE;
392
	cmdinfo.ptAddr = YaoKongNo+1;
393
	cmdinfo.ptVal = YkStatus;
394
	cmdinfo.retCode = 0;
395

    
396
	rawcmd.type=DC_K_CMD_AOOPER;
397
	rawcmd.len=sizeof(cmdinfo);
398

    
399
	memcpy(rawcmd.data,&cmdinfo,sizeof(cmdinfo));
400
	if(pCmdMem->RptAZfCmd(rtuno,rawcmd)==0)
401
	{
402
		ReturnException(0x06,0x02);
403
		PrintLog(LOG_INFORMATION,"Ӧ??ң?????? ERROR (%d:%d)",YaoKongNo,YkStatus);
404
		return;
405
	}
406

    
407
	//?򵥷???һ??һ????????
408
	unsigned char buf[1024];
409
	
410
	int buflen =0;
411
    buf[buflen]=TransIdHi;  buflen++;
412
    buf[buflen]=TransIdLo;  buflen++;
413
    buf[buflen]=0; buflen++;
414
    buf[buflen]=0;  buflen++;
415
	buf[buflen]=0; buflen++;
416
	buf[buflen]=6; buflen++;
417
	buf[buflen]=rtuaddr; buflen++;
418
	buf[buflen]=6;  buflen++;//fun
419
	buf[buflen]=HIBYTE(YaoKongNo); buflen++; 
420
	buf[buflen]=LOBYTE(YaoKongNo); buflen++; 
421
	buf[buflen]=HIBYTE(YkStatus); buflen++; 
422
	buf[buflen]=LOBYTE(YkStatus); buflen++; 
423

    
424
	PrintLog(LOG_INFORMATION,"Ӧ??ң?????? (%d:%d)",YaoKongNo,YkStatus);
425
	
426
	pTxBuf->Write(buf,buflen);
427
	pLink->SetCommStatus(CMST_NORMAL);
428

    
429
}
430
//WYF 20190110
431

    
432
//??????CIOS?Ĵ???
433
sint32 CModbus_TcpS::SendMeasureOne(uint16 RegOffset, uint16 RegNum) {
434
	sint32 rtuno = pLink->GetRtuNo();
435
	uint8 rtuadd = pRtu->GetRtuAddr(rtuno);
436

    
437
	sint32 startno = RegOffset + 1;
438

    
439
	S_RDB_ZFYc *ycinfo;
440
	CEasyList *yclist = pZfDataCtrl->GetYcStructList();
441
	sint32 ycnum = yclist->GetListLen();
442
	if (yclist == NULL || ycnum<0 || RegNum > 2000) {
443
		ReturnException(0x04, 0x02);
444
		return 0;
445
	}
446

    
447
	ycinfo = (S_RDB_ZFYc *)((*yclist)[ycnum - 1]);
448
	if (ycinfo->YcNo < startno || ycinfo->YcNo<(RegOffset + RegNum)) {
449
		PrintLog(LOG_ERROR, "ת?????ݴ???, MAX YCNO=%d", ycinfo->YcNo);
450
		ReturnException(0x04, 0x02);
451
		return 0;
452
	}
453

    
454
	uint8 buf[4009] = { 0 };
455

    
456
	uint16 dl = RegNum * 2 + 3;
457
	sint32 buflen = 0;
458
	buf[buflen] = TransIdHi;  buflen++;
459
	buf[buflen] = TransIdLo;  buflen++;
460
	buf[buflen] = 0; buflen++;
461
	buf[buflen] = 0;  buflen++;
462
	buf[buflen] = dl / 256; buflen++;
463
	buf[buflen] = dl % 256; buflen++;
464
	buf[buflen] = rtuadd; buflen++;
465
	buf[buflen] = 4;  buflen++;//fun
466
	if (RegNum * 2 > 250) {
467
		buf[buflen] = 0; buflen++;
468
	} else {
469
		buf[buflen] = RegNum * 2; buflen++;
470
	}
471

    
472
	sint32 j;
473
	for (j = 0; j<yclist->GetListLen(); j++) {
474
		ycinfo = (S_RDB_ZFYc *)((*yclist)[j]);
475
		if (ycinfo->YcNo>(RegNum + RegOffset))break;
476
		if (ycinfo->YcNo<startno)
477
			continue;
478

    
479
		buf[(ycinfo->YcNo - startno) * 2 + buflen] = ((sint32)(ycinfo->Val)) / 256;
480
		buf[(ycinfo->YcNo - startno) * 2 + 1 + buflen] = ((sint32)(ycinfo->Val)) % 256;
481
		ycinfo->OldVal = ycinfo->Val;///**Modified by WYF20191112 ???Ӹ?????ֵ+??仯??־**/
482
		UNSETBIT(ycinfo->qds, PTMSK_AI_CHANGE);//FEP	
483
		//		ycinfo->chgFlag = 0x00;//WIN		
484
	}
485
	PrintLog(LOG_INFORMATION, "Ӧ??????04: ??ʼ??ַ%d - ????%d)", RegOffset, RegNum);
486

    
487
	pTxBuf->Write(buf, buflen + RegNum * 2);
488
	pLink->SetCommStatus(CMST_NORMAL);
489

    
490
	return 1;
491
}
492

    
493
//????????????
494
sint32 CModbus_TcpS::SendSwitch(uint16 startAddr, uint16 Num) {
495
	sint32 rtuno = pLink->GetRtuNo();				//??ȡrtu???
496
	uint8 rtuadd = pRtu->GetRtuAddr(rtuno);		//??ȡrtuվַ
497

    
498
	sint32 startno = startAddr + 1;
499

    
500
	S_RDB_ZFYx *yxinfo;
501
	CEasyList *yxlist = pZfDataCtrl->GetYxStructList();	//??ȡǰ??ת??????.ң?Žṹ?б?
502
	if (yxlist == NULL || Num>2000)//250*8		ǰ??ת??ң?Žṹ?б?Ϊ??||????ļĴ???????????
503
	{
504
		ReturnException(0x02, 0x02);		//0x02:????????  0x02:???ص??쳣??
505
		return 0;
506
	}
507

    
508
	sint32 yxnum = yxlist->GetListLen();
509
	if (yxnum<0) {
510
		ReturnException(0x02, 0x02);
511
		return 0;
512
	}
513

    
514
	yxinfo = (S_RDB_ZFYx *)((*yxlist)[yxnum - 1]);
515
	if (yxinfo->YxNo < startno || yxinfo->YxNo<(startAddr + Num))		//ǰ??ת??ң???б???????ң?ű??С????ʼ?????????ַ
516
	{
517
		PrintLog(LOG_ERROR, "ת?????ݴ???, MAX YXNO=%d,startno=%d,Num=%d", yxinfo->YxNo, startno, Num);
518
		ReturnException(0x02, 0x02);
519
		return 0;
520
	}
521

    
522
	uint8 buf[300] = { 0 };
523

    
524
	sint32 buflen = 0;
525
	sint32 bytecount = (Num + 7) / 8;
526
	uint16 dl = bytecount + 3;
527
	buf[buflen] = TransIdHi;  buflen++;
528
	buf[buflen] = TransIdLo;  buflen++;
529
	buf[buflen] = 0; buflen++;
530
	buf[buflen] = 0;  buflen++;
531
	buf[buflen] = dl / 256; buflen++;				//Length????(Length?????ֶκ???İ????ֽڡ?Ҳ????3 + 2 * N Data(as requested)?ֶΰ??????ֽ?)
532
	buf[buflen] = dl % 256; buflen++;				//Length????
533
	buf[buflen] = rtuadd; buflen++;				//??Ԫ??ʶ(RTUվַ)
534
	buf[buflen] = 0x02;  buflen++;//fun			//??????
535
	buf[buflen] = bytecount; buflen++;			//Data?????ݳ???
536

    
537

    
538
	sint32 j, byteIdx, bitIdx;
539
	for (j = 0; j<yxlist->GetListLen(); j++) {
540
		yxinfo = (S_RDB_ZFYx *)((*yxlist)[j]);
541
		if (yxinfo->YxNo>(Num + startAddr))break;
542
		if (yxinfo->YxNo<startno)
543
			continue;
544

    
545
		if (yxinfo->Val == 1) {
546
			byteIdx = (yxinfo->YxNo - startno) / 8 + buflen;
547
			bitIdx = (yxinfo->YxNo - startno) % 8;
548

    
549
			buf[byteIdx] |= 1 << bitIdx;
550
		} else {
551
			byteIdx = (yxinfo->YxNo - startno) / 8 + buflen;
552
			bitIdx = (yxinfo->YxNo - startno) % 8;
553
			buf[byteIdx] &= (~(1 << bitIdx)) & 0xff; ///**Modified by WYF20180616 ???ƶ???ȡ??**/
554
			//			buf[byteIdx] &= (~1<<bitIdx & 0xff); 
555
		}
556
		yxinfo->OldVal = yxinfo->Val;
557
		UNSETBIT(yxinfo->qds, PTMSK_DI_CHANGE);//FEP
558
		//		yxinfo->chgFlag = 0x00;//WIN
559
	}
560

    
561
	PrintLog(LOG_INFORMATION, "Ӧ??????02: ??ʼ??ַ%d - ????%d)", startAddr, Num);
562

    
563
	pTxBuf->Write(buf, buflen + bytecount);
564
	pLink->SetCommStatus(CMST_NORMAL);
565

    
566
	return 1;
567
}
568

    
569
//??ȡ??ǰʱ??
570
sint32 CModbus_TcpS::GetNowSecond()
571
{
572
	CSeTime     SE_T;
573
	TCriterionTime tmptime;//sint32
574
	SE_T.GetNow(&tmptime);
575
	return (sint32)tmptime;
576
}
577

    
578
//??Լ???󴴽?????
579
#ifdef __unix
580
extern "C" CProtocol* CreateProtocol(char *defpara)
581
#else
582
extern "C" __declspec(dllexport)  CProtocol* CreateProtocol(char *defpara)
583
#endif
584
{
585
	CProtocol *pEpv = new CModbus_TcpS;
586
	return pEpv;
587
}
(2-2/4)