////////////////////////////////////////////////////////////////////////////// ////// ////// ////// 文 件: 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