#include
#include
#include
using namespace std;
#include
#include
#define DEV_CHECK_OUT 0
int Str2Byte(CString strProtocol,CByteArray bytArProtocol)
{
int nSteLen = 0;
nstrLen = strProtocol.GetLength();
int nBlockLen = 2;
bytArProtocol.SetSize(nstrLen/2);
for (int i = 0; i < nstrLen; i += nBlockLen)
{
int nData = 0;
_stscanf(strProtocol.Mid(i, 2), _T("%x"), &nData);
bytArProtocol.SetAt(i/2, unsigned char(nData));
}
return 0;
}
int Byte2Str(CByteArray bytArProtocol,CString strProtocol)
{
strProtocol = _T("");
CString strTemp = _T("");
for (int i = 0; i < bytArProtocol.GetSize(); i ++)
{
strTemp.Format(_T("%.2X"), bytArProtocol.GetAt(i));
strProtocol += strTemp;
}
return 0;
}
BOOL mfAddSumCRC(CByteArray bytArProtocol)
{
int nStartLen = 5;
int nCheckDir = 0;
int nCrcLen = bytArProtocol.GetSize() - 2;
int i = 0, j = 0;
int MSBInfo;
WORD wCrcData = 0x0000;
/////////////////////////////////////////
if (nCheckDir == DEV_CHECK_OUT)
{
/////////////////STN//////////////////////////
wCrcData = wCrcData ^ (bytArProtocol.GetAt(2));
if (wCrcData < 0) wCrcData = wCrcData + 65536;
for (j= 0; j < 8; j ++)
{
MSBInfo = wCrcData & 0x0001;
wCrcData = wCrcData >> 1;
if (MSBInfo != 0 )
{
wCrcData = wCrcData ^ 0xa001;
}
}
////////////////STX///////////////////////////
wCrcData = wCrcData ^ STX;
if (wCrcData < 0) wCrcData = wCrcData + 65536;
for (j= 0; j < 8; j ++)
{
MSBInfo = wCrcData & 0x0001;
wCrcData = wCrcData >> 1;
if (MSBInfo != 0 )
{
wCrcData = wCrcData ^ 0xa001;
}
}
}
/////////////////////////////////////////
for(i = nStartLen; i < nCrcLen; i++)
{
wCrcData = wCrcData ^ (bytArProtocol.GetAt(i));
if (wCrcData < 0) wCrcData = wCrcData + 65536;
for (j = 0; j < 8; j ++)
{
MSBInfo = wCrcData & 0x0001;
wCrcData = wCrcData >> 1;
if (MSBInfo != 0 )
{
wCrcData = wCrcData ^ 0xa001;
}
}
}
///////////////////ETX///////////////////
wCrcData = wCrcData ^ ETX;
if (wCrcData < 0) wCrcData = wCrcData + 65536;
for (j = 0; j < 8; j ++)
{
MSBInfo = wCrcData & 0x0001;
wCrcData = wCrcData >> 1;
if (MSBInfo != 0 )
{
wCrcData = wCrcData ^ 0xa001;
}
}
switch (nCheckDir)
{
case 0:
// 发数据时校验
{
bytArProtocol.Add((wCrcData&0xff));
bytArProtocol.Add((wCrcData>>8&0xff));
}break;
case 1:
// 收数据时校验
{
nCrcLen = bytArProtocol.GetSize();
if ((wCrcData&0xff) != bytArProtocol.GetAt(nCrcLen - 2)
|| (wCrcData>>8&0xff) != bytArProtocol.GetAt(nCrcLen - 1))
{
return FALSE;
}
}break;
default:
{
}break;
}
return TRUE;
}
int main()
{
CByteArray bytArProtocol;
string strProtocol;
cin >> strProtocol;
Str2Byte(strProtocol,bytArProtocol);
mfAddSumCRC(bytArProtocol);
Byte2Str(bytArProtocol,strProtocol);
cout << strPrototol << endl;
}
这是在VS2008生成解决方案的错误信息:
1>------ 已启动生成: 项目: ABCRC, 配置: Debug Win32 ------
1>正在编译...
1>BullseyeCoverage Compile C++ 7.13.48 Windows License 1001
1>Copyright (c) Bullseye Testing Technology 1990-2010
1>ABCRC.cpp
1> WINVER not defined. Defaulting to 0x0600 (Windows Vista)
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(11) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(14) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(16) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(65) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(94) : error C2065:
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(138) : error C2664:
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(140) : error C2664:
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>e:/w?/ABCRC/ABCRC/ABCRC.cpp(141) : error C2065:
1>Exception: error status 2 from d:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin/cl.exe
1>生成日志保存在“file://e:\练习文件\ABCRC\ABCRC\Debug\BuildLog.htm”
1>ABCRC - 8 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========