📄 processcdt.cpp
字号:
SYSTEMTIME SysTime;
WORD SetTimeFrame[12];
BYTE *SetTimeFrameSyn = (BYTE *)SetTimeFrame;
GetLocalTime(&SysTime);
for (int l=0;l<6;l++) SetTimeFrameSyn[l] = Rtus[RtuNo].SynWord[l+1];
SetTimeFrame[3]=0x7a71; SetTimeFrame[4]=0x0102;
SetTimeFrame[5]=(WORD)Rtus[RtuNo].Addr;
SetTimeFrame[5] +=(WORD)(CDT_CheckCRC((BYTE*)&SetTimeFrame[3])*256);
SetTimeFrame[6]=(SysTime.wMilliseconds%256)*256+0xee;
SetTimeFrame[7] = 0;
SetTimeFrame[7]=SysTime.wSecond*256+(SysTime.wMilliseconds>>8);
SetTimeFrame[8]=SysTime.wMinute;
SetTimeFrame[8] +=(WORD)(CDT_CheckCRC((BYTE*)&SetTimeFrame[6])*256);
SetTimeFrame[9]=SysTime.wHour*256+0xef;
SetTimeFrame[10]=SysTime.wMonth*256+SysTime.wDay;
SetTimeFrame[11]=SysTime.wYear%100;
SetTimeFrame[11] += (WORD)(CDT_CheckCRC((BYTE*)&SetTimeFrame[9])*256);
if (Channels[i].WorkType != 1)
{
MoxaBufWrite(i,(char*)SetTimeFrame,24);
}
}
BYTE CDT_CheckCRC(BYTE *pInfoWord)
{
//A=DS:[SI],[SI+1],[SI+2],[SI+3],[SI+4] => BL=BCH of (48,40) =mod(A*2**8,G)
//SI=SI+5 using the BCH table TBCH0. The G(X)=X**8 + X**2 + X + 1 = 107H
BYTE ret = 0;
for ( int i=0; i<5; i++ )
{
ret ^= *(pInfoWord+i);
ret = TBCH0[ret];
}
return (ret^0xff);
}
void CDT_ReviseTime(int i,int RtuNo,int NonceInfoWord)//设置时钟校正值
{
BYTE *RecBuf = &Rtus[RtuNo].RecBuf[NonceInfoWord];
if(Rtus[RtuNo].RecBuf[NonceInfoWord] == 0x84)//子站时钟反送
{
GetLocalTime(&Rtus[RtuNo].CDT_CurTime);
Rtus[RtuNo].CDT_ChildTime = RecBuf[1] + RecBuf[2]*256 + RecBuf[3]*1000 + RecBuf[4]*60000;//收到召唤命令的子站时间
}else
{
BYTE ReviseTimeFrame[18];
WORD ReviseTimeWord;
DWORD ChildWaitTime = RecBuf[1] + RecBuf[2]*256 + RecBuf[3]*1000 + RecBuf[4]*60000;//子站等待时间
DWORD CurTime = Rtus[RtuNo].CDT_CurTime.wMilliseconds + Rtus[RtuNo].CDT_CurTime.wSecond * 1000//当前时间
+ Rtus[RtuNo].CDT_CurTime.wMinute * 6000;
DWORD CallUpTime = Rtus[RtuNo].CDT_CallUpSysTime.wMilliseconds + Rtus[RtuNo].CDT_CallUpSysTime.wSecond * 1000
+ Rtus[RtuNo].CDT_CallUpSysTime.wMinute * 6000;//召唤时钟的主站时间
int ReviseTime = (CallUpTime+CurTime-ChildWaitTime)/2 - Rtus[RtuNo].CDT_ChildTime;
for (int l=0;l<6;l++) ReviseTimeFrame[l] = Rtus[RtuNo].SynWord[l+1];
if (ReviseTime<0)
{
ReviseTimeWord = (-ReviseTime) % 32512;// &0x7fff;
ReviseTimeWord ^= 0x7fff;
ReviseTimeWord ++;
ReviseTimeWord |= 0x8000;
}else
ReviseTimeWord = ReviseTime % 32512;// &0X7fff
*((DWORD*)&ReviseTimeFrame[6]) = 0x010b71;
ReviseTimeFrame[10] = Rtus[RtuNo].Addr;
ReviseTimeFrame[11] = CDT_CheckCRC(&ReviseTimeFrame[6]);
ReviseTimeFrame[12] = 0xed;
*((WORD*)&ReviseTimeFrame[13]) = ReviseTimeWord;
*((WORD*)&ReviseTimeFrame[15]) = ReviseTimeWord;
ReviseTimeFrame[17] = CDT_CheckCRC(&ReviseTimeFrame[12]);
if (Channels[i].WorkType != 1)
{
MoxaBufWrite(i,(char *)ReviseTimeFrame,18);
}
}
}
void CDT_SOE_Event(int RtuNo,int &NonceInfoWord)
{
WORD MilliSeconds;
FERTEVENT Event;
FERTEVENT *CurEventP;
SYSTEMTIME CurTime;
BYTE *RecBuf = &Rtus[RtuNo].RecBuf[NonceInfoWord];
BYTE *SOEInfo = Rtus[RtuNo].CDT_SOEInfo;
GetLocalTime(&CurTime);
if (RecBuf[11] != CDT_CheckCRC(RecBuf + 6))
{
NonceInfoWord +=6;
return;
}
if ((RecBuf[9] == SOEInfo[0]) && ((RecBuf[10]&0x8f) == (SOEInfo[1]&0x8f))
&& (RecBuf[1] == SOEInfo[2]) && (RecBuf[2] == SOEInfo[3]) && (RecBuf[3] == SOEInfo[4]))
{
NonceInfoWord += 6;
return;
}
if((Events.EventNum+1)==Events.EventSendNum)
{
Beep(1200,500);
NonceInfoWord += 6;
return;
}
MilliSeconds=RecBuf[1] + (RecBuf[2] & 0x03)*256;
if (Rtus[RtuNo].TimeForbit == 1)
{
Event.ms = CurTime.wMilliseconds % 100;
Event.hundms = CurTime.wMilliseconds / 100;
Event.seconds = (BYTE)CurTime.wSecond;
Event.minute = (BYTE)CurTime.wMinute;
Event.hour = (BYTE)CurTime.wHour;
Event.day = (BYTE)CurTime.wDay;
}else
{
Event.ms = MilliSeconds % 100;
Event.hundms = MilliSeconds / 100;
Event.seconds = RecBuf[3] & 0x3f;
Event.minute = RecBuf[4] & 0x3f;
Event.hour = RecBuf[7] & 0x1f;
Event.day = RecBuf[8] & 0x1f;
}
Event.month = (BYTE)CurTime.wMonth;
Event.year = CurTime.wYear;
Event.Recms = CurTime.wMilliseconds % 100;
Event.Rechundms = CurTime.wMilliseconds/100;
Event.Recseconds = (BYTE)CurTime.wSecond;
Event.Recminute = (BYTE)CurTime.wMinute;
Event.Rechour = (BYTE)CurTime.wHour;
Event.Recday = (BYTE)CurTime.wDay;
Event.rtuno = RtuNo;
Event.yxno = RecBuf[9] + (RecBuf[10] & 0xf)*256;
Event.cause = 1;
Event.type = 1;
Event.state = (RecBuf[10] & 0x80) ? 1 : 0;
NonceInfoWord += 6;
SOEInfo[0] = RecBuf[9];
SOEInfo[1] = RecBuf[10];
SOEInfo[2] = RecBuf[1];
SOEInfo[3] = RecBuf[2];
SOEInfo[4] = RecBuf[3];
CurEventP = Events.EventBuf + Events.EventNum;
memcpy(CurEventP,&Event,sizeof(FERTEVENT));
if(++Events.EventNum>=MAX_EVENT_NUM)
Events.EventNum=0;
}
void CDTZF(int ChNo)
{
int RtuNo = Channels[ChNo].ChRtuNo[0];
if(CDTZF_Receive(ChNo))
{
Rtus[RtuNo].bWorking = TRUE;
Channels[ChNo].bWorking = true;
}
CDTZF_Send(ChNo);
}
bool CDTZF_Receive(int ChNo)
{
int InLen = MoxaBufIqueue(ChNo);
int RtuNo = Channels[ChNo].ChRtuNo [0];
if (InLen<6) return false;
Rtus[RtuNo].RecFrameSum += 1;
MoxaBufRead(ChNo,&Rtus[RtuNo].RecBuf[0],6);
Rtus[RtuNo].RecBufPtr=6;
Rtus[RtuNo].RecLen=6;
Rtus[RtuNo].bReceiveCanDisp = TRUE;
return true;
}
void CDTZF_Send(int ChNo)
{//发码顺序 ABACABACABACD1AD2
static int iCurSort = 0;//指明当前发码顺序处于哪一个位置
int RtuNo = Channels[ChNo].ChRtuNo [0];
BYTE *SendBuf = &Rtus[RtuNo].TXBuf [0];
if (Rtus[RtuNo].SoePtr != Rtus[RtuNo].SoeSendPtr )//have soe event and yx change
{//send f4
for (int i = 0;i < 6;i++) SendBuf[i] = Rtus[RtuNo].SynWord [i + 1];
int iCount = (Rtus[RtuNo].YxNum + 31) / 32;//信息字个数
SendBuf[6] = 0x71;
SendBuf[7] = 0xf4;
SendBuf[8] = iCount;
SendBuf[9] = 0;
SendBuf[10] = 0;
SendBuf[11] = CDT_CheckCRC(&SendBuf[6]);
for (i = 0;i < iCount;i++)
{
SendBuf[12 + i * 6] = Rtus[RtuNo].ProtocolStamp[1] + i;
for (int j = 0;j < 4;j++)
{
BYTE YxValue = 0;
for (int k = 0;k < 8;k++)
{
int iDian = i * 32 + j * 8 + k;//yx dian
YxValue |= ((Rtus[RtuNo].YxValue [iDian] & 0x1) << k);
}
SendBuf[12 + i * 6 + j + 1] = YxValue;
}
SendBuf[12 + i * 6 + 5] = CDT_CheckCRC(&SendBuf[12 + i * 6]);
}
Rtus[RtuNo].TXBufPtr = 12 + iCount * 6;
Rtus[RtuNo].TXLen=Rtus[RtuNo].TXBufPtr;
Rtus[RtuNo].bSendCanDisp=TRUE;
MoxaBufWrite(ChNo,(char*)&Rtus[RtuNo].TXBuf[0],Rtus[RtuNo].TXBufPtr);
Rtus[RtuNo].TXBufPtr=0;
int iDelay = (int)((float)Rtus[RtuNo].TXLen / (Channels[ChNo].Baud / 14) * 1000);//ms
Sleep(iDelay);
//send soe
int iSoeNum = Rtus[RtuNo].SoePtr - Rtus[RtuNo].SoeSendPtr ;
int iPtr = Rtus[RtuNo].SoeSendPtr ;
for (i = 0;i < 6;i++) SendBuf[i] = Rtus[RtuNo].SynWord [i + 1];
SendBuf[6] = 0x71;
SendBuf[7] = 0x26;
SendBuf[8] = iSoeNum * 6;
SendBuf[9] = 0;
SendBuf[10] = 0;
SendBuf[11] = CDT_CheckCRC(&SendBuf[6]);
for (i = 0;i < iSoeNum;i++)
{
for (int j = 0;j < 3;j++)
{
SendBuf[12 + i * 36 + j * 12] = 0x80;
SendBuf[12 + i * 36 + j * 12 + 1] = Rtus[RtuNo].SoeEvent [iPtr].SoeTime.wMilliseconds % 256;
SendBuf[12 + i * 36 + j * 12 + 2] = Rtus[RtuNo].SoeEvent [iPtr].SoeTime.wMilliseconds / 256;
SendBuf[12 + i * 36 + j * 12 + 3] = (BYTE)Rtus[RtuNo].SoeEvent [iPtr].SoeTime.wSecond;
SendBuf[12 + i * 36 + j * 12 + 4] = (BYTE)Rtus[RtuNo].SoeEvent [iPtr].SoeTime.wMinute;
SendBuf[12 + i * 36 + j * 12 + 5] = CDT_CheckCRC(&SendBuf[12 + i * 36 + j * 12]);
SendBuf[12 + i * 36 + j * 12 + 6] = 0x81;
SendBuf[12 + i * 36 + j * 12 + 7] = (BYTE)Rtus[RtuNo].SoeEvent [iPtr].SoeTime.wHour;
SendBuf[12 + i * 36 + j * 12 + 8] = (BYTE)Rtus[RtuNo].SoeEvent [iPtr].SoeTime.wDay;
SendBuf[12 + i * 36 + j * 12 + 9] = Rtus[RtuNo].SoeEvent [iPtr].YxNo % 256;
SendBuf[12 + i * 36 + j * 12 + 10] = ((Rtus[RtuNo].SoeEvent [iPtr].YxNo / 256) & 0xf)
+ ((Rtus[RtuNo].SoeEvent [iPtr].YxState & 0x1) << 7);
SendBuf[12 + i * 36 + j * 12 + 11] = CDT_CheckCRC(&SendBuf[12 + i * 36 + j * 12 + 6]);
}
}
Rtus[RtuNo].TXBufPtr = 12 + iSoeNum * 36;
Rtus[RtuNo].TXLen=Rtus[RtuNo].TXBufPtr;
Rtus[RtuNo].bSendCanDisp=TRUE;
MoxaBufWrite(ChNo,(char*)&Rtus[RtuNo].TXBuf[0],Rtus[RtuNo].TXBufPtr);
Rtus[RtuNo].TXBufPtr=0;
Rtus[RtuNo].SoeSendPtr = Rtus[RtuNo].SoePtr;
}
else//按顺序发送
{
switch(iCurSort)
{
case 0://A,first 10 yc
case 2:
case 4:
case 6:
case 8:
case 10:
case 13:
{
for (int i = 0;i < 6;i++) SendBuf[i] = Rtus[RtuNo].SynWord [i + 1];
int iCount = 10 / 2;//信息字个数
if (Rtus[RtuNo].YcNum < 10)
iCount = (Rtus[RtuNo].YcNum + 1) / 2;
SendBuf[6] = 0x71;
SendBuf[7] = 0x61;
SendBuf[8] = iCount;
SendBuf[9] = 0;
SendBuf[10] = 0;
SendBuf[11] = CDT_CheckCRC(&SendBuf[6]);
for (i = 0;i < iCount;i++)
{
SendBuf[12 + i * 6] = Rtus[RtuNo].ProtocolStamp[0] + i;
int YcValue = (int)(Rtus[RtuNo].YcValue [2 * i] * 10) % 2048;
if (YcValue >= 0)
{
SendBuf[12 + i * 6 + 1] = (int)YcValue % 256;
SendBuf[12 + i * 6 + 2] = (int)(YcValue / 256) & 0x7;
}
else
{
WORD YcVal = YcValue + 2048;
SendBuf[12 + i * 6 + 1] = YcVal % 256;
SendBuf[12 + i * 6 + 2] = ((YcVal / 256) & 0x7) | 0x8;
}
YcValue = (int)(Rtus[RtuNo].YcValue [2 * i + 1] * 10) % 2048;
if (YcValue >= 0)
{
SendBuf[12 + i * 6 + 3] = (int)YcValue % 256;
SendBuf[12 + i * 6 + 4] = (int)(YcValue / 256) & 0x7;
}
else
{
WORD YcVal = YcValue + 2048;
SendBuf[12 + i * 6 + 3] = YcVal % 256;
SendBuf[12 + i * 6 + 4] = ((YcVal / 256) & 0x7) | 0x8;
}
SendBuf[12 + i * 6 + 5] = CDT_CheckCRC(&SendBuf[12 + i * 6]);
}
Rtus[RtuNo].TXBufPtr = 12 + iCount * 6;
Rtus[RtuNo].TXLen=Rtus[RtuNo].TXBufPtr;
Rtus[RtuNo].bSendCanDisp=TRUE;
MoxaBufWrite(ChNo,(char*)&Rtus[RtuNo].TXBuf[0],Rtus[RtuNo].TXBufPtr);
Rtus[RtuNo].TXBufPtr=0;
}
break;
case 1://B 20 yc
case 5:
case 9:
{
for (int i = 0;i < 6;i++) SendBuf[i] = Rtus[RtuNo].SynWord [i + 1];
int iCount = 20 / 2;//信息字个数
if (Rtus[RtuNo].YcNum < 30)
iCount = (Rtus[RtuNo].YcNum - 10 + 1) / 2;
SendBuf[6] = 0x71;
SendBuf[7] = 0xc2;
SendBuf[8] = iCount;
SendBuf[9] = 0;
SendBuf[10] = 0;
SendBuf[11] = CDT_CheckCRC(&SendBuf[6]);
for (i = 0;i < iCount;i++)
{
SendBuf[12 + i * 6] = Rtus[RtuNo].ProtocolStamp[0] + 5 + i;
int YcValue = (int)(Rtus[RtuNo].YcValue [2 * i + 10] * 10) % 2048;
if (YcValue >= 0)
{
SendBuf[12 + i * 6 + 1] = (int)YcValue % 256;
SendBuf[12 + i * 6 + 2] = (int)(YcValue / 256) & 0x7;
}
else
{
WORD YcVal = YcValue + 2048;
SendBuf[12 + i * 6 + 1] = YcVal % 256;
SendBuf[12 + i * 6 + 2] = ((YcVal / 256) & 0x7) | 0x8;
}
YcValue = (int)(Rtus[RtuNo].YcValue [2 * i + 10 + 1] * 10) % 2048;
if (YcValue >= 0)
{
SendBuf[12 + i * 6 + 3] = (int)YcValue % 256;
SendBuf[12 + i * 6 + 4] = (int)(YcValue / 256) & 0x7;
}
else
{
WORD YcVal = YcValue + 2048;
SendBuf[12 + i * 6 + 3] = YcVal % 256;
SendBuf[12 + i * 6 + 4] = ((YcVal / 256) & 0x7) | 0x8;
}
SendBuf[12 + i * 6 + 5] = CDT_CheckCRC(&SendBuf[12 + i * 6]);
}
Rtus[RtuNo].TXBufPtr = 12 + iCount * 6;
Rtus[RtuNo].TXLen=Rtus[RtuNo].TXBufPtr;
Rtus[RtuNo].bSendCanDisp=TRUE;
MoxaBufWrite(ChNo,(char*)&Rtus[RtuNo].TXBuf[0],Rtus[RtuNo].TXBufPtr);
Rtus[RtuNo].TXBufPtr=0;
}
break;
case 3://C
case 7:
case 11:
{
for (int i = 0;i < 6;i++) SendBuf[i] = Rtus[RtuNo].SynWord [i + 1];
int iCount = 0;//信息字个数
if (Rtus[RtuNo].YcNum > 30)
iCount = (Rtus[RtuNo].YcNum - 30 + 1) / 2;
SendBuf[6] = 0x71;
SendBuf[7] = 0xb3;
SendBuf[8] = iCount;
SendBuf[9] = 0;
SendBuf[10] = 0;
SendBuf[11] = CDT_CheckCRC(&SendBuf[6]);
for (i = 0;i < iCount;i++)
{
SendBuf[12 + i * 6] = Rtus[RtuNo].ProtocolStamp[0] + 15 + i;
int YcValue = (int)(Rtus[RtuNo].YcValue [2 * i + 30] * 10) % 2048;
if (YcValue >= 0)
{
SendBuf[12 + i * 6 + 1] = (int)YcValue % 256;
SendBuf[12 + i * 6 + 2] = (int)YcValue & 0x7;
}
else
{
WORD YcVal = YcValue + 2048;
SendBuf[12 + i * 6 + 1] = YcVal % 256;
SendBuf[12 + i * 6 + 2] = ((YcVal / 256) & 0x7) | 0x8;
}
YcValue = (int)(Rtus[RtuNo].YcValue [2 * i + 30 + 1] * 10) % 2048;
if (YcValue >= 0)
{
SendBuf[12 + i * 6 + 3] = (int)YcValue % 256;
SendBuf[12 + i * 6 + 4] = (int)(YcValue / 256) & 0x7;
}
else
{
WORD YcVal = YcValue + 2048;
SendBuf[12 + i * 6 + 3] = YcVal % 256;
SendBuf[12 + i * 6 + 4] = ((YcVal / 256) & 0x7) | 0x8;
}
SendBuf[12 + i * 6 + 5] = CDT_CheckCRC(&SendBuf[12 + i * 6]);
}
Rtus[RtuNo].TXBufPtr = 12 + iCount * 6;
Rtus[RtuNo].TXLen=Rtus[RtuNo].TXBufPtr;
Rtus[RtuNo].bSendCanDisp=TRUE;
MoxaBufWrite(ChNo,(char*)&Rtus[RtuNo].TXBuf[0],Rtus[RtuNo].TXBufPtr);
Rtus[RtuNo].TXBufPtr=0;
}
break;
case 12://D1
{
for (int i = 0;i < 6;i++) SendBuf[i] = Rtus[RtuNo].SynWord [i + 1];
int iCount = (Rtus[RtuNo].YxNum + 31) / 32;//信息字个数
SendBuf[6] = 0x71;
SendBuf[7] = 0xf4;
SendBuf[8] = iCount;
SendBuf[9] = 0;
SendBuf[10] = 0;
SendBuf[11] = CDT_CheckCRC(&SendBuf[6]);
for (i = 0;i < iCount;i++)
{
SendBuf[12 + i * 6] = Rtus[RtuNo].ProtocolStamp[1] + i;
for (int j = 0;j < 4;j++)
{
BYTE YxValue = 0;
for (int k = 0;k < 8;k++)
{
int iDian = i * 32 + j * 8 + k;//yx dian
YxValue |= ((Rtus[RtuNo].YxValue [iDian] & 0x1) << k);
}
SendBuf[12 + i * 6 + j + 1] = YxValue;
}
SendBuf[12 + i * 6 + 5] = CDT_CheckCRC(&SendBuf[12 + i * 6]);
}
Rtus[RtuNo].TXBufPtr = 12 + iCount * 6;
Rtus[RtuNo].TXLen=Rtus[RtuNo].TXBufPtr;
Rtus[RtuNo].bSendCanDisp=TRUE;
MoxaBufWrite(ChNo,(char*)&Rtus[RtuNo].TXBuf[0],Rtus[RtuNo].TXBufPtr);
Rtus[RtuNo].TXBufPtr=0;
}
break;
case 14://D2
break;
}
if (iCurSort++ > 14) iCurSort = 0;
int iDelay = (int)((float)Rtus[RtuNo].TXLen / (Channels[ChNo].Baud / 14) * 1000);//ms
Sleep(iDelay);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -