Project

General

Profile

Feature #1429 » 103.cpp

yufeng wu, 06/03/2021 12:25 PM

 
1
// 103.cpp: implementation of the C103 class.
2
//
3
//////////////////////////////////////////////////////////////////////
4

    
5
#include "stdafx.h"
6
#include "se_log.h"
7
#include "se_btl.h"
8
//#include "comm\dc_comm.h"
9
#include "103.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
typedef struct EVENT_INFO
21
{
22
	uint8	fun;
23
	uint8	inf;
24
	uint8	sort;
25
	uint8	inx;
26
}EVENT_INFO;
27

    
28

    
29
static EVENT_INFO event_table1[]=   
30
{
31
	{0x01, 0x01, 0, 1},
32
	{0x02, 0x02, 0, 2}, 
33
	{0x03, 0x03, 0, 3}, 
34
	{0x04, 0x04, 0, 4}, 
35
	{0x05, 0x05, 0, 5}, 
36
	{0x06, 0x06, 0, 6}, 
37
	{0x07, 0x07, 0, 7}, 
38
	{0x08, 0x08, 0, 8}, 
39
	{0x09, 0x09, 0, 9},
40
	{0x0a, 0x0a, 0, 10},
41
	{0x0b, 0x0b, 0, 11},
42
	//??????Ϣ
43
	{0x02, 0x07, 1, 19},
44
	{0x96, 0xa1, 1, 20}, 
45
	{0xa0, 0x2f, 1, 21},
46
	{0x3c, 0x3a, 1, 22},
47
	{0xa0, 0x5a, 1, 23},
48
	//{0xa0, 0x5b, 1, 23},
49
	{0xa0, 0x5c, 1, 24},
50
	{0xa0, 0x5d, 1, 25}, 
51
	{0xa0, 0x80, 1, 26},
52
	{0x0c, 0x0c, 1, 27},
53
	{0x04, 0x03, 1, 28},
54
	{0x4a, 0x46, 1, 29},
55
	{0x97, 0x75, 1, 30}, 
56
	{0x4a, 0x27, 1, 31},//?͵?ѹ	
57
	{0xb0, 0x44, 1, 32},
58
	{0x3c, 0x48, 1, 33},
59
	{0x3c, 0x3d, 1, 34}, 
60
	{0xff, 0xff, 1, 35},
61
	{0x4b, 0x5b, 1, 36},
62
	{0x4f, 0x28, 1, 37},//????ѹ
63
};
64

    
65
//ȡ?¼???Ϣ
66
static EVENT_INFO get_event_info(BYTE btFun, BYTE btInfo, EVENT_INFO *p_tab)
67
{
68
	WORD i = 0;
69
	EVENT_INFO evt_info;
70
	WORD wEvetNum = sizeof(event_table1)/sizeof(EVENT_INFO);
71
	
72
	for( ; i<wEvetNum; i++ )
73
	{
74
		if ( p_tab[i].fun == btFun 
75
			 && p_tab[i].inf == btInfo )
76
		{
77
			evt_info = p_tab[i];
78
			return evt_info;
79
		}
80
	}
81

    
82
	evt_info.fun = 0;
83
	return evt_info;
84
}
85

    
86
C103::~C103()
87
{
88
}
89

    
90
void C103::Init(S_PROTOCOLCFG * pcfg )
91
{
92
	PRawCtrl	=	pcfg->PRawCtrl;
93
	pRxBuf		=	pcfg->pRxBuf;
94
	pTxBuf		=	pcfg->pTxBuf;
95
	pCmdMem		=	pcfg->pCmdMem;
96
	pRtu		=	pcfg->pRtu;
97
	pLink		=	pcfg->pLink;
98
	pTable		=	pcfg->pTable;
99
	pZfDataCtrl	=	pcfg->pZfDataCtrl;
100
	pHis		=	pcfg->pHis;
101
	m_rii=0;
102
	m_scn=0;
103
	m_LastRxProtSoe = 1;
104
	InitRtu();
105
}
106

    
107
void C103::InitRtu()
108
{
109
	sint32 rtuno=pLink->GetRtuNo();
110
	
111
	//?????????
112
	int buflen=pRxBuf->GetReadableSize();
113
	pRxBuf->Move(buflen);
114
	buflen=pTxBuf->GetReadableSize();
115
	pTxBuf->Move(buflen);
116
	
117
	m_LinkStatus=LSTATUS_INIT;
118
	m_ReSendCnt=-1;
119
	m_SendBufLen=0;
120
	m_LastSendTime=GetNowSecond();
121
	m_LastCallAllTime=0;
122
	m_LastSyncTime=0;
123
	m_ACDFlag=0;
124
	m_OverTimeFlag=0;
125
	m_MainFCBFlag=0;
126
	m_SubFCBFlag=0;
127
	m_cmdf =0;
128
	m_CRC_ErrCnt=15;
129
	
130
	m_RcvTimeOuts=pLink->GetRxTimeouts();
131
	if(m_RcvTimeOuts<=0)
132
		m_RcvTimeOuts=2;
133
	m_ReSendMaxCount=pLink->GetRetryTimes();
134
	if(m_ReSendMaxCount<=0)
135
		m_ReSendMaxCount=3;
136
	pLink->SetCommStatus(CMST_NORMAL);
137
}
138

    
139
void C103::SetTxRxStatus(E103_STATUS  status)
140
{
141
	sint32 rtuno=pLink->GetRtuNo();
142
	switch(status)
143
	{
144
	case STATUS_CRCERR:
145
		{
146
			m_CRC_ErrCnt--;
147
			pLink->RegisterFrm( FRAME_RX_CHECKERR );
148
			if(m_CRC_ErrCnt<=0)
149
			{
150
				InitRtu();
151
			}
152
			else 
153
			{	
154
				//???򵥳?ʱ?߼???ʼ
155
				m_ReSendCnt++;
156
				m_OverTimeFlag=1;
157
				if(m_ReSendCnt>=m_ReSendMaxCount)
158
				{
159
					if(m_ReSendCnt>5*m_ReSendMaxCount)
160
						InitRtu();
161
					pLink->SetCommStatus(CMST_NORMAL);
162
				}
163
				else
164
				{
165
					pLink->SetCommStatus(CMST_TX_CNT);
166
				}
167
			}
168
			break;
169
		}
170
	case STATUS_OVERTIME:
171
		m_ReSendCnt++;
172
		m_OverTimeFlag=1;
173
		pLink->RegisterFrm( FRAME_RX_TIMEOUT );
174
		if(m_ReSendCnt>=m_ReSendMaxCount)
175
		{
176
			if(m_ReSendCnt>5*m_ReSendMaxCount)
177
				InitRtu();
178
			else
179
			{
180
				int buflen = pRxBuf->GetReadableSize();
181
				pRxBuf->Move(buflen);
182
				buflen = pTxBuf->GetReadableSize();
183
				pTxBuf->Move(buflen);
184
				pLink->SetCommStatus(CMST_NORMAL);
185
				m_OverTimeFlag=0;
186
			}
187
		}
188
		else
189
		{
190
			pLink->SetCommStatus(CMST_TX_CNT);
191
			kprintf(LOG_COMM,DCV_LOG_103, LOG_VIOLATION, "%d???նˣ??ȴ????????ݳ?ʱ", rtuno);
192
		}
193
		m_CRC_ErrCnt=15;
194
		break;
195
	case STATUS_RXOK:
196
	case STATUS_NODATA:
197
		m_CRC_ErrCnt=15;
198
		m_ReSendCnt=-1; 
199
		pLink->RegisterFrm( FRAME_RX_SUC );
200
		sint32 rtuno=pLink->GetRtuNo();
201
		if(m_ACDFlag )pLink->SetCommStatus(CMST_TX_CNT);
202
		else pLink->SetCommStatus(CMST_NORMAL);
203
		break;
204
	}
205
}
206

    
207
//??Լ???͹???
208
sint32	C103::TxProc()
209
{
210
	sint32 rtuno=pLink->GetRtuNo();
211
	if(pLink->GetCommStatus()==CMST_RX_CNT)
212
		return 1;
213
	
214
	//????????,?????·??ͨ??ɾ?????и???·??????
215
	if(m_LinkStatus!=LSTATUS_OK)
216
	{
217
		while(pCmdMem->GetCmdNum(rtuno)>0)
218
			pCmdMem->DelACmd(rtuno);
219
	}
220

    
221
	//?ѱ????????Զ?????
222
	int nowtime=GetNowSecond();
223
	if(m_LastRxProtSoe>0 && (nowtime-m_LastRxProtSoe)>10)
224
	{
225
		reset_protect_info();
226
		m_LastRxProtSoe=-1;
227
	}
228

    
229
	//??ʱ??վ?ᷢ?Ͷ?֡???ݱ?????ȫ??????ϡ?
230
	int buflen=pRxBuf->GetReadableSize();
231
	if  (buflen >0 )
232
		return 1;
233
	
234
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
235
	
236
	if(m_OverTimeFlag && m_ReSendCnt>=0)
237
	{
238
		ReSendData();//??ʱ?ط?
239
		return 1;
240
	}
241
	
242
	switch(m_LinkStatus)
243
	{
244
	case LSTATUS_INIT:
245
		SendRequestLinkStatusCmd();
246
		return 1;
247
	case LSTATUS_RESET:
248
		SendResetLinkCmd();
249
		return 1;
250
	case LSTATUS_OK:
251
		if(m_ACDFlag) 	//?ٻ?һ??????
252
		{
253
			SendCallClassOneDataCmd();
254
			m_ACDFlag=0;
255
			return 1;
256
		}
257
		
258
		if(ProcCmd()) //?????ⲿ?·?????
259
		{
260
			return 1;
261
		}
262

    
263
		//????Уʱ
264
		uint32 spacetime=pRtu->GetSyncTimeInt(rtuno);
265
		if(spacetime ==0) spacetime = 10;
266
		if( uint32(nowtime-m_LastSyncTime) >= spacetime*60)
267
		{
268
			SendTimeCmd();
269
			m_LastSyncTime = nowtime;
270
			return 1;
271
		}
272
	
273
		//??ȫ????
274
		spacetime=pLink->GetAllDataScanInterval();
275
		if(spacetime>0)
276
		{
277
			if( uint32(nowtime-m_LastCallAllTime) >= spacetime*60) 
278
			{
279
				m_LastCallAllTime = nowtime;
280
				SendCallAllDataCmd();
281
				return 1;
282
			}
283
		}
284

    
285
		SendCallClassTwoDataCmd();
286
		return 1;
287
	}
288
	return 1;
289
}
290

    
291
//???鱣???¼?
292
void C103::reset_protect_info()
293
{
294
	WORD		i;
295
	int rtuno=pLink->GetRtuNo();
296
	WORD wEvetNum = sizeof(event_table1)/sizeof(EVENT_INFO);
297
	
298
	for(i=0; i<wEvetNum; i++)
299
	{
300
		if(event_table1[i].sort == 1){
301
			PRawCtrl->PutAYx(rtuno,event_table1[i].inx-1,0);
302
		}
303
	}
304
}
305

    
306
/***********************************************************
307
?????
308
***********************************************************/
309
BOOL C103::ProcCmd()
310
{
311
	sint32 rtuno=pLink->GetRtuNo();
312
	if(pCmdMem->GetCmdNum(rtuno)<=0)
313
		return FALSE;
314
	
315
	S_RAWCMD rawcmd;
316
	if(pCmdMem->GetACmd(rtuno,&rawcmd)==0)
317
		return FALSE;
318
	
319
	S_CmdInfo *cmdinfo;
320
	while(pCmdMem->GetCmdNum(rtuno)>0)
321
	{
322
		if(pCmdMem->GetACmd(rtuno,&rawcmd)==0)
323
			break;
324
		switch(rawcmd.type)
325
		{
326
		case DC_K_PIPE_SYNCTIME: //Уʱ????
327
			pCmdMem->DelACmd(rtuno);
328
			SendTimeCmd();
329
			return TRUE;
330
			break;
331
		case DC_K_PIPE_YKOPER: //ң??
332
			cmdinfo=(S_CmdInfo *)(&rawcmd.Data);
333
			m_cmdprotsort =rawcmd.cmdsrc_sort;
334
			m_cmdsrc_rtu  =rawcmd.cmdsrc_rtu; 
335
			m_CmdSrc=rawcmd.addr;
336
			if (cmdinfo->func1==DC_K_PIPE_YKCMD)
337
			{
338
				//ѡ??
339
				SendYKCmd(*cmdinfo,DC_K_YKSELECT);
340
			}
341
			else if (cmdinfo->func1==DC_K_PIPE_YKEXE)
342
			{
343
				//ִ??
344
				SendYKCmd(*cmdinfo,DC_K_YKEXECUTE);
345
			}
346
			else if (cmdinfo->func1==DC_K_PIPE_YKDEL)
347
			{
348
				//????
349
				SendYKCmd(*cmdinfo,DC_K_YKCANCEL);
350
			}
351
			
352
			pCmdMem->DelACmd(rtuno);
353
			return TRUE;
354
			break;
355
		default: //?????????·????????
356
			pCmdMem->DelACmd(rtuno);
357
			break;
358
		}
359
	}
360
	return FALSE;
361
}
362

    
363
/*****************************************************************
364
ң?????TypeΪң??????
365
******************************************************************/
366
void C103::SendYKCmd(S_CmdInfo cmdinfo ,BYTE type)
367
{
368
	sint32 rtuno=pLink->GetRtuNo();
369
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
370
	BYTE status;
371
	uint8 ykno,ykbak;
372
	ykno = ykbak = (uint8)cmdinfo.ctrlpt;//ң?غ?
373
	status = (BYTE)cmdinfo.func2;//ң??״̬
374
	if(type == DC_K_YKSELECT){//ֱ?ӷ?У
375
		S_RAWCMD rawcmd;
376
		S_CmdInfo cmdinfo;
377
		rawcmd.type=DC_K_PIPE_YKRETURN;
378
		rawcmd.rtuno=rtuno;
379
		cmdinfo.ctrlpt=ykno;
380
		cmdinfo.func1=DC_K_PIPE_YKCMD;
381
		if(status==0xcc)
382
			cmdinfo.func2=1;
383
		else cmdinfo.func2=0;
384
		rawcmd.len=sizeof(cmdinfo);
385
		rawcmd.cmdsrc_sort = m_cmdprotsort;
386
		rawcmd.cmdsrc_rtu = m_cmdsrc_rtu; 
387
		rawcmd.addr=m_CmdSrc;
388
		cmdinfo.ret=1;
389
		memcpy(rawcmd.Data,&cmdinfo,sizeof(cmdinfo));
390
		pCmdMem->ZfAResult(rtuno,rawcmd);
391
		kprintf(LOG_COMM,DCV_LOG_103,LOG_ERROR,"%d???նˣ?ֱ??ң?ط?У??ȷ", rtuno);
392
		return;
393
	}
394
	else if(type == DC_K_YKCANCEL){
395
		return;
396
	}
397

    
398
	S103_CONTROL_BYTE		control;
399
	BYTE		*buf = m_SendBuf;
400
	*buf++ = 0x68;
401
	*buf++ = 0x0a;
402
	*buf++ = 0x0a;
403
	*buf++ = 0x68;
404
	control.CtrlField.DIR = 0;
405
	control.CtrlField.PRM = 1;
406
	control.CtrlField.FCV = 1;
407
	control.CtrlField.FCB =m_MainFCBFlag ;
408
	m_MainFCBFlag=(m_MainFCBFlag+1)%2;
409
	control.CtrlField.FC  = 3;
410
	*buf++ = control.BCtrlField;
411
	*buf++ = rtuaddr;
412
	
413
	//ң?غ??ں?̨???㿪ʼ
414
	//ykno =ykno+0xb01;//
415
	//if(ykno >0xb80)
416
	//	kprintf(LOG_COMM,DCV_LOG_103,LOG_ERROR,"ң?غų???128,ÿվ??ң???????ܴ???128?? %d",rtuno);
417
	
418

    
419
	*buf++=20;
420
	*buf++=0x81;
421

    
422
	*buf++ =20;
423
	*buf++=rtuaddr;
424
	*buf++=240;			
425
	*buf++=160+ykno;	
426
		
427
	if(status==0)
428
		*buf++=1;
429
	else if(status==1)
430
		*buf++=2;
431
	else return;
432
	
433
	*buf++ = m_rii++;
434
	*buf++=GetByteCheckSum(m_SendBuf+4,buf-m_SendBuf-4);
435
	*buf++=0x16;
436
	m_SendBufLen = buf - m_SendBuf;
437
	
438
	SendToPhysical();
439

    
440
	kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"ң?أ?????=%x   ???=%x rtuno:%d" ,status , ykbak,rtuno);
