123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- #include "stdafx.h"
- #include "PowerwareProcess.h"
- #include "Global.h"
- #include "struct.h"
- #include "NoticeQueue.h"
- CRITICAL_SECTION g_csPowerwareReadOneData;
- //UPS PARADIGM动态库输出函数
- HINSTANCE g_hPowerwareLibModule;
- POWERWARE_DLLInitCom pPOWERWARE_DLLInitCom;
- POWERWARE_DLLInit pPOWERWARE_DLLInit;
- POWERWARE_DLLUnInit pPOWERWARE_DLLUnInit;
- int PowerwareCommandSend();
- void UnInitPowerwareDll();
- int GetPowerwareFromIni(char chCmd[32], char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID],
- char chRequestLen[MAX_ID], char chResponseLen[MAX_ID], char chCmdID[MAX_ID]);
- BOOL LoadPowerwareDll(CString strpath)
- {
- char strFile[256] = {0};
- g_hPowerwareLibModule = NULL;
- sprintf(strFile, "%s\\dll\\Powerware.dll", strpath);
- InitializeCriticalSection( &g_csPowerwareReadOneData );
- g_hPowerwareLibModule = AfxLoadLibrary(strFile);
- //UPSPARADIGM 动态库初始化
- if (NULL != g_hPARADIGMLibModule)
- {
- pPOWERWARE_DLLInitCom =( POWERWARE_DLLInitCom)::GetProcAddress(g_hPowerwareLibModule, "POWERWARE_DLLInitCom");
- pPOWERWARE_DLLInit =(POWERWARE_DLLInit)::GetProcAddress(g_hPowerwareLibModule, "POWERWARE_DLLInit");
- pPOWERWARE_DLLUnInit =(POWERWARE_DLLUnInit)::GetProcAddress(g_hPowerwareLibModule, "POWERWARE_DLLUnInit");
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- BOOL InitPowerwareComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
- {
- if (pPOWERWARE_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
- return TRUE;
- else
- return FALSE;
- }
- int PowerwareRequestData(
- int nPort,
- int nDevAddr,
- char chDevUid[20],
- int iVarID,
- char chRs232cmd[32])
- {
- char chMsg[80] = {0};
- char chAdrBeginPos[MAX_ID] = {0};
- char chCmdLen[MAX_ID] = {0};
- char chRequestLen[MAX_ID] = {0};
- char chResponseLen[MAX_ID] = {0};
- char chCmdID[MAX_ID] = {0};
- GetPowerwareFromIni((char*)(LPCTSTR)chRs232cmd,
- chAdrBeginPos,
- chCmdLen,
- chRequestLen,
- chResponseLen,
- chCmdID);
- //TRACE("起始索引=%d, 变量长度=%d,数据长度=%d\r\n", atoi(chAdrBeginPos), atoi(chCmdLen), atoi(chDataLen));
- int nRet = pPOWERWARE_DLLInit( nPort,
- nDevAddr,
- atoi(chRequestLen),
- atoi(chResponseLen),
- atoi(chCmdID),
- atoi(chAdrBeginPos),
- atoi(chCmdLen),
- 0,
- chMsg);
- if(nRet==0)
- {
- nRet = PowerwareSingleResponseData(chDevUid, iVarID, chMsg);
- }
- return nRet;
- }
- int PowerwareSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80])
- {
- int nDeviceIndex = -1, nVarIndex = -1;
- BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
- if( bFind == FALSE ) return -1;
- CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
- CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
- EnterCriticalSection(&g_csPowerwareReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csPowerwareReadOneData);
- DWORD nValue = 0;
- WORD wdValue = 0;
- double fCoef = pBaseVar->m_dbCoefficient;
- int nDataLen = pBaseVar->m_iDataLen;
- int nVarItemID = pBaseVar->m_nVarItemID;
- if( nVarItemID > 0 )//BIT位变量
- {
- int nStartBit = pBaseVar->m_nStartBit;
- int nEndBit = pBaseVar->m_nEndBit;
- int nStartReg, nEndReg;
- char szValue[4];
- if( nEndBit - nStartBit < 7 )
- {
- if( nDataLen == 1 )
- {
- char chBuffer[8] = {0};
- memset(chBuffer, 0, sizeof(chBuffer));
- BYTE byValue = chMsg[0];
- DigitToBinary(byValue, chBuffer, 8) ;
- CString strTemp;
- switch( nEndBit - nStartBit )
- {
- case 0:
- strTemp.Format("%c", chBuffer[nStartBit]);
- nValue = atoi(strTemp);
- break;
- case 1:
- strTemp.Format("00%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
- szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
- szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
- szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
- szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
- nValue = Hex16(szValue);
- break;
- case 2:
- strTemp.Format("0%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
- szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
- szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
- szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
- szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
- nValue = Hex16(szValue);
- break;
- case 3:
- strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
- chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
- szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
- szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
- szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
- szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
- nValue = Hex16(szValue);
- break;
- default:
- LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
- }
- }
- else if( nDataLen == 2 )
- {
- char chBuffer[16] = {0};
- memset(chBuffer, 0, sizeof(chBuffer));
- WORD wValue;
- memcpy(&wValue, chMsg, sizeof(WORD));
- wValue = ntohs(wValue);
- DigitToBinary(wValue, chBuffer, 16) ;
- CString strTemp;
- switch( nEndBit - nStartBit )
- {
- case 0:
- strTemp.Format("%c", chBuffer[nStartBit]);
- nValue = atoi(strTemp);
- break;
- case 1:
- strTemp.Format("00%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
- szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
- szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
- szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
- szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
- nValue = Hex16(szValue);
- break;
- case 2:
- strTemp.Format("0%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
- szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
- szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
- szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
- szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
- nValue = Hex16(szValue);
- break;
- case 3:
- strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
- chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
- szValue[0] = AsciiToBYTE( strTemp.GetAt(0) );
- szValue[1] = AsciiToBYTE( strTemp.GetAt(1) );
- szValue[2] = AsciiToBYTE( strTemp.GetAt(2) );
- szValue[3] = AsciiToBYTE( strTemp.GetAt(3) );
- nValue = Hex16(szValue);
- break;
- default:
- LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
- }
- }
- }
- // 特殊变量处理(例如,一个命令返回两个变量的情况)
- else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
- {
- nStartReg = (int)nStartBit / 8;
- nEndReg = (int)(nEndBit + 1) / 8;
- nValue = chMsg[nStartReg];
- }
- else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
- {
- nStartReg = (int)nStartBit / 8;
- nEndReg = (int)(nEndBit + 1) / 8;
- memcpy(&wdValue, chMsg, sizeof(WORD));
- nValue = (DWORD)ntohs(wdValue);
- }
- EnterCriticalSection( &g_csPowerwareReadOneData );
- pBaseVar->m_dbData = (double)(nValue * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csPowerwareReadOneData );
- //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
- }
- else
- {
- // 联合类型变量做特殊处理
- if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
- {
- if( nDataLen == 2 )
- {
- //int类型
- union __UNION_VAR_INT{
- char ch[2];
- int value;
- }unionVarInt;
-
- unionVarInt.ch[0] = chMsg[0];
- unionVarInt.ch[1] = chMsg[1];
- EnterCriticalSection( &g_csPowerwareReadOneData );
- pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csPowerwareReadOneData );
- //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
- }
- else if( nDataLen == 4 )
- {
- //float类型
- union __UNION_VAR_FLOAT{
- char ch[4];
- float value;
- }unionVarFloat;
- unionVarFloat.ch[0] = chMsg[0];
- unionVarFloat.ch[1] = chMsg[1];
- unionVarFloat.ch[2] = chMsg[2];
- unionVarFloat.ch[3] = chMsg[3];
- EnterCriticalSection( &g_csPowerwareReadOneData );
- if( unionVarFloat.value < 0 )
- {
- pBaseVar->m_dbData = 0.0;
- }
- else
- {
- pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
- }
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csPowerwareReadOneData );
- //TRACE("%s\r\n", chMsg);
- //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
- }
- else if( nDataLen == 8 )
- {
- //double类型
- union __UNION_VAR_DOUBLE{
- char ch[8];
- double value;
- }unionVarDouble;
- unionVarDouble.ch[0] = chMsg[0];
- unionVarDouble.ch[1] = chMsg[1];
- unionVarDouble.ch[2] = chMsg[2];
- unionVarDouble.ch[3] = chMsg[3];
- unionVarDouble.ch[4] = chMsg[4];
- unionVarDouble.ch[5] = chMsg[5];
- unionVarDouble.ch[6] = chMsg[6];
- unionVarDouble.ch[7] = chMsg[7];
- EnterCriticalSection( &g_csPowerwareReadOneData );
- pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csPowerwareReadOneData );
- //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
- }
- }
- else if( pBaseVar->m_nVarTypeID == MINUTE_TYPE_ID )
- {
- short int nValue;
- memcpy(&nValue, chMsg, sizeof(short int));
- EnterCriticalSection( &g_csPowerwareReadOneData );
- pBaseVar->m_dbData = (double)(nValue / 60.0 * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csPowerwareReadOneData );
- }
- else
- {
- switch( nDataLen )
- {
- case 1:
- nValue = chMsg[0] & 0x000000FF;
- break;
- case 2:
- short int wdValue;
- memcpy(&wdValue, chMsg, sizeof(short int));
- nValue = ntohs(wdValue);
- break;
- case 3: // 一般为字符串类型,不作处理
- LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
- break;
- case 4: // 保留,暂时不知道什么处理
- LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
- break;
- case 5: // 一般为字符串类型,不作处理
- LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
- break;
- case 6: // 一般为字符串类型,不作处理
- LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
- break;
- case 7: // 一般为字符串类型,不作处理
- LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
- break;
- case 8: // 保留,暂时没有用到
- LOG4C((LOG_NOTICE, "保留,暂时没有用到"));
- break;
- }
- EnterCriticalSection( &g_csPowerwareReadOneData );
- if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
- pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
- {
- double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
- (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
- pBaseVar->m_dbData = nValue *
- fCoef *
- fScale;
- }
- else
- {
- pBaseVar->m_dbData = (double)(nValue * fCoef);
- }
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
- LeaveCriticalSection( &g_csPowerwareReadOneData );
- //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, pBaseVar->m_dbData);
- }
- }
-
- EnterCriticalSection(&g_csPowerwareReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csPowerwareReadOneData);
- return 0;
- }
- void UnInitPowerwareDll()
- {
- if (g_hPowerwareLibModule != NULL && ( NULL != pPOWERWARE_DLLUnInit ))
- {
- pPOWERWARE_DLLUnInit();
- }
- DeleteCriticalSection( &g_csPowerwareReadOneData );
- if (g_hPowerwareLibModule!= NULL)
- {
- AfxFreeLibrary(g_hPowerwareLibModule);
- g_hPowerwareLibModule = NULL;
- }
- }
- int GetPowerwareFromIni(char chCmd[32], char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID],
- char chRequestLen[MAX_ID], char chResponseLen[MAX_ID], char chCmdID[MAX_ID])
- {
- CHAR strFile[MAX_FILE_LENGTH + 1] = "";
- CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
- CHAR strParaSum[MAX_ID] = {0};
- CHAR strParaIndex[MAX_ID] = {0};
- wsprintf(strFile, "%s\\config\\Powerware.ini", g_strDirectory);
- //命令ID的参数索引
- GetPrivateProfileString(chCmd, "Para_Index", "", strParaIndex, sizeof(strParaIndex), strFile);
- //命令长度
- GetPrivateProfileString(chCmd, "cmdlen", "", chCmdLen, sizeof(chCmdLen), strFile);
- //命令模块
- GetPrivateProfileString(chCmd,"cmdModule", "", strValue, sizeof(strValue), strFile);
- //模块命令参数的数量
- GetPrivateProfileString(strValue, "ParaSum", "", strParaSum, sizeof(strParaSum), strFile);
- //请求长度
- GetPrivateProfileString(strValue, "Requestlen", "", chRequestLen, sizeof(chRequestLen), strFile);
- //响应长度
- GetPrivateProfileString(strValue, "ResponseLen", "", chResponseLen, sizeof(chResponseLen), strFile);
- //命令ID
- GetPrivateProfileString(strValue, "CmdID", "", chCmdID, sizeof(chCmdID), strFile);
- //读:起始位置,写:设置命令
- char ParaIndex[MAX_ID]={0};
- strcpy(ParaIndex, "para");
- strcat(ParaIndex, strParaIndex);
- GetPrivateProfileString(strValue, ParaIndex, "", chAdrBeginPos, sizeof(chAdrBeginPos), strFile);
-
- return 0;
- }
|