📄 101.c
字号:
TCommCode[15]=nFileDataLen;
nFileDataLen=0;
nASDULen=TCommCode[15]+10;
return ;
}
if(nGroup==PEND_NUM)
{
ReadStaticsData(&TCommCode[16],&nFileDataLen,&stFileName,15, MAXLENGTH);
TCommCode[15]=nFileDataLen;
nFileDataLen=0;
nASDULen=TCommCode[15]+10;
return ;
}
}
}
void EncodeEndFrame(BYTE nASDULen) //the length of ASDU +8=THE LENGTH OF THE WHOLE FRAME....................................3
{
TCommCode[nASDULen+6]=CheckSum(&TCommCode[4],nASDULen+2);
TCommCode[nASDULen+7]=EndCode;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL DecodeFixedLenFrame(BYTE *pRecData)//...........................2
{
if(pRecData[3]!=CheckSum(&pRecData[1],2))
return FALSE;
if(pRecData[4]!=0x16)
return FALSE;
if(pRecData[2]!=TTU_RunParam.AddrOfTTU)
return FALSE;
Controlcode=pRecData[1];
CommState.DoReSend=0;
if((Controlcode &BIT4) && ((Controlcode&BIT5) == (Controlold&BIT5) ))
CommState.DoReSend=1;
Controlold=Controlcode;
return TRUE;
}
BOOL DecodeVariableLenFrame(BYTE *pRecData)//........................................2
{
BYTE len;
if((pRecData[0]!=pRecData[3])||(pRecData[1]!=pRecData[2]))
return FALSE;
len=pRecData[1];
if(CheckSum(&pRecData[4],len)!=pRecData[len+4])
return FALSE;
if(pRecData[len+5]!=0x16)
return FALSE;
if((pRecData[5]!=TTU_RunParam.AddrOfTTU)&&(pRecData[5]!=0xFF))
return FALSE;
if(pRecData[5] == 0xFF)
CommState.IsBroadcast=1;
else
{
Controlcode=pRecData[4];
CommState.DoReSend=0;
if((Controlcode &BIT4) && ((Controlcode&BIT5) == (Controlold&BIT5) ))
CommState.DoReSend=1;
Controlold=Controlcode;
CommState.IsBroadcast=0;
}
nTypeIdentity=pRecData[6];
return TRUE;
}
int DecodeFrame(BYTE *pRecData) //................................................1
{
if(pRecData[0]==0x10)
{
if(DecodeFixedLenFrame(pRecData)==TRUE)
return FIXFRAME;
}
if(pRecData[0]==0x68)
{
if(DecodeVariableLenFrame(pRecData)==TRUE)
return VARFRAME;
}
return FALSE;
}
BOOL HandleData(BYTE *pRecData) //接收解码................................0
{
///BYTE b=0;//测试
UINT temp;
BYTE nTypeIdentity,nCause,nFuction,nGroup=0;
//int *pi;
if(pRecData==NULL)
return FALSE;
temp=DecodeFrame(pRecData);
if(temp==FALSE)
return FALSE ;
if(CommState.DoReSend &&(LinkState == 0x0B) ) //重发处理
{
CS485TransmitEnable;
IE2 |= UTXIE1;
return FALSE;
}
if(temp==FIXFRAME)
{
switch(Controlcode&0xF) //a question :how to set ACD when necessary?
{
case 0: //restore link
strcontrol.FUNCTION=FC_LINKRESTORE;
strcontrol.ACD=0;
strcontrol.DFC=0;
EncodeFixedLenFrame();
if(LinkState==0x0E)
{
LinkState=0x0B;
strcontrol.ACD=1;
SpecialFlag.InitEndFlag=1;
}
break;
case 9:
strcontrol.FUNCTION= (LinkState&0xF); //FC_LINKSTATUSNOEMAL; //request link status
EncodeFixedLenFrame();
break;
case 0xA:
/* if(LinkState != 0x0B)
return FALSE; */
if(SpecialFlag.InitEndFlag==1) //the end of substation initialization
{
nFuction=0x8;
nCause=4;
nTypeIdentity=70;
strcontrol.ACD=0;
EncodeVariableLenFrame(nTypeIdentity,nCause,nFuction,nGroup);
SpecialFlag.InitEndFlag=0;
}
else if(SpecialFlag.YXChangeFlag==1) //class 1 data
{
nFuction=0x8;
nCause=11; //variable yaoxin
nTypeIdentity=0x01;
nGroup=0;
EncodeVariableLenFrame(nTypeIdentity,nCause,nFuction,nGroup);
SpecialFlag.YXChangeFlag=0;
strcontrol.ACD=0;
break;
}
else
{
TCommCode[0]=0xE5;
m_EnCur=1;
CommState.TxWait=1;
TxGap=TTU_RunParam.PWM_SET; ; //发送间隙
CS485TransmitEnable;
IE2 |= UTXIE1;
return TRUE;
}
break;
case 0xB:
/* if(LinkState != 0x0B)
return FALSE; //Class 2 data
*/
nFuction=0x8; //here is yc change respond frame
nCause=5;
if( SpecialFlag.SOEFlag==1 )
{
nTypeIdentity=0x02; //here is SOE respond frame
EncodeVariableLenFrame(nTypeIdentity,nCause,nFuction,nGroup);
SpecialFlag.SOEFlag=0;
DeviceState.bSOEOverFlow=0;
nSOERecords=0;
break;
}
if( SpecialFlag.YCChangeFlag==1 ) //when yc data has changed (to continue.....!)
{
nTypeIdentity=0x9; //here is yc change respond frame
EncodeVariableLenFrame(nTypeIdentity,nCause,nFuction,nGroup);
SpecialFlag.YCChangeFlag=0;
break;
}
else
{
TCommCode[0]=0xE5;
m_EnCur=1;
CommState.TxWait=1;
TxGap=TTU_RunParam.PWM_SET; //发送间隙
CS485TransmitEnable;
IE2 |= UTXIE1;
return TRUE;
}
}
return TRUE;
}
if(temp==VARFRAME)
{
/* if(LinkState != 0x0B)
return FALSE;*/
nTypeIdentity=pRecData[6];
if(CommState.IsBroadcast &&(nTypeIdentity!=C_CS_NA_1))
return FALSE;
switch(nTypeIdentity)
{
case C_CS_NA_1 : //Clock synchronisation command 103 (图 114)
temp=pRecData[12]+(pRecData[13]<<8); //second high byte
stDateTime.MillSecond=temp;
temp/=1000;
stDateTime.second=temp;
if( pRecData[14]>=60||pRecData[15]>23|| (pRecData[16]&0x1f)>31 ||pRecData[17]>12||pRecData[18]>99)//判断是否合法
return FALSE;
stDateTime.minute= pRecData[14];
stDateTime.hour= pRecData[15];
stDateTime.week_date= pRecData[16];
stDateTime.month= pRecData[17];
stDateTime.year= pRecData[18];
temp=HextoBCD(temp); //s
SetPCF8563(2,(BYTE *)&temp,1);
temp=HextoBCD(pRecData[14]); //minu
SetPCF8563(3,(BYTE *)&temp,1);
temp=HextoBCD(pRecData[15]); //hour
SetPCF8563(4,(BYTE *)&temp,1);
temp=HextoBCD(pRecData[16]&0x1f);//date
SetPCF8563(5,(BYTE *)&temp,1);
temp=HextoBCD( ((pRecData[16]&0xe0)>>5)-1); //week
SetPCF8563(6,(BYTE *)&temp,1);
temp=HextoBCD(pRecData[17]); //month
SetPCF8563(7,(BYTE *)&temp,1);
temp=HextoBCD(pRecData[18]); //year
SetPCF8563(8,(BYTE *)&temp,1);
if(CommState.IsBroadcast)
return FALSE;
nCause=7;
nFuction=0;
break;
case C_IC_NA_1: //Interrogation command 100
if(pRecData[12]==20) // here ,first enocode a confirm frame to send
{
nFuction=0;
nCause=0x07; //07h/09h positive or negative
nGroup=20;
SpecialFlag.CallAllFlag=1; //use this flag to handle multiplier frame in dowork()
SpecialFlag.CallAll_YCFlag=1;
SpecialFlag.CallAll_YXFlag=1;
SpecialFlag.CallAll_DDFlag=1;
SpecialFlag.CallAll_EndFlag=1;
}
if(pRecData[12]==29) // group call (the 9 group) yc without quality
{
nCause=29;
nFuction=8;
nGroup=29;
nTypeIdentity=0x15;
}
if(pRecData[12]==21) // group call (the 1 group) yx
{
nCause=21;
nFuction=8;
nGroup=21;
}
//to continue ............... //other group call can be added here
break;
case C_TS_NB_1: //test channel
nCause=0x7;
nFuction=0;
nGroup=0;
break;
case C_CI_NA_1: //Counter interrogation command 101 按分组召唤格式
if(pRecData[12]==0x02) //first group 电镀量
{
nCause=38;
nGroup=0x41;
}
if(pRecData[12]==0x03) //停电记录
{
nCause=40;
nGroup=0x42;
}
if(pRecData[12]==0x04) //遥测越限记录
{
nCause=41;
nGroup=0x43;
}
if(pRecData[12]==0x05) //统计数据状态记录
{
nCause=42;
nGroup=0x44;
}
nFuction=8;
nTypeIdentity=M_IT_NA_1; //15
break;
case C_DC_NA_1 : //yk command 由信息体地址pRecData[10]决定遥控对象
if( pRecData[8]==6 )//选择或执行命令
{
nYKQualifier=pRecData[12];//1-开 2-合
nYKObject=pRecData[10];
if((pRecData[12]&BIT7) == BIT7 ) //为选择命令
{
delayselect = TTU_RunParam.YKSelectTime; //
YKselect= nYKQualifier ;
YKObj = nYKObject;
}
else //为执行命令
{
if(YKObj != nYKObject) YKselect=0;
if(YKselect==0)
{
nGroup=1; //作为否定认可标志
nFuction=0;
nCause=7;
break;
}
else
{
YKoperate = nYKQualifier ;
delayoperate= TTU_RunParam.YKExcecuteTime; //1.5s
}
}
nFuction=0;
nCause=7;
}
if( pRecData[8]==8) //撤消命令
{
YKselect=0;
delayselect=0;
nYKQualifier=pRecData[12];//1-开 2-合
nYKObject=pRecData[10];
YKoperate=0;
nFuction=0;
nCause=9;
}
break;
case C_RP_NC_1: //reset process ( 图 70 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -