Project

General

Profile

Bug #1348 » SE102c.cpp

yufeng wu, 04/20/2021 04:33 PM

 
1
#include "SE102c.h"
2
#pragma pack(1)
3
/*
4
CSE102c::CSE102c()
5
{
6
}
7

    
8

    
9
CSE102c::~CSE102c()
10
{
11
}
12
*/
13
void CSE102c::Init(S_PROTOCOLCFG * pcfg)
14
{
15
	PRawCtrl = pcfg->PRawCtrl;
16
	pRxBuf = pcfg->pRxBuf;
17
	pTxBuf = pcfg->pTxBuf;
18
	pCmdMem = pcfg->pCmdMem;
19
	pRtu = pcfg->pRtu;
20
	pLink = pcfg->pLink;
21
	pTable = pcfg->pTable;
22
	pZfDataCtrl = pcfg->pZfDataCtrl;
23

    
24
	InitStation();
25
}
26

    
27
void CSE102c::InitStation()
28
{
29
	sint32 rtuno = pLink->GetRtuNo();
30
	int buflen = pRxBuf->GetReadableSize();
31
	pRxBuf->Move(buflen);
32
	buflen = pTxBuf->GetReadableSize();
33
	pTxBuf->Move(buflen);
34

    
35
	LostNum = 0;
36
	int curtime = GetNowSecond();
37
	cmdType = 0;
38
	LastTxTime = curtime;
39
	LastRxTime = curtime;
40
	LastCallkwhTime = curtime;
41
	LastSyncTime = curtime;
42
	AppLayerStat = IEC102_APP_STATE_WAITSTART;
43
	SendSeqNo = 0;
44
	ReceiveSeqNo = 0;
45
	ConSeqNo = 0;
46
	ResendCount = 0;
47
	ResendCountLimit = pLink->GetRetryTimes();
48
	//CallAllDataFlag = 1;
49
	//<xyanglee>
50
	CallKwhDataFlag = 0;
51
	Timeout2 = 1;
52
	Timeout3 = 15;
53
	RecordTime = 0;
54

    
55
	RtuTimeFlag = 0;
56
	SendACKFlag = 0;
57
	pLink->SetCommStatus(CMST_NORMAL);
58

    
59
	LastCmdNeedRet = 0;
60
}
61

    
62
int CSE102c::SendStartFormat()
63
{
64
	uint8* ptr = senddata.databuf;
65
	static int start = 0;
66
	senddata.headP = senddata.trailP = 0;
67

    
68
	int curtime = GetNowSecond();
69
	if (curtime - LastTxTime < Timeout2 ||
70
		curtime - LastRxTime < Timeout2)
71
	{
72
		return -1;
73
	}
74
	*ptr++ = 0x10;
75
	*ptr++ = 0x49;
76
	*ptr++ = 0x01;
77
	*ptr++ = 0x00;
78
	*ptr++ = 0x4a;
79
	*ptr++ = 0x16;
80
	senddata.trailP = ptr - senddata.databuf;
81
	AppToLink();
82

    
83
	PrintLog(LOG_INFORMATION, "????start????????! ???:%d",++start);
84
	//if (AppLayerStat == IEC102_APP_STATE_IDLE) 		//????״̬?²???ˢ??LastTxTime??
85
		LastTxTime = curtime;
86

    
87
	return 1;
88
}
89

    
90
int CSE102c::SendResetFormat()
91
{
92
	uint8* ptr = senddata.databuf;
93

    
94
	senddata.headP = senddata.trailP = 0;
95

    
96
	int curtime = GetNowSecond();
97
	if (curtime - LastTxTime < Timeout2 ||
98
		curtime - LastRxTime < Timeout2)
99
	{
100
		return -1;
101
	}
102
	*ptr++ = 0x10;
103
	*ptr++ = 0x40;
104
	*ptr++ = 0x01;
105
	*ptr++ = 0x00;
106
	*ptr++ = 0x41;
107
	*ptr++ = 0x16;
108
	senddata.trailP = ptr - senddata.databuf;
109
	AppToLink();
110

    
111

    
112
//	if (AppLayerStat == IEC102_APP_STATE_IDLE) 		//????״̬?²???ˢ??LastTxTime??
113
		LastTxTime = curtime;
114
		PrintLog(LOG_INFORMATION, "???͸?λ????????! ");
115
	return 1;
116
}
117

    
118
void CSE102c::CallKwhData(short endAddr, short startAddr)
119
{
120
	static int callKwh = 0;
121
	int curtime = GetNowSecond();
122
	if (curtime - LastTxTime < Timeout2 ||
123
		curtime - LastRxTime < Timeout2)
124
	{
125
		//PrintLog(LOG_INFORMATION, "???͵??????ʧ??! ");
126
		return ;
127
	}
128
	RecordTime = GetNowSecond();
129
	int rtuno = pLink->GetRtuNo();
130
	uint16 addr = pRtu->GetRtuAddr(rtuno);
131

    
132
	uint8		*Buff = senddata.databuf;
133
	DC_D_DATAUNITID  DataHead;
134
	senddata.headP = senddata.trailP = 0;
135
	
136
	DataHead.TypeID = C_CI_ZJ_2;
137
	DataHead.VSQ.Num = 1;
138
	DataHead.VSQ.SQ = 0;
139
	DataHead.Cause = APP_COT_ACT;
140
	DataHead.CommonAddrLo = 0x01;
141
	DataHead.CommonAddrHi = 0x00;
142
	DataHead.RadAddr = 0x9f;
143
	*Buff++ = 0x68;
144
	*Buff++ = 0x0D;
145
	*Buff++ = 0x0D;
146
	int dataLen = 13;
147
	*Buff++ = 0x68;
148
	uint8* ptr = Buff;
149
	short tempJiaoyan = 0;
150
	*Buff++ = 0x53;
151
	*Buff++ = 0x01;
152
	*Buff++ = 0x00;
153
	memcpy(Buff, (uint8*)&DataHead, sizeof(DC_D_DATAUNITID));
154
	Buff += sizeof(DC_D_DATAUNITID);
155

    
156
//ʵʱ???ݵ?ַ??Χ
157
	/**Buff++ = 0x00;
158
	*Buff++ = 0x00;
159
	*Buff++ = 0xD9; //
160
	*Buff++ = 0x00;*/
161
	uint8 startL = startAddr & 0x00ff;
162
	uint8 startH = (startAddr & 0xff00) >> 8;
163
	uint8 endL = endAddr & 0x00ff;
164
	uint8 endH = (endAddr & 0xff00) >> 8;
165
	*Buff++ = startL;
166
	*Buff++ = startH;
167
	*Buff++ = endL; //
168
	*Buff++ = endH;
169
	PrintLog(LOG_INFORMATION, ">>>>>> ʵʱ????ٻ????? startAddr:%d ,endAddr:%d ,startL:%d, endL:%d ", startAddr,endAddr,startL,endL);
170
	for (int i = 0; i < dataLen; ++i)
171
	{
172
		tempJiaoyan += *(ptr++);
173
	}
174
	*Buff++ = tempJiaoyan & 0x00ff;
175
	*Buff++ = 0x16;
176
	senddata.trailP = Buff - senddata.databuf;
177
	AppToLink();
178
	AppLayerStat = IEC102_APP_STATE_WAITALLKWHCONF;
179
	Timeout3 = Timeout2;
180
	PrintLog(LOG_INFORMATION, ">>>>>> ????ʵʱ????ٻ????? ???:%d",++callKwh);
181

    
182
	//ÿ????һI֡????SendSeqNo??1???巢??ȷ?ϱ?־SendACKFlag??
183
	//SendSeqNo = (SendSeqNo + 1) % 0x8000;
184
	//SendACKFlag = 0;
185
	LastTxTime = GetNowSecond();
186
	
187
//	LastCmdNeedRet = LASTCALL_ACCDATA;
188
}
189

    
190
void CSE102c::App_CheckState()
191
{
192
	int curtime = GetNowSecond();
193
	sint32 rtuno = pLink->GetRtuNo();
194
	short endAddr = pRtu->GetKwhNum(rtuno);
195
	int secnum;
196
	switch (AppLayerStat)
197
	{
198
	case IEC102_APP_STATE_IDLE:
199
		break;
200
	case IEC102_APP_STATE_WAITSTART:
201
		SendStartFormat();
202
		break;
203
	case IEC102_APP_STATE_UNRESET:
204
	//	PrintLog(LOG_INFORMATION, "???͸?λ????????! ");
205
		SendResetFormat();
206
		break;
207
	case IEC102_APP_STATE_WAITTIMECONF:
208
	//	PrintLog(LOG_INFORMATION, "?????ٻ?????????????! ");
209
		Call5KwhData();;
210
		break;
211
	case IEC102_APP_STATE_WAITALLKWHCONF:
212
	//	PrintLog(LOG_INFORMATION, "?????ٻ?????????????! ");
213
		Call7KwhData();
214
		break;
215
	case IEC102_APP_STATE_WAITGRPDATA:
216
	//	PrintLog(LOG_INFORMATION, "?????ٻ???ʷ????????! ");
217
		SendHidataToSubstation();;
218
		break;
219
	case IEC102_APP_STATE_WAITGRPKWH:		
220
		CallKwhData(endAddr);
221
		break;
222
	case IEC102_APP_STATE_WAITALLKWH:
223
		if (curtime - LastTxTime >30)
224
		{
225
			AppLayerStat = IEC102_APP_STATE_IDLE;
226
			CallKwhDataFlag = 0;
227
		}
228
		break;
229
	default:
230
		PrintLog(LOG_INFORMATION, "IEC104 Invalid primary app state ");
231
		break;
232
	}
233
}
234

    
235
/*??Ƚ??մ???
236
*/
237
void CSE102c::App_ReceiveProc()
238
{
239
	sint32 rtuno = pLink->GetRtuNo();
240
	uint8 * ptr = recvdata.databuf;
241
	//sint32 KwhValue;
242
	//uint16 index = 0;
243
	DC_D_DATAUNITID * pDataHead = (DC_D_DATAUNITID *)recvdata.databuf;
244
	uint8 TypeID = pDataHead->TypeID;
245
	uint8 number = pDataHead->VSQ.Num;
246
	uint8 sq = pDataHead->VSQ.SQ;
247

    
248
	switch (TypeID)
249
	{
250
	case M_IT_TA_2:
251
	//case M_SP_TA_2:
252
		RecordTime = GetNowSecond();
253
		PrintLog(LOG_INFORMATION, "?յ?KWH֡ ????%d ", TypeID);
254
		App_KwhProc();
255
		break;
256
	default:
257
		PrintLog(LOG_INFORMATION, "?յ?û?н??͵ı??? ????%d ", TypeID);
258
		break;
259
	}
260
	recvdata.headP = recvdata.trailP = 0;
261

    
262
}
263

    
264
int  CSE102c::App_PipeProc()
265
{
266
	sint32 rtuno = pLink->GetRtuNo();
267

    
268
	//if (pCmdMem->GetCmdNum(rtuno) <= 0)
269
	//	return FALSE;
270

    
271
	S_RAWCMD rawcmd;
272

    
273
	//if (pCmdMem->GetACmd(rtuno, &rawcmd) == 0)
274
	//	return FALSE;
275
	//if (rawcmd.type == DC_K_CMD_SYNCTIME)
276
	{
277
		//??̨????ʱ????
278
		RtuTimeFlag = 1;
279
		pCmdMem->DelACmd(rtuno);
280
		return FALSE;
281
	}
282

    
283
	/*{
284
		pCmdMem->DelACmd(rtuno);
285
		return FALSE;
286
	}
287

    
288
	return FALSE*/;
289
}
290

    
291
void CSE102c::App_KwhProc()
292
{
293
	sint32 rtuno = pLink->GetRtuNo();
294
	uint8 * Buff = recvdata.databuf;
295
	DC_D_DATAUNITID * pDataHead = (DC_D_DATAUNITID *)recvdata.databuf;
296
	uint8 TypeID = pDataHead->TypeID;
297
	uint8 number = pDataHead->VSQ.Num;
298
	uint8 sq = pDataHead->VSQ.SQ;
299

    
300
	sint32 KwhValue;
301
	uint8 Qual;
302
	int i;
303
	uint16 index = *(Buff + DC_D_HEADLEN ) + ( *(Buff + DC_D_HEADLEN + 1) << 8);
304
	PrintLog(LOG_INFORMATION, "?յ???ȱ??? ????%d ", index);
305
	//ע?⣬???ֵ?ֽ???????
306
	switch (TypeID)
307
	{
308
	case M_IT_TA_2://2:???
309
		for (i = 0; i<number; i++)
310
		{
311
			/*if (recvdata.headP >= recvdata.trailP)
312
			{
313
				PrintLog(LOG_INFORMATION, "?????Ϣ????Ŀ?쳣  ");
314
				break;
315
			}*/
316
			uint8 tempInt = *(Buff + DC_D_HEADLEN + 2);
317
			uint8 firstValue = *(Buff + DC_D_HEADLEN + i * DC_D_INFOADDRLEN + 2);
318
			uint8 tempSe = *(Buff + DC_D_HEADLEN + i * DC_D_INFOADDRLEN + 3);
319
			sint32 tempSecond = tempSe << 8;
320
			sint32 secondValue = (*(Buff + DC_D_HEADLEN + i * DC_D_INFOADDRLEN + 3)) << 8;
321
			sint32 thirdValue = ((*(Buff + DC_D_HEADLEN + i * DC_D_INFOADDRLEN + 4)) << 8) << 8;
322
			sint32 fourValue =  (((*(Buff + DC_D_HEADLEN + i * DC_D_INFOADDRLEN + 5)) << 8) << 8 )<< 8;
323
			KwhValue =  firstValue + secondValue + thirdValue + fourValue;
324
			if (index > 216)
325
			{
326
				RecordTime = GetNowSecond();
327
			}
328
			PrintLog(LOG_INFORMATION, "?յ???ȱ??? ????%d ??ȣ?%d", index, KwhValue);
329
			PRawCtrl->PutAKwh(rtuno, index, KwhValue);
330
			
331
			//recvdata.headP += 5;
332
			if (sq)
333
			{
334
				index++;
335
			}
336
			else
337
			{
338
				index = *(Buff + DC_D_HEADLEN + (i + 1)* DC_D_INFOADDRLEN) + (*(Buff + DC_D_HEADLEN + 1 + (i + 1)* DC_D_INFOADDRLEN) << 8);
339
				
340
			}
341
		}
342
		break;
343
	case M_SP_TA_2://1:??ʱ??ĵ??
344
		/*CP56Time2a tmTime;
345
		for (i = 0; i<number; i++)
346
		{
347
			if (recvdata.headP >= recvdata.trailP)
348
			{
349
				PrintLog(LOG_INFORMATION, "??ʱ??????Ϣ????Ŀ?쳣  ");
350
				break;
351
			}
352

    
353
#ifdef NET_BYTEORDER
354
			KwhValue = (*(Buff + recvdata.headP + 3)) << 24 + (*(Buff + recvdata.headP + 2)) << 16
355
				+ (*(Buff + recvdata.headP + 1)) << 8 + *(Buff + recvdata.headP);
356
#else
357
			KwhValue = *((sint32 *)(Buff + recvdata.headP));
358
#endif
359
			Qual = *(Buff + recvdata.headP + 4);
360
			recvdata.headP += 5;
361
			memcpy((uint8 *)&tmTime, Buff + recvdata.headP, sizeof(CP56Time2a));
362
			if ((Qual & 0x80) == 0)
363
			{
364
				PRawCtrl->PutAKwh(rtuno, index, KwhValue);
365
			}
366
			recvdata.headP += sizeof(CP56Time2a);
367
			index = (*(Buff + recvdata.headP) + (*(Buff + recvdata.headP + 1) * 256));
368
			recvdata.headP += DC_D_INFOADDRLEN;
369
		}*/
370
		break;
371
	default:
372
		PrintLog(LOG_INFORMATION, "?յ?û?н??͵ı??? ????%d ", TypeID);
373
	}
374

    
375
}
376

    
377
//????: ????Ƿ????ٻ?ȫ???ݡ???ȣ?ͬ??ʱ?ӣ???????Ӧ??־.
378
void CSE102c::App_CheckTime()
379
{
380
	int curtime = GetNowSecond();
381
	if (pLink->GetKwhScanInterval()>0)
382
	{
383
		if (curtime - LastCallkwhTime >= pLink->GetKwhScanInterval() * 60)
384
		{
385
			CallKwhDataFlag = 1;
386
			LastCallkwhTime = curtime;
387
		}
388
	}
389
	int spacetime = pRtu->GetSyncTimeInt(pLink->GetRtuNo());
390
	if (spacetime>0)
391
	{
392
		if (int(curtime - LastSyncTime) >= spacetime * 60)
393
		{
394
			RtuTimeFlag = 1;
395
			LastSyncTime = curtime;
396
		}
397
	}
398
}
399

    
400
int CSE102c::GetNowSecond()
401
{
402
	CSeTime     SE_T;
403
	TCriterionTime tmptime;
404
	SE_T.GetNow(&tmptime);
405
	return (int)tmptime;
406
}
407

    
408
void CSE102c::App_RxFixFrame(uint8 *buf, int buflen)
409
{
410
	sint32 rtuno = pLink->GetRtuNo();
411
	pLink->RegisterFrm(FRAME_RX_SUC);
412
	if (buf[1] == 0x0b)
413
	{
414
		PrintLog(LOG_INFORMATION, "?յ?????ȷ??֡ ");
415
		SendSeqNo = 0;
416
		ReceiveSeqNo = 0;
417

    
418
		AppLayerStat = IEC102_APP_STATE_UNRESET;
419
		//SendResetFormat();
420
	}
421
	else if (buf[1] == 0x00)
422
	{
423
		PrintLog(LOG_INFORMATION, "?յ???λȷ??֡ ");
424
		SendSeqNo = 0;
425
		ReceiveSeqNo = 0;
426
		SendACKFlag = 1;
427
		AppLayerStat = IEC102_APP_STATE_IDLE;
428
	}
429
	else
430
	{
431
		PrintLog(LOG_INFORMATION, "?յ?δ֪֡ ");
432
	}
433
	pLink->RegisterFrmCode(RAW_CODEDIR_UP, (char *)buf, 6);
434
	pRxBuf->Move(buflen);
435
}
436

    
437
int	CSE102c::App_RxVarFrame(uint8 *buf, int buflen)
438
{
439
	unsigned char initflag = 0;
440
	sint32 rtuno = pLink->GetRtuNo();
441
	int time = GetNowSecond();
442
	int tempInt = time - RecordTime;
443
	int DataLen = buf[1];
444
	ReceiveSeqNo++;		//??ŵ??ǵ?ǰ?????к?
445
	memcpy(recvdata.databuf, buf + 7, DataLen - 3);
446
	//recvdata.trailP = DataLen + 2;
447
	//recvdata.headP = 0;
448
	if (buf[7] == 0x80)
449
	{
450
		PrintLog(LOG_INFORMATION, "?յ???ʱȷ??֡ ");
451
		RecordTime = GetNowSecond();
452
		AppLayerStat = IEC102_APP_STATE_WAITGRPKWH;
453
	}
454
	else if (buf[7] == 0x02)
455
	{
456
		static int sendID = 0;
457
		if (AppLayerStat == IEC102_APP_STATE_WAITTIMECONF || AppLayerStat == IEC102_APP_STATE_WAITGRPKWH)
458
			AppLayerStat = IEC102_APP_STATE_WAITALLKWHCONF;
459
		else if (AppLayerStat == IEC102_APP_STATE_WAITALLKWHCONF || AppLayerStat == IEC102_APP_STATE_WAITGRPDATA)
460
			AppLayerStat = IEC102_APP_STATE_WAITTIMECONF;
461
		App_ReceiveProc();//???մ???
462
		
463
		//if (/*(tempInt) % 30 == 0 || tempInt % 12 == 0 || tempInt % 13 == 0 || */tempInt % 12 == 0) //???ַ???ʽ
464
		{
465
		//	AppLayerStat = IEC102_APP_STATE_WAITGRPKWH;
466
		//	PrintLog(LOG_INFORMATION, "?յ????????ݵı??? ????ʵʱ???????%d ", ++sendID);
467
		}
468
		//if ((tempInt) % 1200 == 0) //???ַ???ʽ
469
		{
470
			//AppLayerStat = IEC102_APP_STATE_WAITGRPDATA;
471
		}
472
	}
473
	else
474
	{
475
		PrintLog(LOG_INFORMATION, "?յ??ɱ䳤δ֪ȷ??֡ typeID:%d",buf[7]);
476
	}
477
	*buf++ = 4 + DataLen + 2;
478
	SendACKFlag++;
479
	
480

    
481
	pLink->RegisterFrm(FRAME_RX_SUC);
482
	pLink->RegisterFrmCode(RAW_CODEDIR_UP, (char *)buf, DataLen + 6);
483
	pRxBuf->Move(DataLen + 6);
484
	return 1;
485
}
486

    
487
sint32	CSE102c::RxProc()
488
{
489
	uint8  buf[512];
490
	int buflen = pRxBuf->GetReadableSize();
491
	if (buflen <= 0)
492
	{
493
		//PrintLog(LOG_INFORMATION, "?յ?????֡??ʽ????");
494
		return -1;
495
	}
496
	int nRet = 0;
497
	sint32 datalen = 0;
498
	LastRxTime = GetNowSecond();				//?յ?֡??ˢ?½??ռ?ʱ??
499
	int tempInt = LastRxTime - RecordTime;
500
	if (buflen == 1)
501
	{
502
		Timeout3 = 15;
503
		//PrintLog(LOG_INFORMATION, "?յ????ַ?ȷ??֡ ??buflen:%d,???ͺ?:%d", buflen, SendSeqNo);
504
		if (AppLayerStat == IEC102_APP_STATE_WAITTIMECONF || AppLayerStat == IEC102_APP_STATE_WAITGRPKWH)
505
			AppLayerStat = IEC102_APP_STATE_WAITALLKWHCONF;
506
		else if (AppLayerStat == IEC102_APP_STATE_WAITALLKWHCONF || AppLayerStat == IEC102_APP_STATE_WAITGRPDATA)
507
			AppLayerStat = IEC102_APP_STATE_WAITTIMECONF;
508
		//if (tempInt > 300)
509

    
510
		if (tempInt > 300) //?ٻ?ʵʱ???ݼ??5????
511
		{
512
			static int sendAID = 0;
513
			AppLayerStat = IEC102_APP_STATE_WAITGRPKWH;
514
			PrintLog(LOG_INFORMATION, "<<<<<<<<<?յ????ֽڵı??? ????ʵʱ???????%d ʱ??%d", ++sendAID,tempInt);
515
		}
516
		//if ((tempInt) % 1200 == 0) //???ַ???ʽ
517
		//{
518
		//	AppLayerStat = IEC102_APP_STATE_WAITGRPDATA;
519
		//}
520
		pLink->RegisterFrmCode(RAW_CODEDIR_UP, (char *)buf, buflen);
521
		pRxBuf->Move(buflen);
522
		
523
	}
524
	else
525
	{
526
		while ((buflen = pRxBuf->GetReadableSize()) >= 6 )
527
		{
528
			if (buflen > 512)
529
				buflen = 512;
530
			datalen = pRxBuf->Read(buf, buflen, DEF_BUFF_NOMOVE);
531
			if (buf[0] != 0x68 && buf[0] != 0x10 )
532
			{
533
				sint32 unuse = 0;
534
				while (unuse < datalen)
535
				{
536
					if (buf[unuse] != 0x68 && buf[unuse] != 0x10)
537
						unuse++;
538
					else
539
					{
540
						break;
541
					}
542
				}
543
				pRxBuf->Move(unuse);
544
				continue;
545
			}
546
			
547
			if (buf[0] == 0x68) //?ɱ?֡֡??ʽ
548
			{
549
				nRet = App_RxVarFrame(buf, buflen);
550
				if (!nRet)
551
					break;
552
			}
553
			else if (buf[0] == 0x10) //?̶?֡֡??ʽ
554
			{
555
				App_RxFixFrame(buf, buflen);
556
				break;
557
			}			
558
			else
559
			{
560

    
561
			}
562
		}
563
	}
564
	return 1;
565
}
566

    
567
void CSE102c::AppToLink()
568
{
569
	int curtime = GetNowSecond();
570
	/*if (curtime - LastTxTime < Timeout2 ||
571
		curtime - LastRxTime < Timeout2)
572
	{
573
		return;
574
	}*/
575
	uint8 * ptr = senddata.databuf;
576
	int len = senddata.trailP;
577

    
578
	pLink->RegisterFrmCode(RAW_CODEDIR_DOWN, (char *)ptr, len);
579
	pTxBuf->Write(ptr, len);
580
	PrintLog(LOG_INFORMATION, ">>>>>> ????????success ");
581
}
582

    
583
void CSE102c::SendTimeToSubstation()
584
{
585
	int rtuno = pLink->GetRtuNo();
586
	uint16 addr = pRtu->GetRtuAddr(rtuno);
587
	int curtime = GetNowSecond();
588
	if (curtime - LastTxTime < Timeout2 || curtime - LastRxTime < Timeout2)
589
	{
590
		return;
591
	}
592
	CP56Time2b	T_102;
593
	memset(&T_102,0,sizeof(T_102));
594
	DC_D_DATAUNITID  DataHead;
595
	uint8		*Buff = senddata.databuf;
596
	senddata.headP = senddata.trailP = 0;
597
	uint16  SendNum, RevNum;
598
	SendNum = SendSeqNo << 1;
599
	RevNum = ReceiveSeqNo << 1;
600

    
601
	DataHead.TypeID = C_SYN_TA_2;
602
	DataHead.VSQ.Num = 1;
603
	DataHead.VSQ.SQ = 0;
604
	DataHead.Cause = APP_COT_ACT;
605
	DataHead.RadAddr = 0x00;
606
	DataHead.CommonAddrLo = 0x01;//addr % 256;
607
	DataHead.CommonAddrHi = 0x00;//addr / 256;
608
	const uint8 sendLen = 16;
609
	*Buff++ = 0x68;
610
	*Buff++ = 0x10;
611
	*Buff++ = 0x10;
612
	*Buff++ = 0x68;
613
	uint8* ptr = Buff;
614
	*Buff++ = 0x73;
615
	*Buff++ = 0x01;
616
	*Buff++ = 0x00;
617

    
618
	//*Buff++ = 4 + DC_D_HEADLEN + 3 + sizeof(CP56Time2b);
619
	
620
	memcpy(Buff, (uint8*)&DataHead, DC_D_HEADLEN);
621
	Buff += DC_D_HEADLEN;
622

    
623
	//*Buff++ = 0;
624
	//*Buff++ = 0;
625
	//*Buff++ = 0;	//InfoAddr 3 byte
626

    
627

    
628
	SSE_CLOCK	systime;
629
	CSeTime     SE_T;
630
	SE_T.GetNow(&systime);
631

    
632
	T_102.MinuteInfo.minute = systime.minute;
633
	T_102.HourInfo.hour = systime.hour;
634
	T_102.DayInfo.dfw = systime.day;
635
	//T_101.DaysWeeks.Weeks = systime.wday;
636
	T_102.MonthInfo.month = systime.month;
637
	T_102.YearInfo.year = (systime.year - 2000);
638
	T_102.SecondStruct.mileSecond = systime.msecond;
639
	T_102.SecondStruct.second = systime.second;
640
	uint16 tempT = 0;
641
	uint8 tempP = *ptr;
642
	uint8 tempS = *(ptr + 1);
643
	uint8 tempB = *(ptr + 2);
644
	uint8 tempC = *(ptr + 6);
645
	/*for (int i = 0; i < sendLen; ++i)
646
	{
647
		tempT += *(ptr + i);
648
	}*/
649
	memcpy(Buff, (uint8*)&T_102, sizeof(CP56Time2b) -1);
650
	uint8 tempF = *(ptr + 9);
651
	tempT = 0x74 + 0x88 + *(ptr + 9) + *(ptr + 10) + *(ptr + 11) + *(ptr + 12) + *(ptr + 13) + *(ptr + 14) + *(ptr + 15);
652
	uint8 tempE = *(ptr + 15);
653
	T_102.jiaoYan = tempT & 0x00ff;
654
	
655
	uint8 timeLen = sizeof(CP56Time2b);
656
	Buff += (timeLen -1);
657
	*Buff++ = T_102.jiaoYan;
658
	*Buff++ = 0x16;
659

    
660
	senddata.trailP = Buff - senddata.databuf;
661
	AppToLink();
662

    
663
	//SendSeqNo = (SendSeqNo + 1) % 0x8000;
664
	SendACKFlag = 0;
665

    
666
	LastTxTime = GetNowSecond();;
667
	AppLayerStat = IEC102_APP_STATE_WAITTIMECONF;
668
	RecordTime = GetNowSecond();
669
	PrintLog(LOG_INFORMATION, ">>>>>> ???Ͷ?ʱ???? ");
670
	LastCmdNeedRet = LASTCALL_SETTIME;
671
}
672

    
673
int CSE102c::SendHidataToSubstation(/*CP40Time2a &time_start, CP40Time2a &time_end, short addr_end, short addr_start*/)
674
{
675
	uint8* buff = senddata.databuf;
676
	uint8* ptr;
677
	CP40Time2a	T_102_s,T_102_e;
678
	memset(&T_102_s,0,sizeof(T_102_s));
679
	memset(&T_102_e, 0, sizeof(T_102_e));
680
	DC_D_DATAUNITID  DataHead;
681
	senddata.headP = senddata.trailP = 0;
682
	SSE_CLOCK	systime;
683
	CSeTime     SE_T;
684
	SE_T.GetNow(&systime);
685
	int curtime = GetNowSecond();
686
	if (curtime - LastTxTime < Timeout3 ||
687
		curtime - LastRxTime < Timeout3)
688
	{
689
		return -1;
690
	}
691
	*buff++ = 0x68;
692
	*buff++ = 0x17;
693
	*buff++ = 0x17;
694
	*buff++ = 0x68;
695
	*buff++ = 0x53;
696
	*buff++ = 0x01;
697
	*buff++ = 0x00;
698
	DataHead.TypeID = C_CI_NR_2;
699
	DataHead.VSQ.Num = 1;
700
	DataHead.VSQ.SQ = 0;
701
	DataHead.Cause = APP_COT_ACT;
702
	DataHead.RadAddr = 0x9f;
703
	DataHead.CommonAddrLo = 0x01;//addr % 256;
704
	DataHead.CommonAddrHi = 0x00;
705
	memcpy(buff, (uint8*)&DataHead, DC_D_HEADLEN);
706
	buff += DC_D_HEADLEN;
707
	ptr = buff;
708
	//??Ϣ???ַ
709
	*buff++ = 0x00;
710
	*buff++ = 0x00;
711
	*buff++ = 0xDC;
712
	*buff++ = 0x00;
713
	
714
	T_102_s.MinuteInfo.minute = systime.minute;
715
	T_102_s.HourInfo.hour = systime.hour;
716
	T_102_s.YearInfo.year = systime.year;
717
	T_102_s.DayInfo.dfm = systime.day - 7;
718
	T_102_e.MinuteInfo.minute = systime.minute;
719
	T_102_e.HourInfo.hour = systime.hour;
720
	T_102_e.YearInfo.year = systime.year;
721
	T_102_e.DayInfo.dfm = systime.day;
722
	uint8 T2aLen = sizeof(CP40Time2a);
723
	memcpy(buff, (uint8*)&T_102_s, sizeof(CP40Time2a));
724
	buff += sizeof(CP40Time2a);
725
	memcpy(buff, (uint8*)&T_102_e, sizeof(CP40Time2a));
726
	buff += sizeof(CP40Time2a);
727
	uint8 tempPtr = *(ptr - 1);
728
	uint8 tempA = *(ptr + 2);
729
	uint8 tempE = *(ptr + 9);
730
	uint16 tempBuff = 0;
731
	const int TimeLen = 14;
732
	tempBuff = 0x53 + 0x01 + 0x78 + 0x01 + 0x06 + 0x01 + 0x9f;// 
733
	for (int i = 0; i < TimeLen; ++i)
734
	{
735
		tempBuff += *(ptr + i);
736
	}
737
	*buff++ = tempBuff & 0x00ff;
738
	*buff++ = 0x16;
739
	senddata.trailP = buff - senddata.databuf;
740

    
741
	AppToLink();
742

    
743
	PrintLog(LOG_INFORMATION, ">>>>>> ??????????ʷ???????? ");
744
//	if (AppLayerStat == IEC102_APP_STATE_IDLE) 		//????״̬?²???ˢ??LastTxTime??
745
		LastTxTime = curtime;
746

    
747
	return 1;
748
}
749

    
750
sint32 CSE102c::TxProc()
751
{
752
	App_CheckTime();
753
	if (AppLayerStat == IEC102_APP_STATE_IDLE)//??һ?????ж?
754
	{
755
		ResendCount = 0;//???ϴ??ط????ط?????????ֵ
756

    
757

    
758
		if (App_PipeProc())
759
		{
760

    
761
			//ofstream outfile;
762
			//outfile.open("changetime.txt", ios::app);
763
			//outfile << "se104c??Լ????   ??" << endl;
764
			//outfile << "AppLayerStat   ??" << AppLayerStat << endl;
765
			//outfile << "ʱ???   ??" << log_Time() << endl;
766
			//outfile << "??ɣ?" << endl << endl;
767
			//outfile.close();
768

    
769
			return 1;
770
		}
771
		if (RtuTimeFlag)
772
		{
773
			//ʱ??ͬ??????
774
			SendTimeToSubstation();
775
			RtuTimeFlag = 0;
776
			return 1;
777
		}
778

    
779
		if (CallKwhDataFlag)
780
		{
781
			//??????ٻ?????
782
			if (CallKwhDataFlag == 5) //
783
				Call5KwhData();//CallKwhData(QCC_CALLALLKWH | 0x40);
784
			else if (CallKwhDataFlag == 7)	//??ȡ
785
				Call7KwhData();//CallKwhData(QCC_CALLALLKWH);
786

    
787
			//			CallKwhDataFlag=0;
788
			return 1;
789
		}
790
		//Send U(TESTFR act) format
791
	}
792
	else
793
		App_CheckState();
794

    
795
	if (SendACKFlag == 10)				//Send S format
796
	{
797

    
798
		//SendHidataToSubstation();
799
	//	{
800
	//		PrintLog(LOG_INFORMATION, "<<<<< ????S֡  ֡??:%d ", ReceiveSeqNo);
801
	//		return 1;
802
	//	}
803
	}
804
	return 1;
805
}
806

    
807
int CSE102c::Call5KwhData()
808
{
809
	uint8* ptr = senddata.databuf;
810

    
811
	senddata.headP = senddata.trailP = 0;
812

    
813
	int curtime = GetNowSecond();
814
	if (curtime - LastTxTime < Timeout3 ||
815
		curtime - LastRxTime < Timeout3)
816
	{
817
		return -1;
818
	}
819
	*ptr++ = 0x10;
820
	*ptr++ = 0x5b;
821
	*ptr++ = 0x01;
822
	*ptr++ = 0x00;
823
	*ptr++ = 0x5c;
824
	*ptr++ = 0x16;
825
	senddata.trailP = ptr - senddata.databuf;
826
	AppToLink();
827
	PrintLog(LOG_INFORMATION, ">>>>>> ?????ٻ?????????5b???? ");
828
	CallKwhDataFlag = 7;
829
	//if (AppLayerStat == IEC102_APP_STATE_IDLE) 		//????״̬?²???ˢ??LastTxTime??
830
		LastTxTime = curtime;
831

    
832
	return 1;
833
}
834

    
835
int CSE102c::Call7KwhData()
836
{
837
	uint8* ptr = senddata.databuf;
838

    
839
	senddata.headP = senddata.trailP = 0;
840

    
841
	int curtime = GetNowSecond();
842
	if (curtime - LastTxTime < Timeout3 ||
843
		curtime - LastRxTime < Timeout3)
844
	{
845
		return -1;
846
	}
847
	*ptr++ = 0x10;
848
	*ptr++ = 0x7b;
849
	*ptr++ = 0x01;
850
	*ptr++ = 0x00;
851
	*ptr++ = 0x7c;
852
	*ptr++ = 0x16;
853
	senddata.trailP = ptr - senddata.databuf;
854
	AppToLink();
855
	PrintLog(LOG_INFORMATION, ">>>>>> ?????ٻ?????????7b???? ");
856
	CallKwhDataFlag = 5;
857
//	if (AppLayerStat == IEC102_APP_STATE_IDLE) 		//????״̬?²???ˢ??LastTxTime??
858
		LastTxTime = curtime;
859

    
860
	return 1;
861
}
862

    
863
sint32 CSE102c::MsgProc(sint32 msg)
864
{
865
	if (msg == DEF_MSG_COMMDOWN)
866
		InitStation();
867

    
868
	return 1;
869
}
(3-3/3)