/************************************************* /* Copyright (C), 2010-2011,StoneU. Co., Ltd. /* File name: Bagkee.cpp /* Author: Zero.t /* Version: Ver1.0.0.1 /* Date: 2011-8-18 /* Description: 柏克B系列UPS,具体型号未知;本类首次使用在项目广东 顺德锦丽; /* Others: UPS协议类,不做控制设备操作; /* Function List: ### /* History: 1.Date: ### Author: ### Modification: ### *************************************************/ #include "StdAfx.h" #include "CommProcess.h" #include ".\bagkee.h" CBagkee::CBagkee( 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_szG1Msg, 0, sizeof(m_szG1Msg)); memset(m_szG2Msg, 0, sizeof(m_szG2Msg)); memset(m_szG3Msg, 0, sizeof(m_szG3Msg)); memset(m_szQ1Msg, 0, sizeof(m_szQ1Msg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } CBagkee::~CBagkee() { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL CBagkee::BagkeeOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval) { //LOG4C((LOG_NOTICE,"打开串口")); BOOL bResult = FALSE; bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval ); return bResult; } int CBagkee::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,"读到设备数据")); int nIndex = 0, nLen = 0; char szSendMsg[UPS_SANTAK_SEND_MSG] = {0}; char szType[CMD_TYPE] = {0}; m_address = nAddr; GetParFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if( ( strlen(m_szG1Msg) == 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-7") == 0 || strcmp(szCmd,"cmd-8") == 0 ))|| (strlen(m_szG2Msg) == 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 ))|| (strlen(m_szG3Msg) == 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 ))|| (strlen(m_szQ1Msg) == 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-1") == 0 || strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd,"cmd-27") == 0 || strcmp(szCmd,"cmd-39") == 0 ) { nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); if( nRet != 0 ) return nRet; } if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 ) { m_devOnline = FALSE; } else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 ) { m_devOnline = TRUE; } if( m_devOnline == FALSE ) { return -1; } nRet = GetBagkeeG1Msg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetBagkeeG2Msg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetBagkeeG3Msg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetBagkeeQ1Msg(szCmd, szMsg, nIndex, nLen, szType); //LOG4C_HEX_DUMP((LOG_NOTICE, szMsg, sizeof(szMsg))); //LOG4C((LOG_NOTICE, "%s=%d, sizeof = %d, return %d", szCmd, atoi(szMsg), sizeof(buf), nRet)); return nRet; } int CBagkee::GetDeviceParam( int nAddr, CCommProcess *pComm, // 串口对象指针 char szSendMsg[UPS_SANTAK_SEND_MSG], // 发送Buffer char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[CMD_TYPE] // 变量数据类型 ) { //LOG4C((LOG_NOTICE,"GetDeviceParam")); int nRet = -1; nRet = RequestStatus(nAddr, pComm, szSendMsg); if( nRet != 0 ) { return nRet; // 串口忙 } nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType); return nRet; } int CBagkee::GetParFromIni( char szPath[MAX_PATH], //服务器程序所在目录 char szIniName[MAX_PATH], //配置文件名 char szCmd[MAX_CMD], //命令 char szSendCmd[MAX_CMD], //发送Buffer char nType[MAX_CMD], int &nIndex, int &nLen ) { CHAR szFile[MAX_PATH + 1] = ""; wsprintf(szFile, "%s\\config\\%s", szPath, szIniName); TRACE("szFile =%s\n",szFile); GetPrivateProfileString(szCmd, "SendCmd", "", szSendCmd, 10, szFile); szSendCmd[strlen(szSendCmd)] = 0x0D; GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile); nType[strlen(nType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); //LOG4C((LOG_NOTICE,"SendCmd = %s",szSendCmd)); return 0; } int CBagkee::RequestStatus(int nAddr, CCommProcess *pComm,const char chSendMsg[UPS_SANTAK_SEND_MSG] ) { #if UPS_DEBUG //开关量,用于测试模拟数据 REQUEST_STRUCT structResuest; int nLen = sizeof(REQUEST_STRUCT); memset(&structResuest,0,nLen); memcpy(structResuest.chkSum, chSendMsg, 2); structResuest.End = 0x0D; //LOG4C_HEX_DUMP((LOG_NOTICE, chSendMsg, 2)); 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, chSendMsg, 3)); //LOG4C((LOG_NOTICE, "CSanTak::RequestStatus chSendMsg = %s, nResult = %d, nDatalen = %d", chSendMsg,nResult, nDatalen)); if (nResult == nDatalen) { } else { SetEvent(m_hSemComm); return EER_CODE_BAGKEE_COM_REGNUM; } } else { return ERR_CODE_BAGKEE_COM_BUSY; //串口忙 } #endif return 0; } int CBagkee::ResponseStatus( int nAddr, CCommProcess *pComm, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[CMD_TYPE] ) { #if UPS_DEBUG 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 , nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen); //LOG4C((LOG_NOTICE, "CSanTak::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen)); if (nReadLen <= 0) { SetEvent(m_hSemComm); if (pBuff != NULL) { delete [] pBuff; pBuff = NULL; } return ERR_CODE_BAGKEE_COM_READ_NO_DATA; } //长度校验出错 SetBagkeeG1Msg(szCmd, pBuff); SetBagkeeG2Msg(szCmd, pBuff); SetBagkeeG3Msg(szCmd, pBuff); SetBagkeeQ1Msg(szCmd, pBuff); m_dwOnlineTick = GetTickCount(); // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } int CBagkee::GetBagkeeG1Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[CMD_TYPE] ) { 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-7") == 0 || strcmp(szCmd,"cmd-8") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG1Msg + nIndex, szMsg, nLen); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CBagkee::GetBagkeeG2Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[CMD_TYPE] ) { int nRet = 0; if ( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szG2Msg + nIndex, szMsg, nLen); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CBagkee::GetBagkeeG3Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[CMD_TYPE] ) { int nRet = 0; if ( 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_szG3Msg + nIndex, szMsg, nLen); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CBagkee::GetBagkeeQ1Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[CMD_TYPE] ) { int nRet = 0; if ( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szType, m_szQ1Msg + nIndex, szMsg, nLen); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } void CBagkee::SetBagkeeG1Msg(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-7") == 0 || strcmp(szCmd,"cmd-8") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG1Msg, pBuff, sizeof(m_szG1Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CBagkee::SetBagkeeG2Msg(char szCmd[MAX_CMD], char *pBuff) { if ( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szG2Msg, pBuff, sizeof(m_szG2Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CBagkee::SetBagkeeG3Msg(char szCmd[MAX_CMD], char *pBuff) { if ( 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_szG3Msg, pBuff, sizeof(m_szG3Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CBagkee::SetBagkeeQ1Msg(char szCmd[MAX_CMD], char *pBuff) { if ( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szQ1Msg, pBuff, sizeof(m_szQ1Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CBagkee::SimulationCommData(void) { memcpy(m_szG1Msg, "!407 100 0000 000.0 +34.0 51.1 51.1 50.8", sizeof("!407 100 0000 000.0 +34.0 51.1 51.1 50.8")); memcpy(m_szG2Msg, "!00000001 00000111 00000000", sizeof("!00000001 00000111 00000000")); memcpy(m_szG3Msg, "!221.0/225.1/219.3 221.0/225.1/219.3 219.4/218.7/218.7 001.0/001.0/006.0", sizeof("!221.0/225.1/219.3 221.0/225.1/219.3 219.4/218.7/218.7 001.0/001.0/006.0")); memcpy(m_szQ1Msg, "(224.4 000.0 219.2 001 51.2 2.28 33.0 00000000", sizeof("(224.4 000.0 219.2 001 51.2 2.28 33.0 00000000")); }