441
}
442

    
443
/************************************************************************************************
444
??λͨ?ŵ?Ԫ
445
************************************************************************************************/
446
void	C103::SendRequestLinkStatusCmd()
447
{
448
	sint32 rtuno=pLink->GetRtuNo();
449
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
450
	
451
	S103_CONTROL_BYTE		control;
452
	BYTE		*buf = m_SendBuf;
453
	*buf++ = 0x10;
454
	control.CtrlField.DIR = 0;
455
	control.CtrlField.PRM = 1;
456
	control.CtrlField.FCV = 0;
457
	control.CtrlField.FCB = 0;
458
	control.CtrlField.FC = 0x00;
459
	*buf++ = control.BCtrlField;
460
	*buf++ = rtuaddr;
461
	*buf++=GetByteCheckSum(m_SendBuf+1,buf-m_SendBuf-1);
462
	*buf++=0x16;
463
	m_SendBufLen = buf - m_SendBuf;
464
	
465
	kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"%d???նˣ???????·״̬", rtuno);
466
	
467
	SendToPhysical();
468
}
469

    
470

    
471
/************************************************************************************************
472
??λ?Է???·
473
************************************************************************************************/
474
void	C103::SendResetLinkCmd()
475
{
476
	sint32 rtuno=pLink->GetRtuNo();
477
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
478
	
479
	S103_CONTROL_BYTE		control;
480
	BYTE		*buf = m_SendBuf;
481
	*buf++ = 0x10;
482
	control.CtrlField.DIR = 0;
483
	control.CtrlField.PRM = 1;
484
	control.CtrlField.FCV = 0;
485
	control.CtrlField.FCB = 0;
486
	control.CtrlField.FC  = 0;
487
	*buf++ = control.BCtrlField;
488
	*buf++ = rtuaddr;
489
	*buf++=GetByteCheckSum(m_SendBuf+1,buf-m_SendBuf-1);
490
	*buf++=0x16;
491
	m_SendBufLen = buf - m_SendBuf;
492
	
493
	kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"%d???նˣ???λ??·", rtuno);
