📄 commproc.cpp
字号:
//************************************************
//***////配置485帧的起始位置和长度的x10命令解析保存到byte数组中Set485Frame
//***
//************************************************
BOOL CCommProc::Set485Frame(byte byHC,byte byDC,byte Byte1,byte Byte2,byte *pbyX10Frame,int &iLen)
{
//配置抄表485协议数据长度位置及长度
byte byDest,byTemp,byExtCode,byCommand;
byExtCode=0x1B;//11011;
byCommand=0x05;
FMoveBit(byHC,4,4,byExtCode,3,byDest);
pbyX10Frame[0]=byDest;
FMoveBit(byExtCode,7,7,byDC,4,byDest);
byTemp=byDest;
FMoveBit(byTemp,0,3,0x02,0,byDest);
pbyX10Frame[1]=byDest;
FMoveBit(0x02,3,3,byCommand,0,byDest);
pbyX10Frame[2]=byDest;
FMoveBit(byCommand,3,3,Byte1,0,byDest);
pbyX10Frame[3]=byDest;
FMoveBit(Byte1,3,3,Byte2,0,byDest);
pbyX10Frame[4]=byDest;
FMoveBit(Byte2,3,3,0,0,byDest);
pbyX10Frame[5]=byDest;
iLen=6;
return true;
}
BOOL CCommProc::Get485State(byte byHC,byte byDC,byte *pbyX10Frame,int &iLen)
{
//查询485通讯状态
byte byDest,byTemp,byExtCode,byCommand;
byExtCode=0x1B;//11011;
byCommand=0x06;
FMoveBit(byHC,4,4,byExtCode,3,byDest);
pbyX10Frame[0]=byDest;
FMoveBit(byExtCode,7,7,byDC,4,byDest);
byTemp=byDest;
FMoveBit(byTemp,0,3,0x02,0,byDest);
pbyX10Frame[1]=byDest;
FMoveBit(0x02,3,3,byCommand,0,byDest);
pbyX10Frame[2]=byDest;
FMoveBit(byCommand,3,3,0,0,byDest);
pbyX10Frame[3]=byDest;
pbyX10Frame[4]=0x00;
iLen=5;
return true;
}
//********************************************
//***//配置485的波特率的x10命令解析保存到byte数组中Set485Baud
//***
//********************************************
BOOL CCommProc::Set485Baud(byte byHC,byte byDC,byte byBaud,byte *pbyX10Frame,int &iLen)
{
byte byDest,byTemp,byExtCode,byCommand;
byExtCode=0x1B;//11011;
byCommand=0x07;
FMoveBit(byHC,4,4,byExtCode,3,byDest);
pbyX10Frame[0]=byDest;
FMoveBit(byExtCode,7,7,byDC,4,byDest);
byTemp=byDest;
FMoveBit(byTemp,0,3,0x01,0,byDest);
pbyX10Frame[1]=byDest;
FMoveBit(0x01,3,3,byCommand,0,byDest);
pbyX10Frame[2]=byDest;
FMoveBit(byCommand,3,3,byBaud,0,byDest);
pbyX10Frame[3]=byDest;
FMoveBit(byBaud,3,3,0,0,byDest);
pbyX10Frame[4]=byDest;
iLen=5;
return true;
}
//*********************************************
//***还原
//***
//*********************************************
BOOL CCommProc::mRelease(byte *pSource,byte *pDest,int &iRet)
{
int iFrameLen,i,j;
byte byTemp1,byTemp2,byDest;
pDest[0]=pSource[0];//帧头1
pDest[1]=pSource[1];//帧头2
pDest[2]=pSource[2];//帧长
pDest[3]=pSource[3];//保留位
iFrameLen=pSource[2];
byTemp1=pSource[4];
GetReleaseByte(byTemp1,0,4,NULL,0,0,byDest);
pDest[4]=byDest;//HC
byTemp1=pSource[4];
byTemp2=pSource[5];
GetReleaseByte(byTemp1,4,3,byTemp2,0,7,byDest);
pDest[5]=byDest;//ExtCode
byTemp1=pSource[5];
GetReleaseByte(byTemp1,1,4,NULL,0,0,byDest);
pDest[6]=byDest;//DC
j=0;
for(i=5;i<iFrameLen;i++)
{
byTemp1=pSource[i];
byTemp2=pSource[i+1];
GetReleaseByte(byTemp1,5,0,byTemp2,0,3,byDest);
pDest[i+2]=byDest;
j++;
}
iRet=6+j;
pDest[7+j]=pSource[iFrameLen+1];//校验和
return true;
}
BOOL CCommProc::GetReleaseByte(const byte btSource1,int iToFront1,int iToBack1,
const byte btSource2,int iToFront2,int iToBack2,byte &byDest)
{
byte byTemp1,byTemp2;
byTemp1=0;
byTemp2=0;
byTemp1=btSource1;
byTemp1=(byTemp1<<iToFront1);
byTemp1=(byTemp1>>iToBack1);
if(btSource2!=NULL)
{
byTemp2=btSource2;
byTemp2=(byTemp2<<iToFront2);
byTemp2=(byTemp2>>iToBack2);
}
byDest=byTemp1+byTemp2;
return true;
}
/*******************************************************************
函数名称: HexChartobyte()
函数功能: 将无符号的字符char转换成byte
函数返回值: 函数返回值为byte
函数参数:
参数1: unsigned char cHexChar,为源字符,表示输入参数
调用关系:
Calls: 本函数调用的函数清单Calls:无
Call by:
*******************************************************************/
byte CCommProc::HexChartobyte(unsigned char cHexChar)
{
int iRet;
switch (cHexChar)
{
case '0':
iRet = 0;
break;
case '1':
iRet = 1;
break;
case '2':
iRet = 2;
break;
case '3':
iRet = 3;
break;
case '4':
iRet = 4;
break;
case '5':
iRet = 5;
break;
case '6':
iRet = 6;
break;
case '7':
iRet = 7;
break;
case '8':
iRet = 8;
break;
case '9':
iRet = 9;
break;
case 'a':
case 'A':
iRet = 10;
break;
case 'b':
case 'B':
iRet = 11;
break;
case 'c':
case 'C':
iRet = 12;
break;
case 'd':
case 'D':
iRet = 13;
break;
case 'e':
case 'E':
iRet = 14;
break;
case 'f':
case 'F':
iRet = 15;
break;
default:
iRet = 0;
break;
}
return iRet;
}
//写Edit方法
void CCommProc::WriteString(CString strContent,CEdit *pViewText, CString strSeparate)
{
int iLen;
iLen=pViewText->GetWindowTextLength();
pViewText->SetSel(iLen,iLen);
pViewText->ReplaceSel(strContent);
iLen=pViewText->GetWindowTextLength();
pViewText->SetSel(iLen,iLen);
pViewText->ReplaceSel(strSeparate);
}
//*************************
//********将byte型转换为Bin(二进制)*****************
//*************************
CString CCommProc::ByteToStrBin(byte* bySource,int iCount,int iStart) //将byte型转换为Bin(二进制)
{
CString strReturnVale;
strReturnVale="";
byte byTemp,byTemp1;
char pcTemp[2]={0x00,0x00};
for(int i=iStart;i<iCount+iStart;i++)
{
byTemp=0;
byTemp=bySource[i];
for(int j=7;j>=0;j--)
{
byTemp1=( 1 << j );
byTemp1=byTemp/byTemp1;
if(byTemp1==1)
{
strReturnVale+="1";
byTemp-=(1 << j);
}
else
strReturnVale+="0";
}
strReturnVale+=" ";
}
return strReturnVale;
}
//*************************
//********将byte型转换为Dec(八进制)*****************
//*************************
CString CCommProc::ByteToStrDec(byte* bySource,int iCount,int iStart)
{
CString strReturnVale;
int iTemp;
CString strTemp,strTemp1;
char pcTemp[4];
memset(pcTemp,0x00,4);
for(int i=iStart;i<iCount+iStart;i++)
{
iTemp=bySource[i];
itoa(iTemp,pcTemp,10);
strTemp=pcTemp;
strTemp1="";
for(int j=0;j< (3 - strlen(strTemp));j++)
strTemp1+=" ";
strTemp=strTemp1+strTemp;
strReturnVale+=strTemp+" ";
memset(pcTemp,0x00,4);
}
return strReturnVale;
}
//*************************
//********将byte型转换为HEX(十六进制)*****************
//*************************
CString CCommProc::ByteToStrHex(byte* bySource,int iCount,int iStart) //将byte型转换为HEX(十六进制)
{
CString strReturnVale;
CString strTemp;
char pcTemp[5];
for(int i=iStart;i<iCount+iStart;i++)
{
memset(pcTemp,0x00,5);
sprintf(pcTemp,"%X",bySource[i]);
strTemp=pcTemp;
if(strlen(strTemp)<2)
strTemp="0"+strTemp;
strTemp="0x"+strTemp;
strReturnVale+=strTemp+" ";
}
return strReturnVale;
}
/*******************************************************************
函数名称: ByteToStrHexNew()
函数功能: 将byte型转换为HEX(十六进制)
函数返回值: 返回转换结果为CString,为字符串类型
函数参数:
参数1: byte* bySource 指向要转换的byte 型数组,表示源字符串为传入参数;
参数2: int iCount 为要转换的个数,为传入参数;
参数3: int iStart 为开始转换的位置,表示从第几为开始进行转换;为传入参数;
调用关系:
Calls: 无
Call by:
*******************************************************************/
CString CCommProc::ByteToStrHexNew(byte* bySource,int iCount,int iStart) //将byte型转换为HEX(十六进制)
{
CString strReturnVale;
CString strTemp;
char pcTemp[5];
for(int i = iStart;i < iCount+iStart; i++)
{
memset(pcTemp,0x00,5);
sprintf(pcTemp,"%X",bySource[i]);
strTemp = pcTemp;
if(strlen(strTemp) < 2)
strTemp = "0" + strTemp;
//strTemp = "0x" + strTemp;
strTemp = strTemp;
strReturnVale += strTemp + " ";
}
return strReturnVale;
}
//*************************
//********将byte型转换为Char(字符)*****************
//*************************
CString CCommProc::ByteToStrChar(byte* bySource,int iCount,int iStart)
{
CString strReturnVale;
char cTemp;
for(int i=iStart;i<iCount+iStart;i++)
{
cTemp=1;
if(bySource[i]>31 && bySource[i]<129)
cTemp=bySource[i];
strReturnVale+=cTemp;
strReturnVale+=" ";
}
return strReturnVale;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -