| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755 |
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: BatteryAdu2000.cpp //////
- ////// 作 者: Wangjianfeng //////
- ////// 创建时间: //////
- ////// 说 明: ADU2000协议 //////
- ////// //////
- ////// 修改时间:2010-12-08 //////
- ////// 修改说明:数据转换 //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- #include "StdAfx.h"
- #include "CommProcess.h"
- #include ".\batteryadu2000.h"
- CBatteryAdu2000::CBatteryAdu2000(char *szPath,char *szIniName,int nCommPort,int nAddr,int nRate,int nDataBit,int nStopBit,int nParity,int nInterval )
- {
- #if IS_USE_READMSG_CS
- InitializeCriticalSection(&m_csReadMsg);
- #endif
- MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
- for( int i = 0; i < MAX_ADDR; i++ )
- {
- memset(m_Adu_FFMsg[i], 0, sizeof(m_Adu_FFMsg[i]));
- memset(m_Adu_81Msg[i], 0, sizeof(m_Adu_81Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CBatteryAdu2000::~CBatteryAdu2000()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL CBatteryAdu2000::AduOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
- {
- BOOL bResult = FALSE;
- bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
- return bResult;
- }
- int CBatteryAdu2000::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
- GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
- szDataType[strlen(szDataType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- iSBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值
- iEBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile);
- return 0;
- }
- int CBatteryAdu2000::SendReadRequest(
- char *szPath, // 程序所在路径
- char *szIniName, // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char *szCmd, // 请求命令
- char *szMsg, // 响应的值
- int nReversed1, // 预留整形参数1接口
- int nReversed2, // 预留整形参数2接口
- int nReversed3, // 预留整形参数3接口
- int nReversed4, // 预留整形参数4接口
- int nReversed5, // 预留整形参数5接口
- float fReversed1, // 预留float参数1接口
- float fReversed2, // 预留float参数2接口
- float fReversed3, // 预留float参数3接口
- char *szReversed1, // 预留字符数组参数1接口
- char *szReversed2, // 预留字符数组参数2接口
- char *szReversed3, // 预留字符数组参数3接口
- char *szReversed4, // 预留字符数组参数4接口
- char *szReversed5 // 预留字符数组参数5接口
- )
- {
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL )
- {
- LOG4C((LOG_NOTICE,"未找到串口句柄"));
- return -1;
- }
- int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
- char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
- GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
- int nRet = -1;
- int iCmd = atoi(szCmd + 4);
- if (
- (strlen(m_Adu_FFMsg[nAddr -1]) == 0 && (iCmd >= 1 && iCmd <= 46)) || iCmd == 1 ||
- (strlen(m_Adu_81Msg[nAddr -1]) == 0 && (iCmd >= 47 && iCmd <= 81)) || iCmd == 49
- )
- {
- nRet = GetDeviceParam(nAddr,pComm,szCmd,IniSendCMD,IniSendlen );
- if( nRet != 0 ) return nRet;
- }
- if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- {
- m_devOnline[nAddr - 1] = FALSE;
- }
- else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- {
- m_devOnline[nAddr - 1] = TRUE;
- }
- if( m_devOnline[nAddr - 1] == FALSE )
- {
- return -1;
- }
- nRet = GetAdu_CommandFFVarMsg(nAddr, szCmd, szMsg, nIndex, nLen, iSBit, iEBit, szDataType);
- nRet = GetAdu_Command81VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, iSBit, iEBit, szDataType);
- //LOG4C((LOG_NOTICE, "szCmd = %s, szMsg = %s ,nIndex = %d ,return nRet = %d", szCmd, szMsg,nIndex, nRet));
- return nRet;
- }
- int CBatteryAdu2000::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
- {
- int nRet = -1;
- nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd);
- return nRet;
- }
- int CBatteryAdu2000::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
- {
- int nRet = -1;
- #if DEBUG_ADU
- int iLen = sizeof(REQUEST_STRUCT);
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- REQUEST_STRUCT RequestPara;
- memset( &RequestPara, 0, iLen );
- //起始位
- RequestPara.headMes.Start = 0x7E;
- //通讯协议版本
- RequestPara.headMes.Version[0] = 0x32;
- RequestPara.headMes.Version[1] = 0x30;
- //设备地址描述
- RequestPara.headMes.Address[0] = ByteToAscii((nAddr>>4) & 0x0f);
- RequestPara.headMes.Address[1] = ByteToAscii(nAddr & 0x0f);
- //Cid1
- RequestPara.headMes.Cid1[0] = 0x34;
- RequestPara.headMes.Cid1[1] = 0x36;
- RequestPara.headMes.Cid2[0] = 0x34;
- RequestPara.headMes.Cid2[1] = 0x31;
- RequestPara.headMes.Lenth[0] = 0x45;
- RequestPara.headMes.Lenth[1] = 0x30;
- RequestPara.headMes.Lenth[2] = 0x30;
- RequestPara.headMes.Lenth[3] = 0x32;
- memcpy(RequestPara.GroupInfo, IniSendCMD, IniSendlen);
- //校验码
- BYTE *pDataBuf = new BYTE[ iLen - sizeof(RequestPara.chkSum) - 1 ];
- memset(pDataBuf, 0, iLen - sizeof(RequestPara.chkSum) - 1 );
- memcpy(pDataBuf, &RequestPara, iLen - sizeof(RequestPara.chkSum) - 1 );
- GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.chkSum) - 2 );
- delete[] pDataBuf;
- pDataBuf = NULL;
- RequestPara.chkSum[0] = chChkSum[0];
- RequestPara.chkSum[1] = chChkSum[1];
- RequestPara.chkSum[2] = chChkSum[2];
- RequestPara.chkSum[3] = chChkSum[3];
- //结束符
- RequestPara.End = 0x0D;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- int nDataLen = (int)sizeof(RequestPara);
- ResetEvent( m_hSemComm ); //设置无信号;
- int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
- if( nResult == nDataLen )
- {
- nRet = 0;
- }
- else
- {
- LOG4C((LOG_NOTICE,"写入无效"));
- SetEvent( m_hSemComm );
- return EER_CODE_ADU_COM_WRITE_DATA;
- }
- }
- else //如果没信号,返回错误;
- {
- LOG4C((LOG_NOTICE,"线程无信号"));
- return ERR_CODE_ADU_COM_BUSY;
- }
- Sleep(150);
- #endif
- return 0;
- }
- int CBatteryAdu2000::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
- {
- #if DEBUG_ADU
- Sleep(150);
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- RESPONSE_STRUCT structResponse;
- memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) );
- nReceiveLen = sizeof(RESPONSE_STRUCT);
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- //nReadLen才是实际返回的长度;
- nReadLen = pComm->Read((BYTE *)pBuffer,nReceiveLen);
- //LOG4C((LOG_NOTICE,"ADU2000缓存区大小 = %d,实际大小 = %d",nReceiveLen,nReadLen));
- if( nReadLen <= 0)
- {
- LOG4C((LOG_NOTICE, "ADU2000 串口没有读到数据"));
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_ADU_COM_READ_NO_DATA;
- }
- if (LengthCheck(pBuffer,nReadLen) != 0)
- {
- SetEvent(m_hSemComm);
- LOG4C((LOG_NOTICE, "ADU2000 长度校验错误"));
- if (pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- if (!ChkSumCheck(pBuffer, nReadLen))
- {
- LOG4C((LOG_NOTICE, "ADU2000 校验码校验出错"));
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
- }
- if (RtnCheck(pBuffer) != 0)
- {
- LOG4C((LOG_NOTICE, "ADU2000 RTN校验出错"));
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_ERROR; //RTN校验出错
- }
- SetAduCommandFFVarMsg(nAddr, szCmd, pBuffer);
- SetAduCommand81VarMsg(nAddr, szCmd, pBuffer);
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- // 设置串口等待事件为有信号
- SetEvent(m_hSemComm);
- if( pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- int CBatteryAdu2000::GetAdu_CommandFFVarMsg(int nAddr,char *szCmd, char *szRecvMsg, int &nIndex,int &nLen, int &SBit, int &EBit, char *szDataType)
- {
- int nRet = 0;
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 46)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szDataType, m_Adu_FFMsg[nAddr - 1] + nIndex, szRecvMsg, nLen, SBit, EBit);
- //LOG4C((LOG_NOTICE,"nIndex = %d, nLen = %d, szMsg = %s",nIndex, nLen , szMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CBatteryAdu2000::GetAdu_Command81VarMsg(int nAddr,char *szCmd, char *szRecvMsg, int &nIndex,int &nLen, int &SBit, int &EBit, char *szDataType)
- {
- int nRet = 0;
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 47 && iCmd <= 82)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szDataType, m_Adu_81Msg[nAddr - 1] + nIndex, szRecvMsg, nLen, SBit, EBit);
- //LOG4C((LOG_NOTICE,"nIndex = %d, nLen = %d, szMsg = %s",nIndex, nLen , szMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- void CBatteryAdu2000::SetAduCommandFFVarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 1 && iCmd <= 46)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_Adu_FFMsg[nAddr - 1], pBuffer, sizeof(m_Adu_FFMsg[nAddr - 1]));
- //LOG4C((LOG_NOTICE , "m_Adu_FFMsg = %s",m_Adu_FFMsg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CBatteryAdu2000::SetAduCommand81VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- int iCmd = atoi(szCmd + 4);
- if ( iCmd >= 47 && iCmd <= 82)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_Adu_81Msg[nAddr - 1], pBuffer, sizeof(m_Adu_81Msg[nAddr - 1]));
- //LOG4C((LOG_NOTICE , "m_Adu_81Msg = %s",m_Adu_81Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- // 发送设置设备参数请求
- int CBatteryAdu2000::SendSetReuest(
- char *szPath, // 程序所在路径
- char *szIniName, // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char *szCmd, // 请求命令
- char *szMsg, // 响应的值
- int nReversed1, // 预留整形参数1接口
- int nReversed2, // 预留整形参数2接口
- int nReversed3, // 预留整形参数3接口
- int nReversed4, // 预留整形参数4接口
- int nReversed5, // 预留整形参数5接口
- float fReversed1, // 预留float参数1接口
- float fReversed2, // 预留float参数2接口
- float fReversed3, // 预留float参数3接口
- char *szReversed1, // 预留字符数组参数1接口
- char *szReversed2, // 预留字符数组参数2接口
- char *szReversed3, // 预留字符数组参数3接口
- char *szReversed4, // 预留字符数组参数4接口
- char *szReversed5 // 预留字符数组参数5接口
- )
- {
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
- char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
- GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
- char szRecvMsg[20] = {0};
- if (strcmp(szCmd,"cmd-83") == 0 )
- {
- nRet = SetDeviceParam( nAddr,pComm,szCmd,szMsg,szRecvMsg,IniSendCMD,IniSendlen/*,nIndex, nLen, szDataType*/ );
- if( nRet != 0 ) return nRet;
- }
- return -1;
- }
- int CBatteryAdu2000::SetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *szSetMsg,char *szRecvMsg,char *IniSendCMD, const int &IniSendlen)/*,const int &nIndex, const int &nlen, char *szDataType)*/
- {
- int nRet = -1;
- nRet = Send_WriteDeviceData(nAddr,pComm,szCmd,szSetMsg,IniSendCMD,IniSendlen);
- if( nRet != 0 )
- {
- return nRet;
- }
- nRet = Recv_WriteDeviceData(nAddr, pComm, szCmd,szRecvMsg);
- return nRet;
- }
- int CBatteryAdu2000:: Send_WriteDeviceData(int naddr, CCommProcess *pComm, char *szCmd, char *szSetMsg, char *IniSendCMD, const int &IniSendlen )
- {
- BYTE bu[500] = {0};
- pComm->Read(bu,500);
- int nRet = -1;
- #if DEBUG_ADU
- int iLen = sizeof(WREQUEST_STRUCT);
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- WREQUEST_STRUCT RequestPara;
- memset( &RequestPara, 0, iLen );
- //起始位
- RequestPara.headMes.Start = 0x7E;
- //通讯协议版本
- RequestPara.headMes.Version[0] = 0x32;
- RequestPara.headMes.Version[1] = 0x30;
- //设备地址描述
- RequestPara.headMes.Address[0] = ByteToAscii((naddr>>4) & 0x0f);
- RequestPara.headMes.Address[1] = ByteToAscii(naddr & 0x0f);
- //Cid1
- RequestPara.headMes.Cid1[0] = 0x34;
- RequestPara.headMes.Cid1[1] = 0x36;
- RequestPara.headMes.Cid2[0] = 0x46;
- RequestPara.headMes.Cid2[1] = 0x33;
- RequestPara.headMes.Lenth[0] = 0x41;
- RequestPara.headMes.Lenth[1] = 0x30;
- RequestPara.headMes.Lenth[2] = 0x30;
- RequestPara.headMes.Lenth[3] = 0x36;
- //memcpy(RequestPara.GroupInfo, chSendMsg, 2);
- RequestPara.GroupInfo[0] = 0x33;
- RequestPara.GroupInfo[1] = 0x31;
- RequestPara.GroupInfo[2] = 0x46;
- RequestPara.GroupInfo[3] = 0x46;
- RequestPara.GroupInfo[4] = 0x30;
- RequestPara.GroupInfo[5] = 0x31;
-
- //校验码
- BYTE *pDataBuf = new BYTE[ iLen - sizeof(RequestPara.chkSum) - 1 ];
- memset(pDataBuf, 0, iLen - sizeof(RequestPara.chkSum) - 1 );
- memcpy(pDataBuf, &RequestPara, iLen - sizeof(RequestPara.chkSum) - 1 );
- GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.chkSum) - 2 );
- delete[] pDataBuf;
- pDataBuf = NULL;
- RequestPara.chkSum[0] = chChkSum[0];
- RequestPara.chkSum[1] = chChkSum[1];
- RequestPara.chkSum[2] = chChkSum[2];
- RequestPara.chkSum[3] = chChkSum[3];
- //结束符
- RequestPara.End = 0x0D;
- int nDataLen = (int)sizeof(RequestPara);
- ResetEvent( m_hSemComm ); //设置无信号;
- int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
- //LOG4C((LOG_NOTICE , "Write"));
- if( nResult == nDataLen )
- {
- nRet = 0;
- }
- else
- {
- SetEvent( m_hSemComm );
- LOG4C((LOG_NOTICE,"设置数据无效!"));
- return EER_CODE_ADU_COM_WRITE_DATA;
- }
- #endif
- return 0;
- }
- int CBatteryAdu2000::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg )
- {
- #if DEBUG_ADU
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- nReceiveLen = 20;
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- nReadLen = pComm->Read((BYTE *)pBuffer,nReceiveLen);
- if( nReadLen <= 0)
- {
- //LOG4C((LOG_NOTICE, "ADU2000 串口没有读到数据"));
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_ADU_COM_READ_NO_DATA;
- }
- if (LengthCheck(pBuffer,nReadLen) != 0)
- {
- SetEvent(m_hSemComm);
- //LOG4C((LOG_NOTICE, "ADU2000 长度校验错误"));
- if (pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- if (!ChkSumCheck(pBuffer, nReadLen))
- {
- //LOG4C((LOG_NOTICE, "ADU2000 校验码校验出错"));
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
- }
- if (RtnCheck(pBuffer) != 0)
- {
- //LOG4C((LOG_NOTICE, "ADU2000 RTN校验出错"));
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_ERROR; //RTN校验出错
- }
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- // 设置串口等待事件为有信号
- SetEvent(m_hSemComm);
- if( pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- #endif
- return 0;
- }
- void CBatteryAdu2000::SimulationCommData(int nAddr)
- {
- // m_Adu_FFMsg[0][0] = ;
- // m_Adu_81Msg[0][0] = ;
- }
- WORD CBatteryAdu2000::RtnCheck(char Msg[VAR_MSG])
- {
- char buffer[3] = {0};
- int j = 0;
- for (int i = 7; i < 9; i++)
- {
- buffer[j++] = Msg[i];
- }
- buffer[j] = '\0';
- if (strcmp(buffer,"00") == 0)
- {
- return 0;
- }
- else if (strcmp(buffer,"01") == 0)
- {
- return ERR_CODE_RTN_VER_ERROR;
- }
- else if (strcmp(buffer,"02") == 0)
- {
- return ERR_CODE_RTN_CHKSUM_ERROR;
- }
- else if (strcmp(buffer,"03") == 0)
- {
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- else if (strcmp(buffer,"04") == 0)
- {
- return ERR_CODE_RTN_CID_ERROR;
- }
- else if (strcmp(buffer,"05") == 0)
- {
- return ERR_CODE_RTN_COMMAND_FORMAT;
- }
- else if (strcmp(buffer,"06") == 0)
- {
- return ERR_CODE_RTN_INVALID_DATA;
- }
- return 0;
- }
- UINT CBatteryAdu2000::GetCheckSum(char *pBuf, char chDest[5], int len)
- {
- WORD iSum = 0;
- for(int i=0; i<len; i++)//求和
- {
- iSum += pBuf[i];
- }
- WORD iCompliment = iSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- itoa(iCompliment, chDest, 16);
- chDest[0] = lowercase2uppercase(chDest[0]);
- chDest[1] = lowercase2uppercase(chDest[1]);
- chDest[2] = lowercase2uppercase(chDest[2]);
- chDest[3] = lowercase2uppercase(chDest[3]);
- return atoi(chDest);
- }
- BYTE CBatteryAdu2000::GetLengthSum(BYTE szChar[])
- {
- unsigned int nData = 0;
- unsigned int nData1 =0;
- unsigned short nDataLength = 0;
- unsigned short nChar1;
- nChar1 =AsciiToBYTE(szChar[1]) +
- AsciiToBYTE(szChar[2]) +
- AsciiToBYTE(szChar[3]);
- nChar1 = (~nChar1) + 1;
- nChar1 %=16;
- return (BYTE)nChar1;
- }
- int CBatteryAdu2000::LengthCheck(char szSendMsg[],int nReadLen)
- {
- int len = (int)strlen(szSendMsg);
- char buffer[5] = {0};
- int j = 0;
- for (int i = 9; i< 13; i++)
- {
- buffer[j++] = szSendMsg[i]; //取出检验位
- }
- //buffer[j] ='0';
- if(!ValiateLength(buffer)) // 相加模十六取反加一
- {
- LOG4C((LOG_NOTICE,"ADU2000长度校验错误"));
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- /*转换为十六进制*/
- return 0;
- }
- BOOL CBatteryAdu2000::ValiateLength(char szChar[])
- {
- unsigned int nData = 0;
- unsigned int nData1 =0;
- unsigned short nDataLength = 0;
- unsigned short nChar1;
- nChar1 =AsciiToBYTE(szChar[1]) + AsciiToBYTE(szChar[2]) + AsciiToBYTE(szChar[3]);
- nChar1 = (~nChar1) + 1;
- nChar1 %=16;
- nDataLength = AsciiToBYTE(szChar[0]);
- if (nDataLength != nChar1)
- {
- return FALSE;
- }
- return TRUE;
- }
|