| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235 |
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: UpsUH11.cpp //////
- ////// 作 者: Suguobing //////
- ////// 创建时间: //////
- ////// 说 明: UH11协议 //////
- ////// //////
- ////// 修改时间:2010-08-05 //////
- ////// 修改说明:数据转换 //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommProcess.h"
- #include "UpsUH11.h"
- CUpsUh11::CUpsUh11( 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));
- memset(m_szUh_Q41Msg, 0, sizeof(m_szUh_Q41Msg));
- memset(m_szUh_Q43Msg, 0, sizeof(m_szUh_Q43Msg));
- memset(m_szUh_Q44Msg, 0, sizeof(m_szUh_Q44Msg));
- memset(m_szUh_Q45Msg, 0, sizeof(m_szUh_Q45Msg));
- memset(m_szUh_Q4FMsg, 0, sizeof(m_szUh_Q4FMsg));
- memset(m_szUh_Q50Msg, 0, sizeof(m_szUh_Q50Msg));
- m_dwOnlineTick = 0;
- m_devOnline = TRUE;
- }
- CUpsUh11::~CUpsUh11()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL CUpsUh11::UpsUhOpenComm(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 CUpsUh11::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]
- )
- {
- //LOG4C((LOG_NOTICE,"CUpsUh11::SendReadRequest"));
- int nIndex = 0, nLen = 0;
- char szSendMsg[UPS_UH_SEND_MSG] = {0};
- char szType[UPS_TYPE_LENGTH] = {0};
- int startBit = 0;
- int endBit = 0;
- m_address = nAddr;
- GetUhFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if( ( strlen(m_szUh_Q41Msg) == 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 ) ) || //获取系统模拟量量化数据(浮点数)
- ( strlen(m_szUh_Q43Msg) == 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 ) )||//UPS工作状态及传送顺序
- ( strlen(m_szUh_Q44Msg) == 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 ) ) ||//UPS告警内容及传送
- ( strlen(m_szUh_Q45Msg) == 0 &&
- (strcmp(szCmd, "cmd-39") == 0 ) ) ||//获取系统参数(浮点数)
- ( strlen(m_szUh_Q4FMsg) == 0 &&
- (strcmp(szCmd, "cmd-40") == 0 ) ) ||//获取通信协议版本号
- ( strlen(m_szUh_Q50Msg) == 0 &&
- (strcmp(szCmd, "cmd-41") == 0 ) ) || //获取设备地址
- strcmp(szCmd, "cmd-1") == 0 ||
- strcmp(szCmd, "cmd-19") == 0 ||
- strcmp(szCmd, "cmd-25") == 0 ||
- strcmp(szCmd, "cmd-39") == 0 ||
- strcmp(szCmd, "cmd-40") == 0 ||
- strcmp(szCmd, "cmd-41") == 0 )
- {
- nRet = GetDeviceParam( pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
- if( nRet != 0 ) return nRet;
- }
- if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
- {
- m_devOnline = FALSE;
- //LOG4C((LOG_NOTICE,"串口通信FALSE"));
- }
- else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
- {
- m_devOnline = TRUE;
- //LOG4C((LOG_NOTICE,"串口通信TRUE"));
- }
- if( m_devOnline == FALSE )
- {
- return -1;
- }
- nRet = GetUhQ41VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetUhQ43VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetUhQ44VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetUhQ45VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetUhQ4FVarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetUhQ50VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- //LOG4C((LOG_NOTICE, "%s = %s, return %d", szCmd, szMsg, nRet));
- //if (strcmp(szCmd , "cmd-24") == 0)
- //{
- //LOG4C((LOG_NOTICE, "%s = %s, return %d", szCmd, szMsg, nRet));
- //}
- //LOG4C_IF((strcmp(szCmd , "cmd-24") == 0, LOG_NOTICE, "nCommPort = %d,nAddr = %d, %s = %s, return %d",nCommPort, nAddr, szCmd, szMsg, nRet));
- //LOG4C_IF((strcmp(szCmd , "cmd-19") == 0, LOG_NOTICE, "nCommPort = %d,nAddr = %d, %s = %s, return %d",nCommPort, nAddr, szCmd, szMsg, nRet));
- return nRet;
- }
- int CUpsUh11::GetUhQ41VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, 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 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szUh_Q41Msg + nIndex, szMsg, nLen, startBit, endBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CUpsUh11::GetUhQ43VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
- {
- int nRet = 0;
- if (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 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szUh_Q43Msg + nIndex, szMsg, nLen, startBit, endBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CUpsUh11::GetUhQ44VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
- {
- int nRet = 0;
- if (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 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szUh_Q44Msg + nIndex, szMsg, nLen, startBit, endBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CUpsUh11::GetUhQ45VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
- {
- int nRet = 0;
- if (strcmp(szCmd, "cmd-39") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szUh_Q45Msg + nIndex, szMsg, nLen, startBit, endBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CUpsUh11::GetUhQ4FVarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
- {
- int nRet = 0;
- if (strcmp(szCmd, "cmd-40") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szUh_Q4FMsg + nIndex, szMsg, nLen, startBit, endBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CUpsUh11::GetUhQ50VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
- {
- int nRet = 0;
- if (strcmp(szCmd, "cmd-41") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- DataConversion(szType, m_szUh_Q50Msg + nIndex, szMsg, nLen, startBit, endBit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CUpsUh11::GetDeviceParam(
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[UPS_UH_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(pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseStatus(pComm, szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- int CUpsUh11::GetUhFromIni(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);
- return 0;
- }
- int CUpsUh11::RequestStatus(CCommProcess *pComm, char chSendMsg[UPS_UH_SEND_MSG] )
- {
- #if DEBUG_UH //开关量,用于测试模拟数据
- REQUEST_STRUCT structResuest;
- int nLen = sizeof(REQUEST_STRUCT);
- char chkSumBuf[5] ={0};
- char addBuf[UPS_TYPE_LENGTH] = {0};
- memset(&structResuest,0,nLen);
- //
- structResuest.headMes.Start = 0x7E;
- //
- structResuest.headMes.Version[0] = 0x32;
- structResuest.headMes.Version[1] = 0x31;
- //
- sprintf(addBuf, "%x",m_address);
- structResuest.headMes.Address[0] = ByteToAscii((m_address >> 4) & 0x0f);
- structResuest.headMes.Address[1] = ByteToAscii(m_address & 0x0f);
- //
- structResuest.headMes.Cid1[0] = 0x32;
- structResuest.headMes.Cid1[1] = 0x41;
- structResuest.headMes.Cid2[0] = ByteToAscii(chSendMsg[0] & 0x0f);
- structResuest.headMes.Cid2[1] = ByteToAscii(chSendMsg[1] & 0x0f);
- structResuest.headMes.Lenth[0] = 0x30;
- structResuest.headMes.Lenth[1] = 0x30;
- structResuest.headMes.Lenth[2] = 0x30;
- structResuest.headMes.Lenth[3] = 0x30;
- BYTE *pDatabuf = new BYTE[nLen - sizeof(structResuest.chkSum) - 1];
- memset(pDatabuf,0,nLen - sizeof(structResuest.chkSum) - 1);
- memcpy(pDatabuf,&structResuest,nLen - sizeof(structResuest.chkSum) - 1);
- GetCheckSum((char *)pDatabuf + 1, chkSumBuf, nLen - sizeof(structResuest.chkSum) - 2);
- structResuest.chkSum[0] = chkSumBuf[0];
- structResuest.chkSum[1] = chkSumBuf[1];
- structResuest.chkSum[2] = chkSumBuf[2];
- structResuest.chkSum[3] = chkSumBuf[3];
- structResuest.End = 0x0D;
- TRACE("headMes.Start= %c\nversion=%s\ncid1=%s\ncid2=%s\nlength=%s\nchksum=%s\n\n",
- structResuest.headMes.Start,structResuest.headMes.Version,
- structResuest.headMes.Address,structResuest.headMes.Cid1,
- structResuest.headMes.Cid2,structResuest.headMes.Lenth,structResuest.chkSum,structResuest.End);
- if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
- {
- int nDatalen = (int)sizeof(structResuest);
- ResetEvent(m_hSemComm);
- int nResult = pComm->Write((BYTE *)&structResuest, nDatalen);
- //LOG4C_HEX_DUMP((LOG_NOTICE, (char *)&structResuest, nDatalen));
- if (nResult == nDatalen)
- {
- }
- else
- {
- delete [] pDatabuf;
- SetEvent(m_hSemComm);
- return EER_CODE_UPSUH_COM_REGNUM;
- }
- }
- else
- {
- delete [] pDatabuf;
- return ERR_CODE_UPSUH_COM_BUSY; //串口忙
- }
- delete [] pDatabuf;
- #endif
- return 0;
- }
- int CUpsUh11::ResponseStatus(
- CCommProcess *pComm,
- char szCmd[MAX_CMD],
- char szMsg[VAR_MSG],
- int &nIndex,
- int &nLen,
- char szType[TYPE_LENGTH]
- )
- {
- #if DEBUG_UH
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- RESPONSE_STRUCT strcutResponse;
- memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
- nReceiveLen = sizeof(RESPONSE_STRUCT);
- char *pBuff = new char[nReceiveLen];
- memset(pBuff, 0 , nLen);
- nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
- if (nReadLen <= 0)
- {
- //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus nReadLen < = 0"));
- SetEvent(m_hSemComm);
- if (pBuff != NULL)
- {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_UPSUH_COM_READ_NO_DATA;
- }
- //长度校验出错
- if (LengthCheck(pBuff) != 0)
- {
- SetEvent(m_hSemComm);
- //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus LengthCheck ERROR "));
- if (pBuff != NULL)
- {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- if (!ChkSumCheck(pBuff, nReadLen))
- {
- //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus ChkSumCheck ERROR "));
- SetEvent(m_hSemComm);
- if (pBuff != NULL)
- {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
- }
- if (CheckRtn(pBuff, nReadLen) != 0)
- {
- //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus CheckRtn ERROR "));
- SetEvent(m_hSemComm);
- if (pBuff != NULL)
- {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
- }
- SetUhQ41VarMsg(szCmd, pBuff);
- SetUhQ43VarMsg(szCmd, pBuff);
- SetUhQ44VarMsg(szCmd, pBuff);
- SetUhQ45VarMsg(szCmd, pBuff);
- SetUhQ4FVarMsg(szCmd, pBuff);
- SetUhQ50VarMsg(szCmd, pBuff);
- m_dwOnlineTick = GetTickCount();
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuff != NULL)
- {
- delete[] pBuff;
- pBuff = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void CUpsUh11::SetUhQ41VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- 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 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szUh_Q41Msg, pBuff, sizeof(m_szUh_Q41Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CUpsUh11::SetUhQ43VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if ( 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
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szUh_Q43Msg,pBuff,sizeof(m_szUh_Q43Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CUpsUh11::SetUhQ44VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if ( 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 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szUh_Q44Msg,pBuff,sizeof(m_szUh_Q44Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CUpsUh11::SetUhQ45VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if ( strcmp(szCmd, "cmd-39") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szUh_Q45Msg,pBuff,sizeof(m_szUh_Q45Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CUpsUh11::SetUhQ4FVarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if (strcmp(szCmd, "cmd-40") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szUh_Q4FMsg,pBuff,sizeof(m_szUh_Q4FMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CUpsUh11::SetUhQ50VarMsg(char szCmd[MAX_CMD], char *pBuff)
- {
- if (strcmp(szCmd, "cmd-41") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szUh_Q50Msg,pBuff,sizeof(m_szUh_Q50Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CUpsUh11::SimulationCommData(void)
- {
- /*获取模拟量量化数据浮点数*/
- // 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_szUh_Q41Msg[0] = 0x7E;
- m_szUh_Q41Msg[1] = 0x32;
- m_szUh_Q41Msg[2] = 0x31;
- m_szUh_Q41Msg[3] = 0x30;
- m_szUh_Q41Msg[4] = 0x30;
- m_szUh_Q41Msg[5] = 0x32;
- m_szUh_Q41Msg[6] = 0x41;
- m_szUh_Q41Msg[7] = 0x30;
- m_szUh_Q41Msg[8] = 0x30;
- m_szUh_Q41Msg[9] = 0x37;
- m_szUh_Q41Msg[10] = 0x30;
- m_szUh_Q41Msg[11] = 0x39;
- m_szUh_Q41Msg[12] = 0x30;
- m_szUh_Q41Msg[13] = 0x30;
- m_szUh_Q41Msg[14] = 0x30;
- m_szUh_Q41Msg[15] = 0x36;
- m_szUh_Q41Msg[16] = 0x36;
- m_szUh_Q41Msg[17] = 0x36;
- m_szUh_Q41Msg[18] = 0x36;
- m_szUh_Q41Msg[19] = 0x36;
- //44 34 33 36 36 36 36 36 44 34
- m_szUh_Q41Msg[20] = 0x44;
- m_szUh_Q41Msg[21] = 0x34;
- m_szUh_Q41Msg[22] = 0x33;
- m_szUh_Q41Msg[23] = 0x36;
- m_szUh_Q41Msg[24] = 0x36;
- m_szUh_Q41Msg[25] = 0x36;
- m_szUh_Q41Msg[26] = 0x36;
- m_szUh_Q41Msg[27] = 0x36;
- m_szUh_Q41Msg[28] = 0x44;
- m_szUh_Q41Msg[29] = 0x34;
- //33 36 36 36 36 37 30 34 33 36
- m_szUh_Q41Msg[30] = 0x33;
- m_szUh_Q41Msg[31] = 0x36;
- m_szUh_Q41Msg[32] = 0x36;
- m_szUh_Q41Msg[33] = 0x36;
- m_szUh_Q41Msg[34] = 0x36;
- m_szUh_Q41Msg[35] = 0x37;
- m_szUh_Q41Msg[36] = 0x30;
- m_szUh_Q41Msg[37] = 0x34;
- m_szUh_Q41Msg[38] = 0x33;
- m_szUh_Q41Msg[39] = 0x36;
- //36 36 36 35 43 34 33 20 20 20
- m_szUh_Q41Msg[40] = 0x36;
- m_szUh_Q41Msg[41] = 0x36;
- m_szUh_Q41Msg[42] = 0x36;
- m_szUh_Q41Msg[43] = 0x35;
- m_szUh_Q41Msg[44] = 0x43;
- m_szUh_Q41Msg[45] = 0x34;
- m_szUh_Q41Msg[46] = 0x33;
- m_szUh_Q41Msg[47] = 0x20;
- m_szUh_Q41Msg[48] = 0x20;
- m_szUh_Q41Msg[49] = 0x20;
- //20 20 20 20 20 20 20 20 20 20
- m_szUh_Q41Msg[50] = 0x20;
- m_szUh_Q41Msg[51] = 0x20;
- m_szUh_Q41Msg[52] = 0x20;
- m_szUh_Q41Msg[53] = 0x20;
- m_szUh_Q41Msg[54] = 0x20;
- m_szUh_Q41Msg[55] = 0x20;
- m_szUh_Q41Msg[56] = 0x20;
- m_szUh_Q41Msg[57] = 0x20;
- m_szUh_Q41Msg[58] = 0x20;
- m_szUh_Q41Msg[59] = 0x20;
- //20 20 20 30 30 30 30 36 30 34
- m_szUh_Q41Msg[60] = 0x20;
- m_szUh_Q41Msg[61] = 0x20;
- m_szUh_Q41Msg[62] = 0x20;
- m_szUh_Q41Msg[63] = 0x30;
- m_szUh_Q41Msg[64] = 0x30;
- m_szUh_Q41Msg[65] = 0x30;
- m_szUh_Q41Msg[66] = 0x30;
- m_szUh_Q41Msg[67] = 0x36;
- m_szUh_Q41Msg[68] = 0x30;
- m_szUh_Q41Msg[69] = 0x34;
- // 30 20 20 20 20 20 20 20 20 20
- m_szUh_Q41Msg[70] = 0x30;
- m_szUh_Q41Msg[71] = 0x20;
- m_szUh_Q41Msg[72] = 0x20;
- m_szUh_Q41Msg[73] = 0x20;
- m_szUh_Q41Msg[74] = 0x20;
- m_szUh_Q41Msg[75] = 0x20;
- m_szUh_Q41Msg[76] = 0x20;
- m_szUh_Q41Msg[77] = 0x20;
- m_szUh_Q41Msg[78] = 0x20;
- m_szUh_Q41Msg[79] = 0x20;
- //20 20 20 20 20 20 20 39 41 31
- m_szUh_Q41Msg[80] = 0x20;
- m_szUh_Q41Msg[81] = 0x20;
- m_szUh_Q41Msg[82] = 0x20;
- m_szUh_Q41Msg[83] = 0x20;
- m_szUh_Q41Msg[84] = 0x20;
- m_szUh_Q41Msg[85] = 0x20;
- m_szUh_Q41Msg[86] = 0x20;
- m_szUh_Q41Msg[87] = 0x39;
- m_szUh_Q41Msg[88] = 0x41;
- m_szUh_Q41Msg[89] = 0x31;
- //39 38 36 34 33 33 44 31 34 34
- m_szUh_Q41Msg[90] = 0x39;
- m_szUh_Q41Msg[91] = 0x38;
- m_szUh_Q41Msg[92] = 0x36;
- m_szUh_Q41Msg[93] = 0x34;
- m_szUh_Q41Msg[94] = 0x33;
- m_szUh_Q41Msg[95] = 0x33;
- m_szUh_Q41Msg[96] = 0x44;
- m_szUh_Q41Msg[97] = 0x30;
- m_szUh_Q41Msg[98] = 0x41;
- m_szUh_Q41Msg[99] = 0x34;
- //38 34 32 30 30 30 30 30 36 37
- m_szUh_Q41Msg[100] = 0x38;
- m_szUh_Q41Msg[101] = 0x34;
- m_szUh_Q41Msg[102] = 0x32;
- m_szUh_Q41Msg[103] = 0x30;
- m_szUh_Q41Msg[104] = 0x30;
- m_szUh_Q41Msg[105] = 0x30;
- m_szUh_Q41Msg[106] = 0x30;
- m_szUh_Q41Msg[107] = 0x30;
- m_szUh_Q41Msg[108] = 0x36;
- m_szUh_Q41Msg[109] = 0x37;
- // 42 31 34 34 38 34 32 30 30 30
- m_szUh_Q41Msg[110] = 0x42;
- m_szUh_Q41Msg[111] = 0x31;
- m_szUh_Q41Msg[112] = 0x34;
- m_szUh_Q41Msg[113] = 0x34;
- m_szUh_Q41Msg[114] = 0x38;
- m_szUh_Q41Msg[115] = 0x34;
- m_szUh_Q41Msg[116] = 0x32;
- m_szUh_Q41Msg[117] = 0x30;
- m_szUh_Q41Msg[118] = 0x30;
- m_szUh_Q41Msg[119] = 0x30;
- //30 30 30 34 31 30 30 30 30 43
- m_szUh_Q41Msg[120] = 0X30;
- m_szUh_Q41Msg[121] = 0x30;
- m_szUh_Q41Msg[122] = 0x30;
- m_szUh_Q41Msg[123] = 0x34;
- m_szUh_Q41Msg[124] = 0x31;
- m_szUh_Q41Msg[125] = 0x30;
- m_szUh_Q41Msg[126] = 0x30;
- m_szUh_Q41Msg[127] = 0x30;
- m_szUh_Q41Msg[128] = 0x30;
- m_szUh_Q41Msg[129] = 0x43;
- //38 34 32 34 38 45 31 44 41 34
- m_szUh_Q41Msg[130] = 0x38;
- m_szUh_Q41Msg[131] = 0x34;
- m_szUh_Q41Msg[132] = 0x32;
- m_szUh_Q41Msg[133] = 0x34;
- m_szUh_Q41Msg[134] = 0x38;
- m_szUh_Q41Msg[135] = 0x45;
- m_szUh_Q41Msg[136] = 0x31;
- m_szUh_Q41Msg[137] = 0x44;
- m_szUh_Q41Msg[138] = 0x41;
- m_szUh_Q41Msg[139] = 0x34;
- //31 36 36 45 36 36 41 34 33 30
- m_szUh_Q41Msg[140] = 0x31;
- m_szUh_Q41Msg[141] = 0x39;
- m_szUh_Q41Msg[142] = 0x41;
- m_szUh_Q41Msg[143] = 0x39;
- m_szUh_Q41Msg[144] = 0x39;
- m_szUh_Q41Msg[145] = 0x36;
- m_szUh_Q41Msg[146] = 0x42;
- m_szUh_Q41Msg[147] = 0x34;
- m_szUh_Q41Msg[148] = 0x33;
- m_szUh_Q41Msg[149] = 0x36;
- //30 38 30 38 36 34 33 45 31 46
- m_szUh_Q41Msg[150] = 0x36;
- m_szUh_Q41Msg[151] = 0x36;
- m_szUh_Q41Msg[152] = 0x36;
- m_szUh_Q41Msg[153] = 0x38;
- m_szUh_Q41Msg[154] = 0x36;
- m_szUh_Q41Msg[155] = 0x34;
- m_szUh_Q41Msg[156] = 0x33;
- m_szUh_Q41Msg[157] = 0x45;
- m_szUh_Q41Msg[158] = 0x31;
- m_szUh_Q41Msg[159] = 0x46;
- //30 0D
- m_szUh_Q41Msg[160] = 0x30;
- m_szUh_Q41Msg[161] = 0x0D;
- //7E 32 31 30 30 32 41 30 30 46
- //30 31 30 30 30 30 31 30 35 45
- //31 45 32 45 32 45 34 45 38 46
- //41 31 33 0D
- /*获取开关量输入状态*/
- // 7E 32 31 30 30 32 41 30 30 46
- m_szUh_Q43Msg[0] = 0x7E;
- m_szUh_Q43Msg[1] = 0x32;
- m_szUh_Q43Msg[2] = 0x31;
- m_szUh_Q43Msg[3] = 0x30;
- m_szUh_Q43Msg[4] = 0x30;
- m_szUh_Q43Msg[5] = 0x32;
- m_szUh_Q43Msg[6] = 0x41;
- m_szUh_Q43Msg[7] = 0x30;
- m_szUh_Q43Msg[8] = 0x30;
- m_szUh_Q43Msg[9] = 0x46;
- // 30 31 30 30 30 30 31 30 35 45
- m_szUh_Q43Msg[10] = 0x30;
- m_szUh_Q43Msg[11] = 0x31;
- m_szUh_Q43Msg[12] = 0x30;
- m_szUh_Q43Msg[13] = 0x30;
- m_szUh_Q43Msg[14] = 0x30;
- m_szUh_Q43Msg[15] = 0x30;
- m_szUh_Q43Msg[16] = 0x31;
- m_szUh_Q43Msg[17] = 0x30;
- m_szUh_Q43Msg[18] = 0x35;
- m_szUh_Q43Msg[19] = 0x45;
- // 31 45 32 45 32 45 34 45 38 46
- m_szUh_Q43Msg[20] = 0x31;
- m_szUh_Q43Msg[21] = 0x45;
- m_szUh_Q43Msg[22] = 0x32;
- m_szUh_Q43Msg[23] = 0x45;
- m_szUh_Q43Msg[24] = 0x32;
- m_szUh_Q43Msg[25] = 0x45;
- m_szUh_Q43Msg[26] = 0x34;
- m_szUh_Q43Msg[27] = 0x45;
- m_szUh_Q43Msg[28] = 0x38;
- m_szUh_Q43Msg[29] = 0x46;
- // 41 31 33 0D
- m_szUh_Q43Msg[30] = 0x41;
- m_szUh_Q43Msg[31] = 0x31;
- m_szUh_Q43Msg[32] = 0x33;
- m_szUh_Q43Msg[33] = 0x0D;
- /*获取告警状态*/
- // 7E 32 31 30 30 32 41 30 30 38
- // 30 32 36 30 30 30 30 30 30 30
- // 30 30 30 30 30 30 30 30 30 30
- // 41 30 30 30 30 30 30 30 30 30
- // 30 30 30 30 30 30 30 30 30 30
- // 30 46 36 36 39 0D
- m_szUh_Q44Msg[0] = 0x7E;
- m_szUh_Q44Msg[1] = 0x32;
- m_szUh_Q44Msg[2] = 0x31;
- m_szUh_Q44Msg[3] = 0x30;
- m_szUh_Q44Msg[4] = 0x30;
- m_szUh_Q44Msg[5] = 0x32;
- m_szUh_Q44Msg[6] = 0x41;
- m_szUh_Q44Msg[7] = 0x30;
- m_szUh_Q44Msg[8] = 0x30;
- m_szUh_Q44Msg[9] = 0x38;
- // 30 32 36 30 30 30 30 30 30 30
- m_szUh_Q44Msg[10] = 0x30;
- m_szUh_Q44Msg[11] = 0x32;
- m_szUh_Q44Msg[12] = 0x36;
- m_szUh_Q44Msg[13] = 0x30;
- m_szUh_Q44Msg[14] = 0x30;
- m_szUh_Q44Msg[15] = 0x30;
- m_szUh_Q44Msg[16] = 0x30;
- m_szUh_Q44Msg[17] = 0x30;
- m_szUh_Q44Msg[18] = 0x30;
- m_szUh_Q44Msg[19] = 0x30;
- // 30 30 30 30 30 30 30 30 30 30
- m_szUh_Q44Msg[20] = 0x30;
- m_szUh_Q44Msg[21] = 0x30;
- m_szUh_Q44Msg[22] = 0x30;
- m_szUh_Q44Msg[23] = 0x30;
- m_szUh_Q44Msg[24] = 0x30;
- m_szUh_Q44Msg[25] = 0x30;
- m_szUh_Q44Msg[26] = 0x30;
- m_szUh_Q44Msg[27] = 0x30;
- m_szUh_Q44Msg[28] = 0x30;
- m_szUh_Q44Msg[29] = 0x30;
- // 41 30 30 30 30 30 30 30 30 30
- m_szUh_Q44Msg[30] = 0x41;
- m_szUh_Q44Msg[31] = 0x30;
- m_szUh_Q44Msg[32] = 0x30;
- m_szUh_Q44Msg[33] = 0x30;
- m_szUh_Q44Msg[34] = 0x30;
- m_szUh_Q44Msg[35] = 0x30;
- m_szUh_Q44Msg[36] = 0x30;
- m_szUh_Q44Msg[37] = 0x30;
- m_szUh_Q44Msg[38] = 0x30;
- m_szUh_Q44Msg[39] = 0x30;
- // 30 30 30 30 30 30 30 30 30 30
- m_szUh_Q44Msg[40] = 0x30;
- m_szUh_Q44Msg[41] = 0x30;
- m_szUh_Q44Msg[42] = 0x30;
- m_szUh_Q44Msg[43] = 0x30;
- m_szUh_Q44Msg[44] = 0x30;
- m_szUh_Q44Msg[45] = 0x30;
- m_szUh_Q44Msg[46] = 0x30;
- m_szUh_Q44Msg[47] = 0x30;
- m_szUh_Q44Msg[48] = 0x30;
- m_szUh_Q44Msg[49] = 0x30;
- // 30 46 36 36 39 0D
- m_szUh_Q44Msg[50] = 0x30;
- m_szUh_Q44Msg[51] = 0x46;
- m_szUh_Q44Msg[52] = 0x36;
- m_szUh_Q44Msg[53] = 0x36;
- m_szUh_Q44Msg[54] = 0x39;
- m_szUh_Q44Msg[55] = 0x0D;
- }
- int CUpsUh11::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 CUpsUh11::CheckCHKSUM(char szSendMsg[])
- {
- int j = 0;
- char buf1[5] = {0};
- char buf2[5] = {0};
- char szbuf[5] = {0};
- char chDest[5] = {0};
- unsigned short nData = 0;
- int len = (int)strlen(szSendMsg);
- char *buffer = new char [len-2];
- memset(buffer,0,sizeof(buffer));
- for (int i = 1; i < len-5; i++)
- {
- buffer[j++] = szSendMsg[i];
- }
- buffer[j] = '\0';
- for (j = 0; j < buffer[j] != '\0'; j ++)
- {
- nData += AsciiToBYTE(buffer[j]); //十进制相加
- }
- nData = ~nData +1; //取反加1 (有问题)
- nData %= 65536;
- //转换为16进制
- sprintf(buf2,"%x",nData);
- StrLower(buf2,chDest);
- j = 0;
- for (int i = len-5; i < len -1; i++)
- {
- buf1[j++] = szSendMsg[i];
- }
- buf1[j] = '\0';
- StrLower(buf1,szbuf);
- if (strcmp(szbuf,chDest) == 0)
- {
- delete [] buffer;
- return TRUE;
- }
- delete [] buffer;
- return FALSE;
- }
- BOOL CUpsUh11::CheckRtn(char szMsg[], int len)
- {
- //int len = (int)strlen(szMsg);
- char buffer[3] = {0};
- int j = 0;
- for (int i = 7; i < 9; i++)
- {
- buffer[j++] = szMsg[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,"E0") == 0)
- {
- return ERR_CODE_RTN_OPERATE_FAIL;
- }
- else if (strcmp(buffer,"E1") == 0)
- {
- return ERR_CODE_RTN_EQUIT_FAIL;
- }
- else if (strcmp(buffer,"E3")==0 ||
- strcmp(buffer,"E4")==0
- )
- {
- return ERR_CODE_RTN_KEEP;
- }
- else if (strcmp(buffer, "E5")==0 ||
- strcmp(buffer, "E6") == 0 ||
- strcmp(buffer, "E7") == 0 ||
- strcmp(buffer, "E8") == 0 ||
- strcmp(buffer, "E9") == 0
- )
- {
- return ERR_CODE_RTN_WATCH;
- }
- else if (strcmp(buffer,"EA") == 0 ||
- strcmp(buffer, "EB") == 0 ||
- strcmp(buffer, "EC") == 0 ||
- strcmp(buffer, "ED") == 0 ||
- strcmp(buffer, "EE") == 0 ||
- strcmp(buffer, "EF") == 0
- )
- {
- return ERR_CODE_RTN_ELECTICAL_SOURCE;
- }
- return -1;
- }
- int CUpsUh11::UhRequestWrStatus(char szSendMsg[])
- {
- return 0;
- }
- int CUpsUh11::UhResponseWrStatus(char szSendMsg[])
- {
- return 0;
- }
- int CUpsUh11::StrLower(char *pSource, char *pDest)
- {
- int length, i;
- length = (int)strlen(pSource);
- for (i = 0; i < length; i++) {
- if ((pSource[i] >= 'A') && (pSource[i] <= 'Z')){
- pDest[i] = _tolower(pSource[i]);
- }
- else
- pDest[i] = pSource[i];
- }
- return 0;
- }
- BOOL CUpsUh11::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;
- }
- int CUpsUh11::SendSetReuest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szMsg[VAR_MSG], // 响应的值
- 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[MAX_RESERVED1], // 预留字符数组参数1接口
- char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
- char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
- char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
- char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
- )
- {
- return 0;
- }
- UINT CUpsUh11::GetCheckSum(char *pBuf, char chDest[5], int len)
- {
- WORD iSum = 0;
- //unsigned char chCompliment[2] = {0};
- for(int i=0; i<len; i++)//求和
- {
- //TRACE("%x\r\n", pBuf[i]);
- iSum += pBuf[i];
- }
- WORD iCompliment = iSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- //DigitToBinary(iCompliment, chDest, sizeof(chDest));
- 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);
- }
|