494
	
495
	SendToPhysical();
496
	m_MainFCBFlag = 1;			//??ʼ?????????MainstationFCB??0
497
}
498

    
499
/*********************************************************************************
500
???ն˷??????ٻ?????
501
**********************************************************************************/
502
void	C103::SendCallAllDataCmd()
503
{
504
	sint32 rtuno=pLink->GetRtuNo();
505
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
506
	S103_CONTROL_BYTE		control;
507
	BYTE		*buf = m_SendBuf;
508
	*buf++ = 0x68;
509
	*buf++ = 0x09;
510
	*buf++ = 0x09;
511
	*buf++ = 0x68;
512
	control.CtrlField.DIR = 0;
513
	control.CtrlField.PRM = 1;
514
	control.CtrlField.FCV = 1;
515
	control.CtrlField.FCB = m_MainFCBFlag;
516
	m_MainFCBFlag=(m_MainFCBFlag+1)%2;
517
	control.CtrlField.FC = 3;
518
	*buf++ = control.BCtrlField;
519
	*buf++ = rtuaddr;
520

    
521
	*buf++ = 7;//typ
522
	*buf++ = 0x81;//vsq
523
	*buf++ = 0x09;//cot
524
	*buf++ =rtuaddr;
525
	*buf++ = 255;//fun
526
	*buf++ = 0x00;//inf
527
	*buf++ = m_scn++;
528
	*buf++=GetByteCheckSum(m_SendBuf+4,buf-m_SendBuf-4);
529
	*buf++=0x16;
530
	m_SendBufLen = buf - m_SendBuf;
531
	
532
	kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"%d???նˣ????ٻ?????", rtuno);
