123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809 |
- #include "stdafx.h"
- #include "DaiKinProcess.h"
- #include "Global.h"
- #include "struct.h"
- #include "NoticeQueue.h"
- #define SET_RUN 1 // 设置空调运转(开/关)
- #define SET_RUN_MODE 2 // 设置空调运转模式(0:送风,1:制热,2:制冷,3:自动,4:换气清洗,7:除湿
- #define SET_TEMP 3 // 设定温度
- #define SET_FAN 4 // 设置风量
- CRITICAL_SECTION g_csDaiKinReadOneData;
- char g_szDaiKinAnlogMsg[DAIKIN_MAX_MSG] = {0};
- char g_szDaiKinDigitalMsg[DAIKIN_MAX_MSG] = {0};
- char g_szDaiKinAlarmMsg[DAIKIN_MAX_MSG] = {0};
- char g_szDaiKinGetParamMsg[DAIKIN_MAX_MSG] = {0};
- //大金空调动态库输出函数
- HINSTANCE g_hDaiKinLibModule = NULL;
- DAIKIN_DLLInitCom pDaiKin_DLLInitCom = NULL;
- DAIKIN_DLLInit pDaiKin_DLLInit = NULL;
- DAIKIN_DLLUnInit pDaiKin_DLLUnInit = NULL;
- DAIKIN_DLLSetParam pDaiKin_DLLSetParam = NULL;
- DAIKIN_DLLRemoteCtrl pDaiKin_DLLRemoteCtrl = NULL;
- int DaiKinCommandSend();
- void UnInitDaiKinDll();
- int GetDaiKinFromIni(char chCmd[32], int &nVer, int &nCid2,
- char chAdrBeginPos[MAX_ID], char chCmdLen[MAX_ID], char chDataLen[MAX_ID]);
- BOOL LoadDaiKinDll(CString strpath)
- {
- char strFile[256] = {0};
- g_hDaiKinLibModule = NULL;
- sprintf(strFile, "%s\\dll\\Daikin.dll", strpath);
- InitializeCriticalSection( &g_csDaiKinReadOneData );
- g_hDaiKinLibModule = AfxLoadLibrary(strFile);
- //大金空调动态库初始化
- if (NULL != g_hDaiKinLibModule)
- {
- pDaiKin_DLLInitCom =( DAIKIN_DLLInitCom)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLInitCom");
- pDaiKin_DLLInit =(DAIKIN_DLLInit)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLInit");
- pDaiKin_DLLUnInit =(DAIKIN_DLLUnInit)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLUnInit");
- pDaiKin_DLLSetParam = (DAIKIN_DLLSetParam)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLSetParam");
- pDaiKin_DLLRemoteCtrl = (DAIKIN_DLLRemoteCtrl)::GetProcAddress(g_hDaiKinLibModule, "DAIKIN_DLLRemoteCtrl");
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- BOOL InitDaiKinComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
- {
- if (pDaiKin_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
- return TRUE;
- else
- return FALSE;
- }
- int DaiKinRequestData(
- int nPort,
- int nDevAddr,
- char chDevUid[20],
- int iVarID,
- char chRs232cmd[32])
- {
- char chMsg[80] = {0};
- int nCid1 = 0x60;
- int nCid2;
- WORD wLenId = 0x00;//INFO字节长度
- BYTE byCmdType = 0x00;//命令类型
- BYTE byCmdId = 0x00;
- char byDataFlag = 0;
- char chAdrBeginPos[MAX_ID] = {0};
- char chCmdLen[MAX_ID] = {0};
- char chDataLen[MAX_ID] = {0};
- int nVer;
- GetDaiKinFromIni((char*)(LPCTSTR)chRs232cmd,
- nVer,
- nCid2,
- chAdrBeginPos,
- chCmdLen,
- chDataLen);
- int nRet = -1;
- if( strlen(g_szDaiKinAnlogMsg) == 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinAnlogMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- if( strlen(g_szDaiKinDigitalMsg) == 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinDigitalMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- if( strlen(g_szDaiKinAlarmMsg) == 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinAlarmMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- if( strlen(g_szDaiKinGetParamMsg) == 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinGetParamMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- if( strcmp(chRs232cmd, "cmd-1") == 0 && strlen(g_szDaiKinAnlogMsg) > 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinAnlogMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-2") == 0 && strlen(g_szDaiKinDigitalMsg) > 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinDigitalMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-9") == 0 && strlen(g_szDaiKinAlarmMsg) > 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinAlarmMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-11") == 0 && strlen(g_szDaiKinGetParamMsg) > 0 )
- {
- int nRet = pDaiKin_DLLInit( nPort, //端口
- nDevAddr, //设备地址
- nVer, //版本号
- nCid1, //控制标识码
- nCid2, //命令信息
- wLenId, //INFO字节长度
- byCmdId, //命令ID
- atoi(chDataLen), //请求数据长度
- atoi(chAdrBeginPos), //变量索引
- atoi(chCmdLen), //变量长度
- g_szDaiKinGetParamMsg, //读到变量值
- &byDataFlag); //保留未用
- if( nRet != 0 )
- {
- return -1;
- }
- }
- if( strcmp(chRs232cmd, "cmd-1") == 0 )
- {
- if( strlen(g_szDaiKinAnlogMsg) > 0 )
- {
- for( int i = 0; i < atoi(chCmdLen); i++ )
- {
- chMsg[i] = g_szDaiKinAnlogMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
- }
- }
- }
- else if( strcmp(chRs232cmd, "cmd-2") == 0 ||
- strcmp(chRs232cmd, "cmd-3") == 0 ||
- strcmp(chRs232cmd, "cmd-4") == 0 ||
- strcmp(chRs232cmd, "cmd-5") == 0 ||
- strcmp(chRs232cmd, "cmd-6") == 0 ||
- strcmp(chRs232cmd, "cmd-7") == 0 ||
- strcmp(chRs232cmd, "cmd-8") == 0 )
- {
- if( strlen(g_szDaiKinAlarmMsg) > 0 )
- {
- for( int i = 0; i < atoi(chCmdLen); i++ )
- {
- chMsg[i] = g_szDaiKinDigitalMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
- }
- }
- }
- else if( strcmp(chRs232cmd, "cmd-9") == 0 )
- {
- if( strlen(g_szDaiKinDigitalMsg) > 0 )
- {
- for( int i = 0; i < atoi(chCmdLen); i++ )
- {
- chMsg[i] = g_szDaiKinAlarmMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
- }
- }
- }
- else if( strcmp(chRs232cmd, "cmd-11") == 0 ||
- strcmp(chRs232cmd, "cmd-12") == 0 ||
- strcmp(chRs232cmd, "cmd-13") == 0 ||
- strcmp(chRs232cmd, "cmd-14") == 0 )
- {
- if( strlen(g_szDaiKinGetParamMsg) > 0 )
- {
- for( int i = 0; i < atoi(chCmdLen); i++ )
- {
- chMsg[i] = g_szDaiKinGetParamMsg[sizeof(CHILD) + atoi(chAdrBeginPos) + i];
- }
- }
- }
- if( strlen(chMsg) == 0 ) return -1;
- nRet = DaiKinSingleResponseData(chDevUid, iVarID, chMsg, byDataFlag, nCid2 );
- return nRet;
- }
- int DaiKinSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80], char byDataFlag, int nCid2)
- {
- 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_csDaiKinReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csDaiKinReadOneData);
- 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;
- nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
- ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
- }
- EnterCriticalSection( &g_csDaiKinReadOneData );
- pBaseVar->m_dbData = (double)(nValue * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csDaiKinReadOneData );
- //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_csDaiKinReadOneData );
- pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csDaiKinReadOneData );
- //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_csDaiKinReadOneData );
- pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csDaiKinReadOneData );
- //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_csDaiKinReadOneData );
- pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
- if( pBaseVar->m_nRearm != 0 )
- {
- pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
- }
- LeaveCriticalSection( &g_csDaiKinReadOneData );
- //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
- }
- }
- else
- {
- switch( nDataLen )
- {
- case 1:
- nValue = AsciiToBYTE(chMsg[0]) << 4;
- break;
- case 2:
- nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF );
- break;
- case 3: // 一般为字符串类型,不作处理
- LOG4C((LOG_NOTICE, "一般为字符串类型,不作处理"));
- break;
- case 4: // 保留,暂时不知道什么处理
- nValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
- ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
- 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_csDaiKinReadOneData );
- 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_csDaiKinReadOneData );
- //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_ParadigmReadOneData.dbData);
- }
- }
-
- // g_RtuReadOneData.iStatus = GetVarstatus(g_RtuReadOneData.dbData, g_RtuReadOneData.iUpperlimit, g_RtuReadOneData.iLowerlimit, g_RtuReadOneData.iNormalstate);
- EnterCriticalSection(&g_csDaiKinReadOneData);
- pDev->m_dwOnlineTick = GetTickCount();
- LeaveCriticalSection(&g_csDaiKinReadOneData);
- return 0;
- }
- void UnInitDaiKinDll()
- {
- if (g_hDaiKinLibModule != NULL && ( NULL != pDaiKin_DLLUnInit ))
- {
- pDaiKin_DLLUnInit();
- }
- DeleteCriticalSection( &g_csDaiKinReadOneData );
- if (g_hDaiKinLibModule!= NULL)
- {
- AfxFreeLibrary(g_hDaiKinLibModule);
- g_hDaiKinLibModule = NULL;
- }
- }
- int GetDaiKinFromIni(char chCmd[32], 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[4] ="";
- CHAR strVer[10]={0};
- CHAR strParaSum[4] = {0};
- CHAR strParaIndex[4] = {0};
- wsprintf(strFile, "%s\\config\\DaiKin.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(chCmd, "Cid2", "", strCid2, sizeof(strCid2), strFile);
- //模块命令参数的数量
- GetPrivateProfileString(strValue, "ParaSum", "", strParaSum, sizeof(strParaSum), strFile);
- //数据总长度
- GetPrivateProfileString(strValue, "datalen", "", chDataLen, sizeof(chDataLen), strFile);
- //读:起始位置,写:设置命令
- char ParaIndex[12]={0};
- strcpy(ParaIndex, "para");
- strcat(ParaIndex, strParaIndex);
- GetPrivateProfileString(strValue, ParaIndex, "", chAdrBeginPos, sizeof(chAdrBeginPos), strFile);
-
- nCid2 = atoi(strCid2);
- nVer = atoi(strVer);
-
- return 0;
- }
- int RequestDaiKinWrData(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];
- if( strcmp(chRs232cmd, "cmd-15") == 0 ||
- strcmp(chRs232cmd, "cmd-16") == 0 ||
- strcmp(chRs232cmd, "cmd-17") == 0 ||
- strcmp(chRs232cmd, "cmd-18") == 0 ||
- strcmp(chRs232cmd, "cmd-19") == 0 ||
- strcmp(chRs232cmd, "cmd-20") == 0 ) //设置参数
- {
- char chMsg[80] = {0};
- int nCid1 = 0x60;
- int nCid2;
- WORD wLenId = 0x00;//INFO字节长度
- BYTE byCmdType = 0x00;//命令类型
- BYTE byCmdId = 0x00;
- char byDataFlag = 0;
- char chAdrBeginPos[MAX_ID] = {0};
- char chCmdLen[MAX_ID] = {0};
- char chDataLen[MAX_ID] = {0};
- int nVer;
- GetDaiKinFromIni((char*)(LPCTSTR)chRs232cmd,
- nVer,
- nCid2,
- chAdrBeginPos,
- chCmdLen,
- chDataLen);
- CHAR strFile[MAX_FILE_LENGTH + 1] = "";
- CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
- wsprintf(strFile, "%s\\config\\DaiKin.ini", g_strDirectory);
- //模块版本
- GetPrivateProfileString(chRs232cmd,"cmdType", "", strValue, sizeof(strValue), strFile);
- if( pDaiKin_DLLSetParam( pDev->m_iPort,
- pDev->m_iDevideaddr,
- nVer,
- nCid1,
- nCid2,
- atoi(chCmdLen),
- atoi(strValue),
- (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-21") == 0 ) //控制空调运转
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_RUN, 0, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-22") == 0 ) //控制运转模式
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_RUN_MODE, 0, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-23") == 0 ) //设定制冷温度
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_TEMP, 0, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-24") == 0 ) //设定制热温度
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_TEMP, 1, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-25") == 0 ) //设定制热风扇1风量
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_FAN, 0, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-26") == 0 ) //设定制热风扇2风量
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_FAN, 1, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-27") == 0 ) //设定制冷风扇1风量
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_FAN, 2, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- else if( strcmp(chRs232cmd, "cmd-28") == 0 ) //设定制冷风扇2风量
- {
- if( pDaiKin_DLLRemoteCtrl( pDev->m_iPort,
- pDev->m_iDevideaddr, SET_FAN, 3, (int)data ) == 0 )
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- return 0;
- }
|