| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: BatteryAdu.cpp //////
- ////// 作 者: Suguobing //////
- ////// 创建时间: //////
- ////// 说 明: ADU1000协议 //////
- ////// //////
- ////// 修改时间:2010-11-23 //////
- ////// 修改说明:数据转换 //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommProcess.h"
- #include "BatteryAdu.h"
- CBatteryAdu::CBatteryAdu( char szPath[MAX_PATH],
- char szIniName[MAX_PATH],
- 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_41Msg[i], 0, sizeof(m_Adu_41Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CBatteryAdu::~CBatteryAdu()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL CBatteryAdu::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 CBatteryAdu::SendReadRequest(
- char szPath[MAX_PATH],
- char szIniName[MAX_PATH],
- int nCommPort,
- int nAddr,
- char szCmd[MAX_CMD],
- char szMsg[VAR_MSG],
- int nReversed1,
- int nReversed2,
- int nReversed3,
- int nReversed4,
- int nReversed5,
- float fReversed1,
- float fReversed2,
- float fReversed3,
- char szReversed1[MAX_RESERVED1],
- char szReversed2[MAX_RESERVED2],
- char szReversed3[MAX_RESERVED3],
- char szReversed4[MAX_RESERVED4],
- char szReversed5[MAX_RESERVED5]
- )
- {
- int nIndex = 0, nLen = 0;
- char szSendMsg[ADU_SEND_MSG] = {0};
- char szType[ADU_TYPE_LENGTH] = {0};
- int startBit = 0;
- int endBit = 0;
- GetAduFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if( ( strlen(m_Adu_41Msg[nAddr-1]) == 0 &&
- (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
- strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
- strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
- strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
- strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
- strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
- strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
- strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
- strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
- strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
- strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
- strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
- strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
- strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
- strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
- strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
- strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ||
- strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 ||
- strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
- strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 ||
- strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 ||
- strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
- strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 ||
- strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 ||
- strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 ||
- strcmp(szCmd, "cmd-58") == 0 || strcmp(szCmd, "cmd-59") == 0 ||
- strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0) ) ||//获取监测模块
- strcmp(szCmd, "cmd-3") == 0 ||
- strcmp(szCmd, "cmd-59") == 0 ||
- strcmp(szCmd, "cmd-61") == 0 )
- {
- nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
- 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_Q41VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- //LOG4C((LOG_NOTICE, "szCmd = %s, szMsg = %s ,nIndex = %d ,return nRet = %d", szCmd, szMsg,nIndex, nRet));
- return nRet;
- }
- int CBatteryAdu::GetAdu_Q41VarMsg( int nAddr, //设备地址
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH], // 变量数据类型
- int startBit,
- int endBit )
- {
- int nRet = 0;
- if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
- strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
- strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
- strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
- strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
- strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
- strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
- strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
- strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
- strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
- strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
- strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
- strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
- strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
- strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
- strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
- strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ||
- strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 ||
- strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
- strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 ||
- strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 ||
- strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
- strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 ||
- strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 ||
- strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 ||
- strcmp(szCmd, "cmd-58") == 0 || strcmp(szCmd, "cmd-59") == 0 ||
- strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_Adu_41Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
- //LOG4C((LOG_NOTICE," m_Adu_41Msg = %s ,szMsg = %s",m_Adu_41Msg[nAddr - 1], szMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CBatteryAdu::GetDeviceParam(int nAddr,
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[ADU_SEND_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- int nRet = -1;
- nRet = RequestStatus(nAddr, szCmd, pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- int CBatteryAdu::GetAduFromIni(char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char szSanTakSendMsg[MAX_CMD], //发送Buffer
- char nType[MAX_CMD],
- int &nIndex,
- int &nLen,
- int &startBit,
- int &endBit)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- TRACE("szFile =%s\n",szFile);
- GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg, 10, szFile);
- szSanTakSendMsg[strlen(szSanTakSendMsg)] = '\0';
- GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
- nType[strlen(nType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- TRACE("GetUhkFromIniszPath=%s\nszIniName=%s\nszCmd=%s\nzSanTakSendMsg=%s\nsType=%s\nnIndex=%d\nnLen = %d\n",
- szPath,szIniName,szCmd,szSanTakSendMsg,nType,nIndex,nLen);
- return 0;
- }
- int CBatteryAdu::RequestStatus( int nAddr,
- char szCmd[MAX_CMD], // 命令
- CCommProcess *pComm, //串口对象指针
- char chSendMsg[ADU_SEND_MSG] //发送Buffer
- )
- {
- 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;
- //Cid2
- memcpy(RequestPara.headMes.Cid2, chSendMsg, 2);
- RequestPara.headMes.Lenth[1] = 0x30;
- RequestPara.headMes.Lenth[2] = 0x30;
- RequestPara.headMes.Lenth[3] = 0x32;
- RequestPara.headMes.Lenth[0] = ByteToAscii(GetLengthSum(RequestPara.headMes.Lenth));
- RequestPara.headMes.GroupInfo[0] = 0x30;
- RequestPara.headMes.GroupInfo[1] = 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 );
- 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
- {
- delete []pDataBuf;
- SetEvent( m_hSemComm );
- return EER_CODE_ADU_COM_WRITE_DATA;
- }
- }
- else
- {
- //LOG4C((LOG_NOTICE, "m_hSemComm no single"));
- delete []pDataBuf;
- return ERR_CODE_ADU_COM_BUSY;
- }
- delete[] pDataBuf;
- #endif
- return 0;
- }
- int CBatteryAdu::ResponseStatus(
- int nAddr, // 设备地址
- CCommProcess *pComm, //串口对象指针
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- #if DEBUG_ADU
- 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 = pComm->Read((BYTE *)pBuffer,nReceiveLen);
- ///LOG4C((LOG_NOTICE," nReadLen = %d ,pBuffer = %s",nReadLen,pBuffer));
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- //TRACE("串口没有读到数据!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_ADU_COM_READ_NO_DATA;
- }
- if (LengthCheck(pBuffer) != 0)
- {
- SetEvent(m_hSemComm);
- TRACE("长度校验出错");
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- if (!ChkSumCheck(pBuffer, nReadLen))
- {
- TRACE("校验码校验出错");
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
- }
- if (RtnCheck(pBuffer) != 0)
- {
- TRACE("RTN校验出错");
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
- }
- SetAduQ41VarMsg(nAddr, szCmd, pBuffer);
-
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- // 设置串口等待事件为有信号
- SetEvent(m_hSemComm);
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- void CBatteryAdu::SetAduQ41VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
- strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
- strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
- strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
- strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
- strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
- strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
- strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
- strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
- strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
- strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
- strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
- strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
- strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
- strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
- strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
- strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ||
- strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 ||
- strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 ||
- strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 ||
- strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 ||
- strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 ||
- strcmp(szCmd, "cmd-52") == 0 || strcmp(szCmd, "cmd-53") == 0 ||
- strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 ||
- strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 ||
- strcmp(szCmd, "cmd-58") == 0 || strcmp(szCmd, "cmd-59") == 0 ||
- strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_Adu_41Msg[nAddr - 1], pBuffer, sizeof(m_Adu_41Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CBatteryAdu::SimulationCommData(int nAddr)
- {
- /*获取模拟量量化数据浮点数*/
- // 7E 32 31 30 30 32 41 30 30 37
- // 30 39 30 30 30 36 36 36 36 36
- // 44 34 33 36 36 36 36 36 44 34
- // 33 36 36 36 36 37 30 34 33 36
- // 36 36 36 35 43 34 33 20 20 20
- // 20 20 20 20 20 20 20 20 20 20
- // 20 20 20 43 44 43 43 34 43 34 ``
- // 30 20 20 20 20 20 20 20 20 20
- // 20 20 20 20 20 20 20 39 41 31
- // 39 38 36 34 33 37 42 31 34 34
- // 38 34 32 30 30 30 30 30 36 37
- // 42 31 34 34 38 34 32 30 30 30
- // 30 30 30 34 31 30 30 30 30 43
- // 38 34 32 34 38 45 31 44 41 34
- // 31 36 36 45 36 36 41 34 33 30
- // 30 38 30 38 36 34 33 45 31 46
- // 30 0D
- m_Adu_41Msg[nAddr - 1][0] = 0x7E;
- m_Adu_41Msg[nAddr - 1][1] = 0x32;
- m_Adu_41Msg[nAddr - 1][2] = 0x30;
- m_Adu_41Msg[nAddr - 1][3] = 0x30;
- m_Adu_41Msg[nAddr - 1][4] = 0x31;
- m_Adu_41Msg[nAddr - 1][5] = 0x34;
- m_Adu_41Msg[nAddr - 1][6] = 0x30;
- m_Adu_41Msg[nAddr - 1][7] = 0x30;
- m_Adu_41Msg[nAddr - 1][8] = 0x30;
- m_Adu_41Msg[nAddr - 1][9] = 0x38;
- m_Adu_41Msg[nAddr - 1][10] = 0x30;
- m_Adu_41Msg[nAddr - 1][11] = 0x36;
- m_Adu_41Msg[nAddr - 1][12] = 0x32;
- m_Adu_41Msg[nAddr - 1][13] = 0x30;
- m_Adu_41Msg[nAddr - 1][14] = 0x30;
- m_Adu_41Msg[nAddr - 1][15] = 0x30;
- m_Adu_41Msg[nAddr - 1][16] = 0x31;
- m_Adu_41Msg[nAddr - 1][17] = 0x30;
- m_Adu_41Msg[nAddr - 1][18] = 0x32;
- m_Adu_41Msg[nAddr - 1][19] = 0x43;
- m_Adu_41Msg[nAddr - 1][20] = 0x34;
- m_Adu_41Msg[nAddr - 1][21] = 0x43;
- m_Adu_41Msg[nAddr - 1][22] = 0x45;
- m_Adu_41Msg[nAddr - 1][23] = 0x37;
- m_Adu_41Msg[nAddr - 1][24] = 0x31;
- m_Adu_41Msg[nAddr - 1][25] = 0x34;
- m_Adu_41Msg[nAddr - 1][26] = 0x33;
- m_Adu_41Msg[nAddr - 1][27] = 0x42;
- m_Adu_41Msg[nAddr - 1][28] = 0x46;
- m_Adu_41Msg[nAddr - 1][29] = 0x37;
- m_Adu_41Msg[nAddr - 1][30] = 0x36;
- m_Adu_41Msg[nAddr - 1][31] = 0x37;
- m_Adu_41Msg[nAddr - 1][32] = 0x32;
- m_Adu_41Msg[nAddr - 1][33] = 0x34;
- m_Adu_41Msg[nAddr - 1][34] = 0x33;
- m_Adu_41Msg[nAddr - 1][35] = 0x43;
- m_Adu_41Msg[nAddr - 1][36] = 0x38;
- m_Adu_41Msg[nAddr - 1][37] = 0x32;
- m_Adu_41Msg[nAddr - 1][38] = 0x36;
- m_Adu_41Msg[nAddr - 1][39] = 0x37;
- m_Adu_41Msg[nAddr - 1][40] = 0x31;
- m_Adu_41Msg[nAddr - 1][41] = 0x34;
- m_Adu_41Msg[nAddr - 1][42] = 0x33;
- m_Adu_41Msg[nAddr - 1][43] = 0x20;
- m_Adu_41Msg[nAddr - 1][44] = 0x20;
- m_Adu_41Msg[nAddr - 1][45] = 0x20;
- m_Adu_41Msg[nAddr - 1][46] = 0x20;
- m_Adu_41Msg[nAddr - 1][47] = 0x20;
- m_Adu_41Msg[nAddr - 1][48] = 0x20;
- m_Adu_41Msg[nAddr - 1][49] = 0x20;
- m_Adu_41Msg[nAddr - 1][50] = 0x20;
- m_Adu_41Msg[nAddr - 1][51] = 0x30;
- m_Adu_41Msg[nAddr - 1][52] = 0x30;
- m_Adu_41Msg[nAddr - 1][53] = 0x43;
- m_Adu_41Msg[nAddr - 1][54] = 0x31;
- m_Adu_41Msg[nAddr - 1][55] = 0x41;
- m_Adu_41Msg[nAddr - 1][56] = 0x30;
- m_Adu_41Msg[nAddr - 1][57] = 0x37;
- m_Adu_41Msg[nAddr - 1][58] = 0x32;
- m_Adu_41Msg[nAddr - 1][59] = 0x34;
- m_Adu_41Msg[nAddr - 1][60] = 0x33;
- m_Adu_41Msg[nAddr - 1][61] = 0x43;
- m_Adu_41Msg[nAddr - 1][62] = 0x34;
- m_Adu_41Msg[nAddr - 1][63] = 0x43;
- m_Adu_41Msg[nAddr - 1][64] = 0x45;
- m_Adu_41Msg[nAddr - 1][65] = 0x37;
- m_Adu_41Msg[nAddr - 1][66] = 0x31;
- m_Adu_41Msg[nAddr - 1][67] = 0x34;
- m_Adu_41Msg[nAddr - 1][68] = 0x33;
- m_Adu_41Msg[nAddr - 1][69] = 0x46;
- m_Adu_41Msg[nAddr - 1][70] = 0x37;
- m_Adu_41Msg[nAddr - 1][71] = 0x46;
- m_Adu_41Msg[nAddr - 1][72] = 0x41;
- m_Adu_41Msg[nAddr - 1][73] = 0x36;
- m_Adu_41Msg[nAddr - 1][74] = 0x46;
- m_Adu_41Msg[nAddr - 1][75] = 0x34;
- m_Adu_41Msg[nAddr - 1][76] = 0x33;
-
- }
- WORD CBatteryAdu::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_LIMIT;
- }
- else if (strcmp(buffer,"E2") == 0)
- {
- return ERR_CODE_RTN_OTHER_ERROR;
- }
-
- return 0;
- }
- UINT CBatteryAdu::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 CBatteryAdu::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 CBatteryAdu::LengthCheck(char szSendMsg[])
- {
- 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)) // 相加模十六取反加一
- {
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- /*转换为十六进制*/
- return 0;
- }
- BOOL CBatteryAdu::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;
- }
|