123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #include "stdafx.h"
- #include "CarelProcess.h"
- #include "Global.h"
- #include "struct.h"
- #include "NoticeQueue.h"
- HINSTANCE g_hCarelLibModule = NULL;
- CAREL_DLLInit pCarel_DLLInit = NULL;
- CAREL_DLLUnInit pCarel_DLLUnInit = NULL;
- CAREL_DLLRequestData pCarelRequestData = NULL;
- CAREL_DLLControl pCarelControl = NULL;
- CRITICAL_SECTION g_csCarelReadOneData;
- char g_szCarelMsg[CAREL_MAX_MSG] = {0};
- int CarelCommandSend();
- void UnInitCarekDll();
- int GetCarelFromIni( char chCmd[32], char szType[MAX_ID], char szIndex[MAX_ID] );
- BOOL LoadCarelDll(CString strpath)
- {
- char strFile[256] = {0};
- g_hCarelLibModule = NULL;
- sprintf(strFile, "%s\\dll\\Carel.dll", strpath);
- InitializeCriticalSection( &g_csCarelReadOneData );
- g_hCarelLibModule = AfxLoadLibrary(strFile);
- //大金空调动态库初始化
- if (NULL != g_hCarelLibModule)
- {
- pCarel_DLLInit =(CAREL_DLLInit)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLInit");
- pCarel_DLLUnInit =(CAREL_DLLUnInit)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLUnInit");
- pCarelRequestData = (CAREL_DLLRequestData)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLRequestData");
- pCarelControl = (CAREL_DLLControl)::GetProcAddress(g_hCarelLibModule, "CAREL_DLLControl");
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- BOOL InitCarelComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
- {
- if (pCarel_DLLInit(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
- return TRUE;
- else
- return FALSE;
- }
- int CarelRequestData(
- int nPort,
- int nDevAddr,
- char chDevUid[20],
- int iVarID,
- char chRs232cmd[32])
- {
- char chMsg[80] = {0};
- char szType[MAX_ID] = {0};
- char szIndex[MAX_ID] = {0};
- GetCarelFromIni((char*)(LPCTSTR)chRs232cmd,
- szType,
- szIndex);
- int nRet = -1;
- nRet = pCarelRequestData( nPort, //端口
- nDevAddr, //设备地址
- chRs232cmd, //命令
- szType, //变量类型
- szIndex, //变量索引
- g_szCarelMsg //输出缓冲区
- );
- if( nRet != 0 )
- {
- return -1;
- }
- nRet = CarelSingleResponseData(chDevUid, iVarID, szType, g_szCarelMsg );
- return nRet;
- }
- int CarelSingleResponseData(char chDevUid[20], int iVarID, char szType[MAX_ID], 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_csCarelReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csCarelReadOneData);
- DWORD nValue = 0;
- WORD wdValue = 0;
- double fCoef = pBaseVar->m_dbCoefficient;
- int nDataLen = pBaseVar->m_iDataLen;
- int nVarItemID = pBaseVar->m_nVarItemID;
- if( strcmp(szType, "A") == 0 )
- {
- INT16 nValue16;
- BYTE *pB = (BYTE*)&nValue16;
- pB[1] = TwoByteToByte(chMsg[0], chMsg[1]);
- pB[0] = TwoByteToByte(chMsg[2], chMsg[3]);
- nValue = nValue16;
- }
- else if( strcmp(szType, "I") == 0 )
- {
- }
- else if( strcmp(szType, "D") == 0 )
- {
- nValue = AsciiToBYTE(chMsg[0]);
- }
- EnterCriticalSection( &g_csCarelReadOneData );
- 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;
- }
- LeaveCriticalSection( &g_csCarelReadOneData );
- if( pBaseVar->m_nVarTypeID != SNMP_NPM_TYPE &&
- pBaseVar->m_nVarTypeID != SNMP_STRING_ID )
- {
- if( (int)pBaseVar->m_dbData < pBaseVar->m_nReserved2 ||
- (int)pBaseVar->m_dbData > pBaseVar->m_nReserved3 )
- {
- return -1;
- }
- }
- //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_ParadigmReadOneData.dbData);
-
- EnterCriticalSection(&g_csCarelReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csCarelReadOneData);
- return 0;
- }
- void UnInitCarelDll()
- {
- if (g_hCarelLibModule != NULL && ( NULL != pCarel_DLLUnInit ))
- {
- pCarel_DLLUnInit();
- }
- DeleteCriticalSection( &g_csCarelReadOneData );
- if (g_hCarelLibModule!= NULL)
- {
- AfxFreeLibrary(g_hCarelLibModule);
- g_hCarelLibModule = NULL;
- }
- }
- int GetCarelFromIni(char chCmd[32], char szType[MAX_ID], char szIndex[MAX_ID])
- {
- CHAR strFile[MAX_FILE_LENGTH + 1] = "";
- wsprintf(strFile, "%s\\config\\Carel.ini", g_strDirectory);
- //类型
- GetPrivateProfileString(chCmd, "type", "", szType, sizeof(szType), strFile);
- //索引
- GetPrivateProfileString(chCmd, "index", "", szIndex, sizeof(szIndex), strFile);
-
- return 0;
- }
- int CarelControlRequest(char chDevUid[20], int iVarID, double data, char chRs232cmd[32])//用于请求写命令操作
- {
- 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];
-
- char szMsg[MAX_VAR_MSG] = {0};
- char szType[MAX_ID] = {0};
- char szIndex[MAX_ID] = {0};
- if( pCarelControl( pDev->m_iPort,
- pDev->m_iDevideaddr,
- chRs232cmd,
- szType,
- szIndex,
- szMsg
- ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
|