| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #include "StdAfx.h"
- #include "PduEntityHead.h"
- CPduEntityHead::CPduEntityHead(void)
- {
- Initialization(0, PDU_DEV_TYPE_UNKNOWN, 0, PDU_DEV_TYPE_UNKNOWN, PDU_CMD_UNKNOWN, FALSE, 0);
- }
- CPduEntityHead::~CPduEntityHead(void)
- {
- }
- /*****************************************************************
- **【函数名称】 UShortToBytes
- **【函数功能】 32位整数转化为4字节
- **【参数】 nValue:16位整数
- ** byte1-byte4:32位整数的从高到低位对应的4个字节
- **【返回值】
- ****************************************************************/
- void CPduEntityHead::UShortToBytes(USHORT nValue, UCHAR *byte1, UCHAR *byte2)
- {
- *byte2 = (unsigned char)(nValue & 0xFF);
- *byte1 = (unsigned char)((nValue >> 8) & 0xFF);
- }
- /*****************************************************************
- **【函数名称】 BytesToUShort
- **【函数功能】 2字节转化为16位整数
- **【参数】 byte1:16位整数的高位字节
- ** byte2:16位整数的低位字节
- **【返回值】 16位整数
- ****************************************************************/
- USHORT CPduEntityHead::BytesToUShort(UCHAR byte1, UCHAR byte2)
- {
- unsigned short nValue;
- nValue = byte1;
- nValue = nValue << 8;
- nValue ^= byte2;
- return nValue;
- }
- /*****************************************************************
- **【函数名称】 ULongToBytes
- **【函数功能】 32位整数转化为4字节
- **【参数】 nValue:32位整数
- ** byte1-byte4:32位整数的从高到低位对应的4个字节
- **【返回值】
- ****************************************************************/
- void CPduEntityHead::ULongToBytes(ULONG nValue, UCHAR *byte1, UCHAR *byte2, UCHAR *byte3, UCHAR *byte4)
- {
- *byte4 = (unsigned char)(nValue & 0xFF);
- *byte3 = (unsigned char)((nValue >> 8) & 0xFF);
- *byte2 = (unsigned char)((nValue >> 16) & 0xFF);
- *byte1 = (unsigned char)((nValue >> 24) & 0xFF);
- }
- /*****************************************************************
- **【函数名称】 BytesToULong
- **【函数功能】 4字节转化为32位整数
- **【参数】 byte1-byte4:32位整数的从高到低位对应的4个字节
- **【返回值】 32位整数
- ****************************************************************/
- ULONG CPduEntityHead::BytesToULong(UCHAR byte1, UCHAR byte2, UCHAR byte3, UCHAR byte4)
- {
- unsigned long nValue;
- nValue = byte1;
- nValue = nValue << 8;
- nValue ^= byte2;
- nValue = nValue << 8;
- nValue ^= byte3;
- nValue = nValue << 8;
- nValue ^= byte4;
- return nValue;
- }
- /*****************************************************************
- **【函数名称】 Initialization
- **【函数功能】 初始化函数
- **【参数】 本端设备ID,本端设备类型,对端设备ID,对端设备类型,
- ** 具体命令类型,具体命令占用BUF长度
- **【返回值】
- ****************************************************************/
- void CPduEntityHead::Initialization(ULONG a_nSrcId, PDU_DEV_TYPE a_nSrcType, ULONG a_nDstId, PDU_DEV_TYPE a_nDstType, PDU_CMD_TYPE a_nCmdType, BOOL a_bIsExecReturn, USHORT a_nDataLen)
- {
- m_nSrcId = a_nSrcId;
- m_nSrcType = a_nSrcType;
- m_nDstId = a_nDstId;
- m_nDstType = a_nDstType;
- m_nCmdType = a_nCmdType;
- m_nDataLen = a_nDataLen;
- m_nIsExecReturn = a_bIsExecReturn ? 1 : 0;
- m_nUnusedType2 = 0;
- m_nCrcCheck = 0;
- }
- /*****************************************************************
- **【函数名称】 Incode
- **【函数功能】 打包包头
- **【参数】
- **【返回值】 szBuf[]
- ****************************************************************/
- BOOL CPduEntityHead::Incode(UCHAR szBuf[])
- {
- ULongToBytes(m_nSrcId, &(szBuf[0]), &(szBuf[1]), &(szBuf[2]), &(szBuf[3]));
- szBuf[4]=(unsigned char)m_nSrcType;
- ULongToBytes(m_nDstId, &(szBuf[5]), &(szBuf[6]), &(szBuf[7]), &(szBuf[8]));
- szBuf[9] = (unsigned char)m_nDstType;
- UShortToBytes((unsigned short)m_nCmdType,&(szBuf[10]),&(szBuf[11]));
- ULongToBytes(m_nIsExecReturn,&(szBuf[12]),&(szBuf[13]),&(szBuf[14]),&(szBuf[15]));
- ULongToBytes(m_nUnusedType2,&(szBuf[16]),&(szBuf[17]),&(szBuf[18]),&(szBuf[19]));
- UShortToBytes((unsigned short)m_nDataLen,&(szBuf[20]),&(szBuf[21]));
- ULongToBytes(m_nCrcCheck,&(szBuf[22]),&(szBuf[23]),&(szBuf[24]),&(szBuf[25]));
- return TRUE;
- }
- /*****************************************************************
- **【函数名称】 Decode
- **【函数功能】 解析包头
- **【参数】 szBuf[]
- **【返回值】
- ****************************************************************/
- BOOL CPduEntityHead::Decode(UCHAR szBuf[])
- {
- m_nSrcId = BytesToULong(szBuf[0],szBuf[1],szBuf[2],szBuf[3]);
- m_nSrcType = (PDU_DEV_TYPE)szBuf[4];
- m_nDstId = BytesToULong(szBuf[5],szBuf[6],szBuf[7],szBuf[8]);
- m_nDstType = (PDU_DEV_TYPE)szBuf[9];
- m_nCmdType = (PDU_CMD_TYPE)BytesToUShort(szBuf[10],szBuf[11]);
- m_nIsExecReturn = BytesToULong(szBuf[12],szBuf[13],szBuf[14],szBuf[15]);
- m_nUnusedType2 = BytesToULong(szBuf[16],szBuf[17],szBuf[18],szBuf[19]);
- m_nDataLen = BytesToUShort(szBuf[20],szBuf[21]);
- m_nCrcCheck = BytesToULong(szBuf[22],szBuf[23],szBuf[24],szBuf[25]);
- return m_nCrcCheck == 0 ? TRUE : FALSE;
- }
|