📄 commproc.cpp
字号:
// CommProc.cpp: implementation of the CCommProc class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CommProc.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCommProc::CCommProc()
{
}
CCommProc::~CCommProc()
{
}
//********************************
//***
//***
//*********************************
byte CCommProc::strHexToByte(CString strHex)
{
int strHexLen;
char pcHex[16384];
memset(pcHex,0x00,16384);
byte byReturnValue;
byte Tmp;
//int byReturnValue;
byReturnValue=0;
strHexLen=0;
strHexLen=strlen(strHex);
strcpy(pcHex,strHex);
for(byte i=0 ;i<strHexLen;i++)
{
switch (pcHex[i])
{
case '0':
byReturnValue+=0*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x00;
break;
case '1':
byReturnValue+=1*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x01;
break;
case '2':
byReturnValue+=2*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x02;
break;
case '3':
byReturnValue+=3*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x03;
break;
case '4':
byReturnValue+=4*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x04;
break;
case '5':
byReturnValue+=5*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x05;
break;
case '6':
byReturnValue+=6*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x06;
break;
case '7':
byReturnValue+=7*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x07;
break;
case '8':
byReturnValue+=8*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x08;
break;
case '9':
byReturnValue+=9*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x09;
break;
case 'A':
case 'a':
byReturnValue+=10*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x0A;
break;
case 'B':
case 'b':
byReturnValue+=11*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x0B;
break;
case 'C':
case 'c':
byReturnValue+=12*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x0C;
break;
case 'D':
case 'd':
byReturnValue+=13*(1<<(4*(strHexLen-1-i)));;
//Tmp = 0x0D;
break;
case 'E':
case 'e':
byReturnValue+=14*(1<<(4*(strHexLen-1-i)));
//Tmp = 0x0E;
break;
case 'F':
case 'f':
//Tmp = 0x0f;
byReturnValue+=15*(1<<(4*(strHexLen-1-i)));
break;
default:
break;
}
//byReturnValue = byReturnValue + Tmp;
//if (i < 3)byReturnValue = byReturnValue << 4;
}
return byReturnValue;
}
int CCommProc::strHexToInt(CString strHex) //输入如("3FFF")十六进制表示的字符串,转化为int
{
int strHexLen;
char pcHex[16384];
memset(pcHex,0x00,16384);
//byte byReturnValue;
byte Tmp;
int byReturnValue;
byReturnValue=0;
strHexLen=0;
strHexLen=strlen(strHex);
strcpy(pcHex,strHex);
if ((pcHex[0]=='0') && (pcHex[1]=='x') || (pcHex[1]=='X'))
{
return byReturnValue;
}
for(byte i=0 ;i<strHexLen;i++)
{
switch (pcHex[i])
{
case '0':
//byReturnValue+=0*(1<<(4*(strHexLen-1-i)));
Tmp = 0x00;
break;
case '1':
//byReturnValue+=1*(1<<(4*(strHexLen-1-i)));
Tmp = 0x01;
break;
case '2':
//byReturnValue+=2*(1<<(4*(strHexLen-1-i)));
Tmp = 0x02;
break;
case '3':
//byReturnValue+=3*(1<<(4*(strHexLen-1-i)));
Tmp = 0x03;
break;
case '4':
//byReturnValue+=4*(1<<(4*(strHexLen-1-i)));
Tmp = 0x04;
break;
case '5':
//byReturnValue+=5*(1<<(4*(strHexLen-1-i)));
Tmp = 0x05;
break;
case '6':
//byReturnValue+=6*(1<<(4*(strHexLen-1-i)));
Tmp = 0x06;
break;
case '7':
//byReturnValue+=7*(1<<(4*(strHexLen-1-i)));
Tmp = 0x07;
break;
case '8':
//byReturnValue+=8*(1<<(4*(strHexLen-1-i)));
Tmp = 0x08;
break;
case '9':
//byReturnValue+=9*(1<<(4*(strHexLen-1-i)));
Tmp = 0x09;
break;
case 'A':
case 'a':
//byReturnValue+=10*(1<<(4*(strHexLen-1-i)));
Tmp = 0x0A;
break;
case 'B':
case 'b':
//byReturnValue+=11*(1<<(4*(strHexLen-1-i)));
Tmp = 0x0B;
break;
case 'C':
case 'c':
//byReturnValue+=12*(1<<(4*(strHexLen-1-i)));
Tmp = 0x0C;
break;
case 'D':
case 'd':
//byReturnValue+=13*(1<<(4*(strHexLen-1-i)));;
Tmp = 0x0D;
break;
case 'E':
case 'e':
//byReturnValue+=14*(1<<(4*(strHexLen-1-i)));
Tmp = 0x0E;
break;
case 'F':
case 'f':
Tmp = 0x0f;
//byReturnValue+=15*(1<<(4*(strHexLen-1-i)));
break;
default:
break;
}
byReturnValue = (byReturnValue<< 4) + Tmp;
//if (i < 3)byReturnValue = byReturnValue << 4;
}
return byReturnValue;
}
byte CCommProc::strBinToByte(CString strBin)
{
char pcBin[256];
int iLen;
CString strTemp;
iLen=strlen(strBin);
memset(pcBin,0x00,256);
strTemp=strBin;
strcpy(pcBin,strTemp);
byte byReturnValue;
byReturnValue=0;
for(int i=0;i<iLen;i++)
{
if(pcBin[i]=='1')
byReturnValue+=(1<<(iLen-i-1));
}
return byReturnValue;
}
byte CCommProc::strDecTobyte(CString strDec)
{
byte ReturnValue;
ReturnValue=atoi(strDec);
return ReturnValue;
}
//******************
//***x10命令帧组合FMoveBit()
//***
//******************
//x10命令帧组合FMoveBit()
BOOL CCommProc::FMoveBit(const byte btSource1,int iToFirst1,int iLast1,const byte bySource2,int iToFirst2,byte &byDest)
{
byte byTemp1,byTemp2;
byTemp1=btSource1;
byTemp1=(byTemp1<<iToFirst1);
byTemp2=bySource2; //
byTemp2=(byTemp2<<iToFirst2);
byTemp2=(byTemp2>>(8-iLast1));
byDest=byTemp1+byTemp2;
return true;
}
//***************************************
//***计算校验和GetSum()
//***Author: wan
//***date: 2005-03-23
//***************************************
byte CCommProc::GetSum(const byte *AddData,int iCount) //计算校验和GetSum()
{
int i,iReturnResult;
iReturnResult=0;
for(i=0;i<iCount;i++)
{
iReturnResult+=AddData[i];
}
return (byte)iReturnResult;
}
//**************************************
//***
//***
//***
//**************************************
BOOL CCommProc::Get485Data(byte byHC,byte byDC,byte *pby485Data,byte by485DataLen,byte *pbyX10Frame,int &iLen)
{
byte byDest,byTemp,byExtCode,byCommand;
int i,j;
byExtCode=0x1B;//11011;
byCommand=0x01;
FMoveBit(byHC,4,4,byExtCode,3,byDest);
pbyX10Frame[0]=byDest;
FMoveBit(byExtCode,7,7,byDC,4,byDest);
byTemp=byDest;
FMoveBit(byTemp,0,3,by485DataLen,0,byDest);
pbyX10Frame[1]=byDest;
FMoveBit(by485DataLen,3,3,byCommand,0,byDest);
pbyX10Frame[2]=byDest;
FMoveBit(byCommand,3,3,pby485Data[0],0,byDest);
pbyX10Frame[3]=byDest;
j=1;
for(i=1;i<by485DataLen-1;i++)
{
FMoveBit(pby485Data[i],3,3,pby485Data[i+1],0,byDest);
pbyX10Frame[3+j]=byDest;
j++;
}
FMoveBit(pby485Data[by485DataLen-1],3,3,0,0,byDest);
pbyX10Frame[3+j]=byDest;
iLen=4+j;
return true;
}
//*****************************************************
//***//配置485帧头的x10命令解析保存到byte数组中Set485FrameHead
//***
//******************************************************
BOOL CCommProc::Set485FrameHead(byte byHC,byte byDC,byte *pby485Data,byte by485DataLen,byte *pbyX10Frame,int &iLen)
{
byte byDest,byTemp,byExtCode,byCommand;
int i,j;
byExtCode=0x1B;//11011;
byCommand=0x04;
FMoveBit(byHC,4,4,byExtCode,3,byDest);
pbyX10Frame[0]=byDest;
FMoveBit(byExtCode,7,7,byDC,4,byDest);
byTemp=byDest;
FMoveBit(byTemp,0,3,by485DataLen,0,byDest);
pbyX10Frame[1]=byDest;
FMoveBit(by485DataLen,3,3,byCommand,0,byDest);
pbyX10Frame[2]=byDest;
FMoveBit(byCommand,3,3,pby485Data[0],0,byDest);
pbyX10Frame[3]=byDest;
j=1;
for(i=1;i<by485DataLen-1;i++)
{
FMoveBit(pby485Data[i],3,3,pby485Data[i+1],0,byDest);
pbyX10Frame[3+j]=byDest;
j++;
}
FMoveBit(pby485Data[by485DataLen-1],3,3,0,0,byDest);
pbyX10Frame[3+j]=byDest;
iLen=4+j;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -