You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.2 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "StdAfx.h"
#include "FrameIMP_Down.h"
CFrameIMPDown_::CFrameIMPDown_(void)
{
_tcscpy(Name, _T("IMP下行"));
FrameLength = 6;
HeadLength = 1;
Head[0] = 0x55;
memcpy(Addr(), Head, HeadLength);
CheckLength = 1;
CheckPos = FrameLength - 2;
memset(&Data,0,sizeof(DataIMPDown));
memset(recvDataValue, 0, 6);
}
CFrameIMPDown_::~CFrameIMPDown_(void)
{
}
BOOL CFrameIMPDown_::FullFrame()
{
int HeaderPos, HeaderPos2;
HeaderPos = FindHead(0, Length() - HeadLength); //找第一个帧头
if (0 == HeaderPos) {
//帧头在最前面
if (OnCheck()) //CRC校验
DoCheck();
else {
if (Length() < FrameLength) //数据长度不足未通过CRC校验
{
if (Size() < FrameLength) //误码未通过CRC校验
{
DoDropBYTE((UINT16)HeadLength); //丢弃帧头
}
return TRUE;
}
else //误码未通过CRC校验
{
HeaderPos2 = FindHead(HeadLength, Length() - HeadLength);
if (HeaderPos2 >= 0)
{
Drop((UINT16) HeaderPos2);
return FALSE;
}
else
{
DoDropBYTE((UINT16) (Length()-(HeadLength-1)));
return TRUE;
}
}
}
} else if (HeaderPos < 0) {
//没找到帧头
DoDropBYTE((UINT16) (Length()-(HeadLength-1)));
} else {
//丢弃帧头前的数据
DoDropBYTE((UINT16) HeaderPos);
}
return FALSE;
}
BOOL CFrameIMPDown_::OnCheck()
{
//数据帧校验
BYTE* p = Addr();
memcpy(recvDataValue, p, 5);
if (Length() < FrameLength)
{
return false;
}
struCHECK ck1=CheckCRC(p);
//if (p[CheckPos] == ck1.C0 && p[CheckPos + 1] == ck1.C1)//CRC校验
if (p[CheckPos] == ck1.C0 )//和校验
{
memcpy(recvDataValue, p, 5);
return true;
}
else
{
return false;
}
}
void CFrameIMPDown_::DoCheck()
{
CountFrameAll++;
CountFrameCheck++;
OnDataCheck();
Drop(FrameLength);
}
struCHECK CFrameIMPDown_::CheckCRC(BYTE* const p)
{
return MakeCheckSum(p+1, FrameLength - HeadLength - CheckLength - 1);//1为帧尾IMP有一个字节的帧尾
}
void CFrameIMPDown_::Decode()
{
ProtocalIMPDown* pBuffIMP = (ProtocalIMPDown*)Addr();
Data.head = pBuffIMP->head;
Data.Bytes = pBuffIMP->Bytes;
Data.StatusWord = pBuffIMP->StatusWord;
Data.FaultWord = pBuffIMP->FaultWord;
Data.CHKSUM = pBuffIMP->CHKSUM;
Data.EndFlag = pBuffIMP->EndFlag;
}