533

    
534
	SendToPhysical();
535
}
536

    
537
/*************************************************************************************************************************
538
?ٻ?һ??????
539
*************************************************************************************************************************/
540
BOOL	C103::SendCallClassOneDataCmd()
541
{
542
	sint32 rtuno=pLink->GetRtuNo();
543
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
544
	
545
	S103_CONTROL_BYTE		control;
546
	BYTE		*buf = m_SendBuf;
547
	*buf++ = 0x10;
548
	control.CtrlField.DIR = 0;
549
	control.CtrlField.PRM = 1;
550
	control.CtrlField.FCV = 1;
551
	
552
	control.CtrlField.FC  = 0x0a;
553
	control.CtrlField.FCB = m_MainFCBFlag;
554
	m_MainFCBFlag=(m_MainFCBFlag+1)%2;
555
	*buf++ = control.BCtrlField;
556
	*buf=rtuaddr;
557
	buf ++;
558
	*buf++=GetByteCheckSum(m_SendBuf+1,buf-m_SendBuf-1);
559
	*buf++=0x16;
560
	m_SendBufLen = buf - m_SendBuf;
561
	
562
	kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?ٻ?%d???նˣ?һ??????", rtuno);
563
	
564
	SendToPhysical();
565
	return	TRUE;
566
}
567

    
568
/************************************************************************************************************************
569
?ٻ?????????
570
*************************************************************************************************************************/
571
BOOL	C103::SendCallClassTwoDataCmd()
572
{
573
	sint32 rtuno=pLink->GetRtuNo();
574
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
575
	S103_CONTROL_BYTE		control;
576
	BYTE		*buf = m_SendBuf;
577
	*buf++ = 0x10;
578
	control.CtrlField.DIR = 0;
579
	control.CtrlField.PRM = 1;
580
	control.CtrlField.FCV = 1;
581
	control.CtrlField.FC  = 0x0b;
582
	control.CtrlField.FCB = m_MainFCBFlag;
583
	m_MainFCBFlag=(m_MainFCBFlag+1)%2;
584
	*buf++ = control.BCtrlField;
585
	*buf++ = rtuaddr;
586
	*buf++=GetByteCheckSum(m_SendBuf+1,buf-m_SendBuf-1);
587
	*buf++=0x16;
588
	m_SendBufLen = buf - m_SendBuf;
589
	
590
//	kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?ٻ?%d???նˣ?????????", rtuno);
591
	
592
	SendToPhysical();
593
	return	TRUE;
594
}
595

    
596
/************************************************************************************************
597
Ӧ?ò???õĺ???????????Ӧ?ò㹫????·?????ն˽???ʱ??ͬ??
598
************************************************************************************************/
599
BOOL C103::SendTimeCmd()
600
{
601
	sint32 rtuno=pLink->GetRtuNo();
602
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
603
	
604
	S103_CONTROL_BYTE		control;
605
	BYTE		*buf = m_SendBuf;
606
	*buf++ = 0x68;
607
	*buf++ = 15;
608
	*buf++ = 15;
609
	*buf++ = 0x68;
610
	control.CtrlField.DIR = 0;
611
	control.CtrlField.PRM = 1;
612
	control.CtrlField.FCB = m_MainFCBFlag;
613
	m_MainFCBFlag=(m_MainFCBFlag+1)%2;
614
	control.CtrlField.FCV = 1;
615
	control.CtrlField.FC = 3;
616
	*buf++ = control.BCtrlField;
617
	*buf++ = rtuaddr;
618
	*buf++ = 0x6;//typ
619
	*buf++ = 0x81;//vsq
620
	*buf++ = 0x08;//cot
621
	*buf++ =  255;//?㲥
622
	*buf++ = 0;	//inf
623
	
624
	SSE_CLOCK	sck;
625
	CSeTime     SE_T;
626
	SE_T.GetNow(&sck);
627
	
628
	*buf++ = (uint8)(sck.msecond+sck.second*1000)%256;
629
	*buf++ = (uint8)(sck.msecond+sck.second*1000)/256;
630
	*buf++ = sck.minute;
631
	*buf++ = sck.hour;
632
	*buf++ = ((sck.wday << 5) & 0xE0) | (sck.day & 0x1F); 
633
	*buf++ = sck.month;
634
	*buf++ = (uint8)(sck.year-2000);
635
	*buf++=GetByteCheckSum(m_SendBuf+4, buf-m_SendBuf-4);
636
	*buf++=0x16;
637
	m_SendBufLen = buf - m_SendBuf;
638
	
639
	kprintf(LOG_COMM,DCV_LOG_103,LOG_WARNING, "??%d???ն˷?ʱ??ͬ?????ϵͳʱ??:(%2d-%2d-%2d %2d:%2d:%2d %4d)",
640
		rtuno, sck.year, sck.month, sck.day, sck.hour, sck.minute, sck.second, sck.msecond);
641
	m_cmdf = 1;	
642
	SendToPhysical();
643
	return	TRUE;
644
}
645

    
646

    
647
/************************************************************************************************
648
????GPRS????֧??
649
************************************************************************************************/
650
void C103::AppendIp()
651
{
652
	// ???ĸ?ʽ	'k'+'d'+???ݳ???(2???ֽ?)+???볤??(1???ֽ?)+????+??Լ????	???ݳ???ָ?????ܳ???
653
	if(pLink->GetUseIp() > 0)
654
	{
655
		char* pIp = pRtu->GetRtuIpAddr(pLink->GetRtuNo());
656
		
657
		if(NULL == pIp)
658
		{
659
			DCV_Printf(LOG_COMM, DCV_LOG_103, LOG_VIOLATION, "δȡ??RTU %d ??IPָ??", pLink->GetRtuNo());
660
			return;
661
		}
662
		
663
		int iplen = strlen(pIp);
664
		if(0 == iplen)
665
		{
666
			DCV_Printf(LOG_COMM, DCV_LOG_103, LOG_VIOLATION, "??????RTU %d ??IP", pLink->GetRtuNo());
667
			return;
668
		}
669
		
670
		// ???ӿ?????Ϣ
671
		memmove(m_SendBuf+5+iplen, m_SendBuf, m_SendBufLen);
672
		m_SendBufLen += 5+iplen;
673
		
674
		m_SendBuf[0] = 'k';		// ǰ???ֽ?
675
		m_SendBuf[1] = 'd';		// ǰ???ֽ?
676
		m_SendBuf[2] = LOBYTE(m_SendBufLen);
677
		m_SendBuf[3] = HIBYTE(m_SendBufLen);
678
		m_SendBuf[4] = iplen;	
679
		memcpy(m_SendBuf+5, pIp, iplen);	
680
	}
681
}
682

    
683
/************************************************************************************************
684
?????ݴ???·?㻺???????͵??????㻺????
685
************************************************************************************************/
686
void	C103::SendToPhysical(BOOL bResend)
687
{
688
	if(!bResend)
689
	{
690
		AppendIp();		//GPRSר??IP????ͷ
691
	}
692
	pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)m_SendBuf,m_SendBufLen);
693
	pTxBuf->Write(m_SendBuf,m_SendBufLen);
694
	if(m_cmdf==1){
695
		//???Ӳ???Ҫ????
696
		pLink->SetCommStatus(CMST_NORMAL);
697
		m_cmdf = 0;
698
	}
699
	else 
700
		pLink->SetCommStatus(CMST_RX_CNT);
701
	m_LastSendTime = GetNowSecond();
702
	m_OverTimeFlag=0;
703
}
704

    
705
//??Լ???պ???
706
sint32	C103::RxProc()
707
{
708
	sint32 rtuno=pLink->GetRtuNo();
709
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
710
	
711
	uint8  buf[512];
712
	int i;
713
	int datalen;
714
	int buflen=pRxBuf->GetReadableSize();
715
	int nowtime=GetNowSecond();
716
	
717
	if (pLink->GetCommStatus()==CMST_RX_CNT && (nowtime-m_LastSendTime) >= m_RcvTimeOuts)
718
	{
719
		pRxBuf->Move(buflen);
720
		SetTxRxStatus(STATUS_OVERTIME);
721
		return 1;
722
	}
723
	
724
	if (buflen<=0)		//??????
725
		return 1;
726
	
727
	if(buflen>510) buflen = 510;
728
	pRxBuf->Read(buf,buflen,DEF_BUFF_NOMOVE);
729
	
730
	if(buflen<5)
731
	{
732
		for(i=0; i<buflen; i++)		
733
		{
734
			if(buf[i] == 0XE5) 
735
			{
736
				pLink->RegisterFrmCode(RAW_CODEDIR_UP,(char *)(buf+i),1);
737
				pRxBuf->Move(i+1);
738
				SetTxRxStatus(STATUS_NODATA);
739
				kprintf(LOG_COMM,DCV_LOG_103,LOG_WARNING,"%d???նˣ??????????????", rtuno);
740
				return 1;
741
			}
742
			if(buf[i] == 0X10 || buf[i] == 0X68) //?Ƴ???ʼ??֮ǰ????Ч????
743
			{
744
				pRxBuf->Move(i);
745
				break;
746
			}
747
		}
748
		if(i==buflen)	//?Ƴ?ȫ????Ч????
749
			pRxBuf->Move(buflen);
750
	}
751
	
752
	S103_CONTROL_BYTE contrl;
753
	while ((buflen=pRxBuf->GetReadableSize()) >= 5)
754
	{
755
		pRxBuf->Read(buf, 5, DEF_BUFF_NOMOVE );
756
		
757
		if (buf[0]!= 0x10 && buf[0] != 0x68 && buf[0]!=0xe5)
758
		{
759
			pRxBuf->Move(1);
760
			continue;
761
		}
762
		
763
		if(buf[0]==0xe5)
764
		{
765
			pRxBuf->Move(1);
766
			SetTxRxStatus(STATUS_NODATA);
767
			kprintf(LOG_COMM,DCV_LOG_103,LOG_WARNING,"%d???նˣ??????????????", rtuno);
768
			pLink->RegisterFrmCode(RAW_CODEDIR_UP,(char *)buf,1);
769
			continue;
770
		}
771
		
772
		if(buf[0]==0x10)			//?̶?֡??
773
		{
774
			buflen=5;
775
			if(buf[buflen-1]!=0x16)
776
			{
777
				pRxBuf->Move(1);
778
				continue;
779
			}
780
			
781
			if(GetByteCheckSum(&(buf[1]),2)!=buf[3])
782
			{
783
				pRxBuf->Move(buflen);
784
				SetTxRxStatus(STATUS_CRCERR);
785
				kprintf(LOG_COMM,DCV_LOG_103,LOG_ERROR,"%d???նˣ?????֡У??ʹ???", rtuno);
786
				continue;
787
			}
788
						
789
			m_CRC_ErrCnt=15;
790
			contrl.BCtrlField =buf[1];
791
			if (!contrl.CtrlField.PRM)
792
			{
793
				if (buf[2] != rtuaddr)	
794
				{
795
					//??վַ
796
					kprintf(LOG_COMM,DCV_LOG_103,LOG_WARNING,"%d???նˣ?????֡??վַ???ԣ???վַ:%d ??վ????վַΪ%d", rtuno, rtuaddr, buf[2]);
797
					pRxBuf->Move(buflen);
798
					continue;					
799
				}
800
			}
801
			
802
			pLink->RegisterFrmCode(RAW_CODEDIR_UP,(char *)buf,5);
803
			//				if (contrl.CtrlField.PRM)		//?յ??ն??????ϱ??????ݺ󣬳?ʱʱ?????¼???
804
			//					OverSendTimeCount = 0;
805
			
806
			if (contrl.CtrlField.PRM == 0)
807
				m_ACDFlag = contrl.CtrlField.FCB;
808
			if(buf[1]==0) m_ACDFlag=1; //???????豸???⴦??;
809
			
810
			ProcShortCmd(buf,buflen);
811
			pRxBuf->Move(buflen);
812
			SetTxRxStatus(STATUS_RXOK);//lingws
813
			break;
814
		} //end buf[0]==0x10
815
		else if(buf[0]==0x68)
816
		{
817
			//?жϵڶ?????????
818
			if(buf[3] != 0x68)	
819
			{
820
				pRxBuf->Move(1);
821
				continue;
822
			}		
823
			
824
			//?????????ݳ????Ƿ???ͬ
825
			datalen =buf[1];							
826
			if (buf[1] != buf[2]) 
827
			{
828
				pRxBuf->Move(1);
829
				continue;
830
			}
831
			
832
			//?????ݳ????Ƿ??㹻
833
			if (buflen < (datalen + 6)) {
834
				//  kprintf(LOG_COMM,DCV_LOG_103,LOG_ERROR,"?????ݳ??Ȳ??? %d",rtuno);
835
				return 2;		
836
			}
837
			
838
			contrl.BCtrlField =buf[4];
839
			
840
			//?ҵ?ǡ????ͷ
841
			pRxBuf->Read(buf, datalen+6, DEF_BUFF_NOMOVE );
842
			//??????У??
843
			if(buf[datalen+5]!=0x16)
844
			{
845
				pRxBuf->Move(1);
846
				kprintf(LOG_COMM,DCV_LOG_103,LOG_ERROR,"%d???նˣ?????֡??????У?????", rtuno);
847
				continue;
848
			}
849
			
850
			if(GetByteCheckSum(&(buf[4]),datalen)!=buf[datalen+4])
851
			{
852
				pRxBuf->Move(datalen+6);
853
				SetTxRxStatus(STATUS_CRCERR);
854
				kprintf(LOG_COMM,DCV_LOG_103,LOG_ERROR,"%d???նˣ?????֡У??ʹ???", rtuno);
855
				continue;
856
			}
857
			
858
			//һ??׼???????????????????? 
859
			m_CRC_ErrCnt=15;
860
			if (!contrl.CtrlField.PRM)
861
			{
862
				if (buf[5] != rtuaddr)	
863
				{
864
					kprintf(LOG_COMM,DCV_LOG_103,LOG_WARNING,"%d???նˣ?????֡??վַ???ԣ???վַ:%d ??վ????վַΪ%d", rtuno, rtuaddr, buf[2]);
865
					pRxBuf->Move(datalen+6);
866
					continue;	//??վַ
867
				}
868
			}
869
			
870
			pLink->RegisterFrmCode(RAW_CODEDIR_UP,(char *)buf,datalen+6);
871
			
872
			if (contrl.CtrlField.PRM && contrl.CtrlField.FCV)		//??FCB?ʹ???FCB
873
			{
874
				if (contrl.CtrlField.FCB == m_SubFCBFlag)
875
				{
876
					kprintf(LOG_COMM,DCV_LOG_103,LOG_WARNING,"%d???նˣ?????֡????FCB?ظ?!", rtuno);
877
					pRxBuf->Move(datalen+6);
878
					SetTxRxStatus(STATUS_RXOK);//lingws
879
					continue;
880
				}
881
				else 
882
					m_SubFCBFlag = (m_SubFCBFlag+1)%2;
883
			}
884
			
885
			if (contrl.CtrlField.PRM == 0)
886
				m_ACDFlag = contrl.CtrlField.FCB;
887
			
888
			ProcLongCmd(buf,datalen+6);			
889
			pRxBuf->Move(datalen+6);
890
			SetTxRxStatus(STATUS_RXOK);//lingws
891
			break;
892
		}
893
	}
894
	return 1;
895
}
896

    
897
void C103::ProcShortCmd(uint8 *buf,int buflen)
898
{
899
	sint32 rtuno=pLink->GetRtuNo();
900
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
901
	S103_CONTROL_BYTE contrl;
902
	contrl.BCtrlField =buf[1];
903
	
904
	if (contrl.CtrlField.PRM)//?ն??????ϴ?????·??????
905
	{
906
		//??վ??λ?ն?δ?ɹ??????????ն˸?λ??վ
907
	}
908
	else								
909
	{
910
		//?ն?Ӧ?????·??????
911
		switch (m_LinkStatus)
912
		{
913
		case	LSTATUS_INIT:	
914
			//??·????δ??ʼ??????
915
			if(contrl.CtrlField.FC==11)
916
			{
917
				//??λ??·??
918
				kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ???·״̬????֡", rtuno);
919
			}
920
			else
921
				kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ???·״̬????֡", rtuno);
922
			m_LinkStatus=LSTATUS_RESET; //lingws ?????ĶԷ???·??״ֱ̬?Ӹ?λ?Է???·
923
			break;
924
		case LSTATUS_RESET:  
925
			//Ӧ???յ???λ??·????????
926
			if(contrl.CtrlField.FC==0)
927
			{
928
				m_LinkStatus=LSTATUS_OK;
929
				kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ???λ??·????????", rtuno);
930
			}
931
			break;
932
		case LSTATUS_OK:
933
			//?ն˱???û?????????????
934
			if (contrl.CtrlField.FC == 0x09)		
935
			{
936
				m_LinkStatus = LSTATUS_OK;
937
				kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"%d???նˣ?????û?????????????", rtuno);
938
			}
939
			if (contrl.CtrlField.FC == 0x00 )
940
			{
941
				m_LinkStatus = LSTATUS_OK;
942
				kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"%d???նˣ?ң?سɹ?", rtuno);
943
			}
944
			if(contrl.CtrlField.FC == 0x20)		
945
			{
946
				m_LinkStatus = LSTATUS_OK;
947
				kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"%d???նˣ?ң?ز??ɹ?", rtuno);
948
			}
949
			break;
950
		}
951
	}
952
}
953

    
954
void C103::ProcLongCmd(uint8 *buf,int buflen)
955
{
956
	if(m_LinkStatus!=LSTATUS_OK)
957
		return;
958
	int nowtime=GetNowSecond();
959
	CSeTime	kdt;
960
	SSE_CLOCK ck;
961
	kdt.GetNow(&ck);
962
	
963
	sint32 rtuno=pLink->GetRtuNo();
964
	unsigned char rtuaddr=pRtu->GetRtuAddr(rtuno);
965
	S103_CONTROL_BYTE contrl;
966
	contrl.BCtrlField =buf[4];
967
	
968
	unsigned char func,type,sendfor;
969
	func=contrl.CtrlField.FC;
970
	type=buf[6];
971
	sendfor=buf[8];
972
	
973
	switch(type)
974
	{
975
	case 0x68: //??·????ȷ??֡
976
		kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ???·????ȷ??֡", rtuno);
977
		break;
978
	case 0x69://??λRTUȷ??
979
		m_LinkStatus=LSTATUS_INIT;
980
		kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ???λRTUȷ??֡", rtuno);
981
		break;
982
	case 0x15:
983
	case 0x09: 
984
	case 0x03:
985
		//kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ?????Ʒ?ʵ?yc֡,????ԭ??:%d", rtuno, sendfor);
986
		Recv_ProcYC(buf,buflen);
987
		break;
988
	case 8: //???ٻ?????
989
		pLink->SetCommStatus(CMST_NORMAL);
990
		kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ????ٻ?????֡", rtuno);
991
		break;
992
	case 0x01:
993
	case 0x02:
994
		{
995
			kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ???λYX֡", rtuno);
996
			Recv_ProcSOE(buf,buflen);
997
		}
998
		break;
999
	case 205:
1000
		{
1001
			kprintf(LOG_COMM,DCV_LOG_103,LOG_INFORMATION,"?յ?%d???նˣ?KWH֡",rtuno);
1002
			Recv_ProcKWH(buf,buflen);
1003
		}
1004
		break;
1005
	}
1006
}
1007

    
1008
/**************************************************************************
1009
????????ң?ź?˫??ң?ŵ?SOE
1010
DataHead ΪӦ?????ݵ?Ԫ??ʶ
1011
***************************************************************************/
1012
void C103::Recv_ProcSOE(BYTE *buf,int buflen)
1013
{
1014
	if(buflen<12)
1015
		return;
1016

    
1017
	CSeTime     SE_T;
1018
	sint32 rtuno=pLink->GetRtuNo();
1019
	BYTE TypeID=buf[6];
1020
	BYTE sq;
1021
	if((buf[7]&0x80)==0)
1022
		sq=0;
1023
	else sq=1;
1024
	BYTE number=buf[7]&0x7f;
1025
	
1026
	BYTE addr=	buf[9];
1027
	BYTE cause = buf[8];
1028
	
1029
	int lenpt=12;
1030
	uint8 fun = buf[10];
1031
	uint8 inf = buf[11];
1032

    
1033
	EVENT_INFO evt = get_event_info(fun,inf,event_table1);
1034
	if(evt.fun ==0)return;
1035

    
1036
	uint8 oldyx=0;
1037
	if(PRawCtrl->GetAYx(rtuno,evt.inx-1,&oldyx)){
1038
		if ((buf[12] == 2 && oldyx==1) || 
1039
			(buf[12] == 1 && oldyx==0) )return;//ң??û?б仯
1040
		if(evt.sort == 1){
1041
			SE_T.GetNow(&m_LastRxProtSoe);
1042
		}
1043
	}
1044
	if(buf[12] == 2)oldyx=1;
1045
	else if(buf[12] == 1)oldyx=0;
1046
	else return;
1047

    
1048
	PRawCtrl->PutAYx(rtuno,evt.inx-1,oldyx);
1049
	
1050
	SSE_CLOCK systime;
1051
	SE_T.GetNow(&systime);
1052
	S_RAWSOE rawsoe;
1053
	rawsoe.Source=DC_K_RTUSOE;
1054
	rawsoe.Rtuno=rtuno;
1055
	rawsoe.Year=systime.year;
1056
	rawsoe.Month=systime.month;
1057
	rawsoe.Day=systime.day;
1058
	rawsoe.Yxno=evt.inx-1;
1059
	rawsoe.Val=oldyx;
1060
	rawsoe.Hour=buf[16]&0x1f;
1061
	rawsoe.Minute=buf[15]&0x3f;
1062
	rawsoe.Second=(buf[13]*256+buf[14])/1000;
1063
	rawsoe.Ms=(buf[13]*256+buf[14])%1000;
1064
	PRawCtrl->PutASoe(rawsoe);
1065
}
1066

    
1067

    
1068
/**********************************************************************
1069
ң?⴦?????̣?
1070
***********************************************************************/
1071
void C103::Recv_ProcYC(BYTE *buf,int buflen)
1072
{
1073
	if(buflen<12)
1074
		return;
1075
	sint32 rtuno=pLink->GetRtuNo();
1076
	BYTE TypeID=buf[6];
1077
	BYTE sq;
1078
	if((buf[7]&0x80)==0)
1079
		sq=0;
1080
	else sq=1;
1081

    
1082
	BYTE number=buf[7]&0x7f;//vsq
1083
	BYTE addr=buf[9];
1084
	BYTE cause = buf[8];//cot
1085
	BYTE inf = buf[11];
1086

    
1087
	if( (TypeID==9 && inf==0x89) ||
1088
		TypeID==3)
1089
	{
1090
		int lenpt;
1091
		short int YcValue;
1092
		lenpt= 12;
1093
		for(int i=0;i<number;i++){
1094
			YcValue=CalcYcShortVal(buf[lenpt],buf[lenpt+1]);
1095
			lenpt++;lenpt++;
1096
			PRawCtrl->PutAYc(rtuno,i,YcValue);
1097
		}
1098
	}
1099
}
1100

    
1101
/**************************************************************
1102
??ȴ???
1103
**************************************************************/
1104
void C103::Recv_ProcKWH(BYTE *buf,int buflen)
1105
{
1106
	if(buflen<16)
1107
		return;
1108
	sint32 rtuno=pLink->GetRtuNo();
1109
	BYTE cause = buf[8];
1110
	uint8 KwhValue[4];
1111
	uint8 index= buf[11]- 0x33;
1112
	if(index>=6)return;
1113
	memcpy(KwhValue, buf+12, 4);
1114
	 
1115
	uint32 val = (((KwhValue[3]*256+KwhValue[2])*256)+KwhValue[1])*256+KwhValue[0];
1116
	val = val&0x0fffffff;
1117
	PRawCtrl->PutAKwh(rtuno,index,val);
1118
}
1119

    
1120
/************************************************************************************************
1121
????·?????????·???
1122
*************************************************************************************************/
1123
void C103::ReSendData()
1124
{
1125
	int rtuno=pLink->GetRtuNo();
1126
	SendToPhysical(TRUE);
1127
	kprintf(LOG_COMM,DCV_LOG_103, LOG_WARNING, "%d???նˣ????·???????????", rtuno);
1128
}
1129

    
1130
/***********************************************************************************************
1131
103??ԼУ???????
1132
Buff?Ǵ????㻺????????ʼ??ַ??Len?dz???
1133
***********************************************************************************************/
1134
unsigned char C103::GetByteCheckSum(unsigned char *buf,int buflen)
1135
{
1136
	unsigned char result = 0;
1137
	int i;
1138
	for (i=0;i<buflen;i++) 
1139
		result +=buf[i];
1140
	return	result;
1141
}
1142

    
1143
int C103::GetNowSecond()
1144
{
1145
	CSeTime     SE_T;
1146
	TCriterionTime tmptime;
1147
	SE_T.GetNow(&tmptime);
1148
	return (int)tmptime;
1149
}
1150

    
1151
//˫?ֽ?Ycֵ????
1152
short int C103::CalcYcShortVal(BYTE LowByte,BYTE HighByte)
1153
{
1154
	short int YcValue=0;
1155
	if((LowByte&0x01) || (LowByte&0x02)){
1156
		YcValue = -1;
1157
		return -1;
1158
	}
1159
	
1160
	YcValue = LowByte+HighByte*256;
1161
	YcValue>>=3;
1162
	if(YcValue&0x1000){
1163
		YcValue= ~(YcValue&0x0fff);
1164
		YcValue= YcValue&0x0fff;
1165
		YcValue= YcValue*(-1);
1166
	}
1167
	return YcValue;
1168
}
1169

    
1170
extern "C" __declspec(dllexport) 
1171
CProtocol	*CreateProtocol(char *defpara)
1172
{
1173
	return (CProtocol*)(new C103());
1174
}
(1-1/3)