123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- #include "stdafx.h"
- #include "UpsParadigmProcess.h"
- #include "Global.h"
- #include "struct.h"
- #include "NoticeQueue.h"
- HANDLE g_PARADIGMRequestThread = NULL;
- HANDLE g_PARADIGMResponseThread = NULL;
- CRITICAL_SECTION g_csParadigmReadOneData;
- //UPS PARADIGM动态库输出函数
- HINSTANCE g_hPARADIGMLibModule;
- PARADIGM_DLLInitCom pPARADIGM_DLLInitCom;
- PARADIGM_DLLInit pPARADIGM_DLLInit;
- PARADIGM_DLLUnInit pPARADIGM_DLLUnInit;
- int ParadigmCommandSend();
- void UnInitParadigmDll();
- int GetParadigmFromIni(char chCmd[32], int &nAddr,int &nVer, int &nCid2,
- char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID]);
- BOOL LoadParadigmDll(CString strpath)
- {
- char strFile[256] = {0};
- g_hPARADIGMLibModule = NULL;
- sprintf(strFile, "%s\\dll\\upsparadigm.dll", strpath);
- InitializeCriticalSection( &g_csParadigmReadOneData );
- g_hPARADIGMLibModule = AfxLoadLibrary(strFile);
- //UPSPARADIGM 动态库初始化
- if (NULL != g_hPARADIGMLibModule)
- {
- pPARADIGM_DLLInitCom =( PARADIGM_DLLInitCom)::GetProcAddress(g_hPARADIGMLibModule, "PARADIGM_DLLInitCom");
- pPARADIGM_DLLInit =(PARADIGM_DLLInit)::GetProcAddress(g_hPARADIGMLibModule, "PARADIGM_DLLInit");
- pPARADIGM_DLLUnInit =(PARADIGM_DLLUnInit)::GetProcAddress(g_hPARADIGMLibModule, "PARADIGM_DLLUnInit");
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- BOOL InitParadigmComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
- {
- if (pPARADIGM_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
- return TRUE;
- else
- return FALSE;
- }
- int ParadigmRequestData(
- int nPort,
- int nDevAddr,
- char chDevUid[20],
- int iVarID,
- char chRs232cmd[32])
- {
- char chMsg[80] = {0};
- //BYTE byAdr = 0x00;//起始位
- //BYTE byVer = 0x00;
- //BYTE byCid2 = 0x00;
- //BYTE byCid1 = 0xAA;//控制标识码
- WORD wLenId = 0x00;//INFO字节长度
- BYTE byCmdType = 0x00;//命令类型
- //BYTE byCmdId = 0x00;
- int nAddr = 0x0;
- int nVer = 0x0;
- int nCid1 = 0x2A;
- int nCid2 = 0x0;
- int nCmdID = 0x0;
- char byDataFlag = 0;
- char chAdrBeginPos[MAX_ID] = {0};
- char chCmdLen[MAX_ID] = {0};
- char chDataLen[MAX_ID] = {0};
- GetParadigmFromIni((char*)(LPCTSTR)chRs232cmd,
- nAddr,
- nVer,
- nCid2,
- chAdrBeginPos,
- chCmdLen,
- chDataLen);
- //TRACE("起始索引=%d, 变量长度=%d,数据长度=%d\r\n", atoi(chAdrBeginPos), atoi(chCmdLen), atoi(chDataLen));
- int nRet = pPARADIGM_DLLInit( nPort,
- nDevAddr,
- nAddr, //设备地址描述
- nVer,
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdType, //命令类型
- nCmdID,
- atoi(chDataLen),
- //48,
- atoi(chAdrBeginPos),
- atoi(chCmdLen),
- chMsg,
- &byDataFlag);
- if(nRet==0)
- {
- nRet = ParadigmSingleResponseData(chDevUid, iVarID, chMsg, byDataFlag, nCid2 );
- }
- return nRet;
- }
- int ParadigmSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80], char byDataFlag, BYTE byCid2)
- {
- 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_csParadigmReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csParadigmReadOneData);
- 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;
- if( nEndBit - nStartBit < 7 )// 一般告警状态会这样子定义,Modbus Rtu 码暂时没有碰到,没有经过调试,待测试
- {
- if( nDataLen == 1 )
- {
- char chBuffer[8] = {0};
- memset(chBuffer, 0, sizeof(chBuffer));
- nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[0 + 1]) & 0x00FF );
- itoa(nValue, chBuffer, 2);
- CString strTemp;
- switch( nEndBit - nStartBit )
- {
- case 0:
- strTemp.Format("%c", chBuffer[nStartBit]);
- break;
- case 1:
- strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
- break;
- case 2:
- strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
- break;
- case 3:
- strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
- chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
- break;
- case 4:
- strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
- chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
- break;
- case 5:
- strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
- chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
- break;
- case 6:
- strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
- chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
- break;
- default:
- LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
- }
- nValue = atoi(strTemp);
- }
- else if( nDataLen == 2 )
- {
- char chBuffer[16] = {0};
- memset(chBuffer, 0, sizeof(chBuffer));
- nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
- ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
- itoa(nValue, chBuffer, 2);
- CString strTemp;
- switch( nEndBit - nStartBit )
- {
- case 0:
- strTemp.Format("%c", chBuffer[nStartBit]);
- break;
- case 1:
- strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
- break;
- case 2:
- strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
- break;
- case 3:
- strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
- chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
- break;
- case 4:
- strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
- chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
- break;
- case 5:
- strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
- chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
- break;
- case 6:
- strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
- chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
- break;
- default:
- LOG4C((LOG_NOTICE, "起始位与终止位不对",pDev->m_strDeviceName, pBaseVar->m_strDesc));
- }
- nValue = atoi(strTemp);
- }
- }
- // 特殊变量处理(例如,一个命令返回两个变量的情况)
- else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
- {
- nStartReg = (int)nStartBit / 8;
- nEndReg = (int)(nEndBit + 1) / 8;
- nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
- }
- else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
- {
- nStartReg = (int)nStartBit / 8;
- nEndReg = (int)(nEndBit + 1) / 8;
- memcpy(&wdValue, chMsg, sizeof(WORD));
- nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
- ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
- }
- EnterCriticalSection( &g_csParadigmReadOneData );
- pBaseVar->m_dbData = (double)(nValue * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csParadigmReadOneData );
- //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] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
- unionVarInt.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
- EnterCriticalSection( &g_csParadigmReadOneData );
- pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csParadigmReadOneData );
- //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] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
- unionVarFloat.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
- unionVarFloat.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
- unionVarFloat.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
- EnterCriticalSection( &g_csParadigmReadOneData );
- pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csParadigmReadOneData );
- //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] = (AsciiToBYTE(chMsg[0]) << 4) | (AsciiToBYTE(chMsg[1]) & 0x00FF);
- unionVarDouble.ch[1] = (AsciiToBYTE(chMsg[2]) << 4) | (AsciiToBYTE(chMsg[3]) & 0x00FF);
- unionVarDouble.ch[2] = (AsciiToBYTE(chMsg[4]) << 4) | (AsciiToBYTE(chMsg[5]) & 0x00FF);
- unionVarDouble.ch[3] = (AsciiToBYTE(chMsg[6]) << 4) | (AsciiToBYTE(chMsg[7]) & 0x00FF);
- unionVarDouble.ch[4] = (AsciiToBYTE(chMsg[8]) << 4) | (AsciiToBYTE(chMsg[9]) & 0x00FF);
- unionVarDouble.ch[5] = (AsciiToBYTE(chMsg[10]) << 4) | (AsciiToBYTE(chMsg[11]) & 0x00FF);
- unionVarDouble.ch[6] = (AsciiToBYTE(chMsg[12]) << 4) | (AsciiToBYTE(chMsg[13]) & 0x00FF);
- unionVarDouble.ch[7] = (AsciiToBYTE(chMsg[14]) << 4) | (AsciiToBYTE(chMsg[15]) & 0x00FF);
- EnterCriticalSection( &g_csParadigmReadOneData );
- pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csParadigmReadOneData );
- //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
- }
- }
- else
- {
- switch( nDataLen )
- {
- case 1:
- nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
- break;
- case 2:
- nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
- ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
- 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_csParadigmReadOneData );
- 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_csParadigmReadOneData );
- //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, pBaseVar->m_dbData);
- }
- }
-
- EnterCriticalSection(&g_csParadigmReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csParadigmReadOneData);
- return 0;
- }
- void UnInitParadigmDll()
- {
- if (g_hPARADIGMLibModule != NULL && ( NULL != pPARADIGM_DLLUnInit ))
- {
- pPARADIGM_DLLUnInit();
- }
- DeleteCriticalSection( &g_csParadigmReadOneData );
- if (g_hPARADIGMLibModule!= NULL)
- {
- AfxFreeLibrary(g_hPARADIGMLibModule);
- g_hPARADIGMLibModule = NULL;
- }
- }
- int GetParadigmFromIni(char chCmd[32], int &nAddr,int &nVer, int &nCid2,
- char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID])
- {
- CHAR strFile[MAX_FILE_LENGTH + 1] = "";
- CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
- CHAR strCid2[MAX_ID] ="";
- char strAdr[MAX_ID]="";
- CHAR strVer[MAX_ID]={0};
- CHAR strParaSum[MAX_ID] = {0};
- CHAR strParaIndex[MAX_ID] = {0};
- wsprintf(strFile, "%s\\config\\upsparadigm.ini", g_strDirectory);
- //模块版本
- GetPrivateProfileString("VER", "Ver", "", strVer, sizeof(strVer), strFile);
- //命令模块
- GetPrivateProfileString(chCmd,"cmdModule", "", strValue, sizeof(strValue), strFile);
- //命令ID的参数索引
- GetPrivateProfileString(chCmd, "Para_Index", "", strParaIndex, sizeof(strParaIndex), strFile);
- //命令长度
- GetPrivateProfileString(chCmd, "cmdlen", "", chCmdLen, sizeof(chCmdLen), strFile);
- //命令的CID2
- GetPrivateProfileString(strValue, "Cid2", "", strCid2, sizeof(strCid2), strFile);
- //模块设备地址
- GetPrivateProfileString(strValue, "Addr", "", strAdr, sizeof(strAdr), strFile);
- //模块命令参数的数量
- GetPrivateProfileString(strValue, "ParaSum", "", strParaSum, sizeof(strParaSum), strFile);
- //数据总长度
- GetPrivateProfileString(strValue, "datalen", "", chDataLen, sizeof(chDataLen), strFile);
- //读:起始位置,写:设置命令
- char ParaIndex[MAX_ID]={0};
- strcpy(ParaIndex, "para");
- strcat(ParaIndex, strParaIndex);
- GetPrivateProfileString(strValue, ParaIndex, "", chAdrBeginPos, sizeof(chAdrBeginPos), strFile);
-
- nCid2 = atoi(strCid2);
- nAddr = atoi(strAdr);
- nVer = atoi(strVer);
-
- return 0;
- }
|