| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- #include "stdafx.h"
- #include "Daikin_Modify.h"
- DaiKin_Modify::DaiKin_Modify(char *szPath,char *szIniName,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 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
- for( int i = 0; i < 9; i++ )
- {
- memset(m_szDaKin_400Msg[i], 0, sizeof(m_szDaKin_400Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- DaiKin_Modify::~DaiKin_Modify()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL DaiKin_Modify::DaiKinOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
- {
- BOOL bResult = FALSE;
- bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
- return bResult;
- }
- int DaiKin_Modify::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "-1", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
- GetPrivateProfileString(szCmd, "type", "-1", szDataType, 10, szFile);
- szDataType[strlen(szDataType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", -1, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", -1, szFile);
- iSBit = GetPrivateProfileInt(szCmd, "StaBit", -1, szFile);//从配置文件中取值
- iEBit = GetPrivateProfileInt(szCmd, "EndBit", -1, szFile);
- return 0;
- }
- // 发送读取设备参数请求
- int DaiKin_Modify::SendReadRequest(
- char *szPath,
- char *szIniName,
- int nCommPort,
- int nAddr,
- char *szCmd,
- char *szMsg,
- int nReversed1,
- int nReversed2,
- int nReversed3,
- int nReversed4,
- int nReversed5,
- float fReversed1,
- float fReversed2,
- float fReversed3,
- char *szReversed1,
- char *szReversed2,
- char *szReversed3,
- char *szReversed4,
- char *szReversed5 )
- {
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
- char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
- GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
- int iCmd = atoi(szCmd + 4);
- if( (strlen(m_szDaKin_400Msg[nAddr -1]) == 0 && (iCmd >= 5 && iCmd <= 15)) || iCmd == 5)
- {
- nRet = GetDeviceParam(nAddr, pComm,szCmd, IniSendCMD, IniSendlen);
- if( nRet != 0 ) return nRet;
- }
- if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- m_devOnline[nAddr - 1] = FALSE;
- else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- m_devOnline[nAddr - 1] = TRUE;
- if( m_devOnline[nAddr - 1] == FALSE )
- return -1;
- nRet = GetDaiKin_400VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType, iSBit, iEBit);
- //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szCmd= %s, szMsg = %s", szCmd, szMsg));
- return nRet;
- }
- void DaiKin_Modify::BitDataProcess(const BYTE &bySour, char *szRecv,int &StaBit,int &EndBit)
- {
- int ByteBit = 0;
- int sum = bySour - 48;
- switch(StaBit)
- {
- case 0:
- ByteBit = sum & 0x01;
- break;
- case 1:
- ByteBit = (sum & 0x02) / 0x02 ;
- break;
- case 2:
- ByteBit = (sum & 0x04) / 0x04 ;
- break;
- case 3:
- ByteBit = (sum & 0x08) / 0x08 ;
- break;
- case 4:
- ByteBit = (sum & 0x10) / 0x10 ;
- break;
- case 5:
- ByteBit = (sum & 0x20) / 0x20 ;
- break;
- case 6:
- ByteBit = (sum & 0x40) / 0x40 ;
- break;
- case 7:
- ByteBit = (sum & 0x80) / 0x80;
- break;
- }
- sprintf(szRecv,"%d",ByteBit);
- }
- int DaiKin_Modify::GetDaiKin_400VarMsg( int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- //DataConversion(szType, m_szDaKin_400Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
- _BYTE_1_Decimal(szMsg,(BYTE*)m_szDaKin_400Msg[nAddr - 1] +nIndex);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- return 0;
- }
- int DaiKin_Modify::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD, const int &IniSendlen)
- {
- int nRet = -1;
- nRet = Send_ReadDeviceData(nAddr,pComm,szCmd,IniSendCMD,IniSendlen);
- if( nRet != 0 )
- return nRet;
- nRet = Recv_ReadDeviceData(nAddr, pComm, szCmd);
- return nRet;
- }
- int DaiKin_Modify::SetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *szSetMsg,char *szRecvMsg,char *IniSendCMD, const int &IniSendlen)
- {
- int nRet = -1;
- nRet = Send_WriteDeviceData(nAddr, pComm, szCmd, szSetMsg,IniSendCMD, IniSendlen );
- if( nRet != 0 )
- return nRet;
- nRet = Recv_WriteDeviceData(nAddr, pComm, szCmd, szRecvMsg);
- return nRet;
- }
- // 发送设置设备参数请求
- int DaiKin_Modify::SendSetReuest(
- char *szPath,
- char *szIniName,
- int nCommPort,
- int nAddr,
- char *szCmd,
- char *szSetMsg,
- int nReversed1,
- int nReversed2,
- int nReversed3,
- int nReversed4,
- int nReversed5,
- float fReversed1,
- float fReversed2,
- float fReversed3,
- char *szReversed1,
- char *szReversed2,
- char *szReversed3,
- char *szReversed4,
- char *szReversed5
- )
- {
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
- char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
- GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
- char szRecvMsg[10] = {0};
- SetDeviceParam(nAddr, pComm, szCmd, szSetMsg,szRecvMsg,IniSendCMD,IniSendlen);
- return 0;
- }
- int DaiKin_Modify::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd,const char *IniSendCMD,const int &IniSendlen)
- {
- int nRet = -1;
- #if DEBUG_DAIKIN_MODIFY
- int iSendLen = 0;
- BYTE byArySend[30] = {0};
- byArySend[0] = 0x7E;
- byArySend[1] = nAddr;
- byArySend[2] = 0x01;
- byArySend[3] = 0x05;
- byArySend[4] = byArySend[1] +
- byArySend[2] +
- byArySend[3];
- // EOI;
- byArySend[5] = 0x0D;
- //LOG4C_HEX_DUMP((LOG_NOTICE,(char *)byArySend,30));
- iSendLen = 6;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write(byArySend, iSendLen);
- if( nResult != iSendLen )
- {
- SetEvent( m_hSemComm );
- LOG4C((LOG_NOTICE,"DAKIN Write长度错误"));
- return EER_CODE_COM_WRITE_DATA;
- }
- }
- else
- {
- LOG4C((LOG_NOTICE,"DAKIN 无法获取信号控制"));
- return ERR_CODE_COM_BUSY;
- }
- #endif
- return 0;
- }
- int DaiKin_Modify::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
- {
- #if DEBUG_DAIKIN_MODIFY
- BYTE *byAryRecv = new BYTE[90];
- ZeroMemory(byAryRecv, 90);
- int iRecvLen = pComm->Read(byAryRecv,90);
- if( iRecvLen <= 0)
- {
- SetEvent( m_hSemComm );
- if( byAryRecv != NULL)
- {
- delete[] byAryRecv;
- byAryRecv = NULL;
- }
- LOG4C((LOG_NOTICE, "DAKIN 串口没有读到数据"));
- return ERR_CODE_COM_READ_NO_DATA;
- }
-
- // copy to the array ;
- memcpy(m_szDaKin_400Msg[nAddr -1], byAryRecv, iRecvLen);
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- //LOG4C_HEX_DUMP((LOG_NOTICE,(char *)m_szDaKin_400Msg[nAddr -1],iRecvLen));
- SetEvent( m_hSemComm );
- if( byAryRecv != NULL)
- {
- delete[] byAryRecv;
- byAryRecv = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- int DaiKin_Modify::Send_WriteDeviceData( int nAddr,CCommProcess *pComm, char *szCmd, char *szSetMsg,char *IniSendCMD, const int &IniSendlen)
- {
- #if DEBUG_DAIKIN_MODIFY
- /************************************************************************/
- int iSendLen = 0;
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- BYTE byArySend[120] = {0};
- // SOI;
- byArySend[0] = 0x7E;
- byArySend[1] = nAddr;
- byArySend[2] = 0x03;
- byArySend[3] = 0x04;
- byArySend[4] = atoi(IniSendCMD);
- if ( byArySend[4] == 9)
- {
- byArySend[5] = TwoHexCharToChar(szSetMsg[0] ,szSetMsg[1]);
- //LOG4C((LOG_NOTICE,"Set = %s,%d",szSetMsg,byArySend[5]));
- }
- else
- byArySend[5] = atoi(szSetMsg);
- byArySend[6] = byArySend[1] +
- byArySend[2] +
- byArySend[3] +
- byArySend[4] +
- byArySend[5] ;
- byArySend[7] = 0x0D;
- iSendLen = 8;
- //LOG4C_HEX_DUMP((LOG_NOTICE,(char *)byArySend,8));
- /************************************************************************/
- ResetEvent( m_hSemComm );
- // 读取上次未读的数据,确保空调缓存已无数据;
- BYTE byRecv[250] = {0};
- pComm->Read(byRecv, 250);
- Sleep(100);
- int nResult = pComm->Write(byArySend, iSendLen);
- if( nResult != iSendLen )
- {
- SetEvent( m_hSemComm );
- LOG4C((LOG_NOTICE,"DAIKIN 写长度不对"));
- return EER_CODE_COM_WRITE_DATA;
- }
- Sleep(200);
- #endif
- return 0;
- }
- int DaiKin_Modify::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg)
- {
- #if DEBUG_DAIKIN_MODIFY
- int nReceiveLen = 250;
- int nProcessLen = 0;
- int nReadLen = 0;
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- //Sleep(200);
-
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- if( nReadLen <= 0)
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE,"DAKIN 写 串口没有读到数据"));
- return ERR_CODE_COM_READ_NO_DATA;
- }
- // 设置串口等待事件为有信号;
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- void DaiKin_Modify::SimulationCommData(int nAddr)
- {
- m_szDaKin_400Msg[nAddr -1][0] = 0x7E;
- m_szDaKin_400Msg[nAddr -1][1] = 0x01;
- m_szDaKin_400Msg[nAddr -1][2] = 0x11;
- m_szDaKin_400Msg[nAddr -1][3] = 0x05;
- m_szDaKin_400Msg[nAddr -1][4] = 0x16;
- m_szDaKin_400Msg[nAddr -1][5] = 0x16;
- m_szDaKin_400Msg[nAddr -1][6] = 0x20;
- m_szDaKin_400Msg[nAddr -1][7] = 0x10;
- m_szDaKin_400Msg[nAddr -1][8] = 0x20;
- m_szDaKin_400Msg[nAddr -1][9] = 0x10;
- m_szDaKin_400Msg[nAddr -1][10] = 0x01;
- m_szDaKin_400Msg[nAddr -1][11] = 0x62;
- m_szDaKin_400Msg[nAddr -1][12] = 0x17;
- m_szDaKin_400Msg[nAddr -1][13] = 0x02;
- m_szDaKin_400Msg[nAddr -1][14] = 0x00;
- m_szDaKin_400Msg[nAddr -1][15] = 0x1A;
- m_szDaKin_400Msg[nAddr -1][16] = 0x00;
- m_szDaKin_400Msg[nAddr -1][17] = 0x00;
- m_szDaKin_400Msg[nAddr -1][18] = 0x20;
- m_szDaKin_400Msg[nAddr -1][19] = 0x20;
- m_szDaKin_400Msg[nAddr -1][20] = 0x79;
- m_szDaKin_400Msg[nAddr -1][21] = 0x0D;
- }
- void DaiKin_Modify::lowcase2uppcase(BYTE &btSrc)
- {
- if( btSrc >= 'a' && btSrc <= 'z' )
- btSrc = btSrc - 'a' + 'A';
- }
- void DaiKin_Modify::GetCheckSum(char *szData, char *szCheck ,int nlen)
- {
- DWORD dwSum(0);
- for (int i = 0; i < nlen; i++)
- dwSum += szData[i];
- WORD iCompliment = dwSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- itoa(iCompliment, szCheck, 16);
- for (int i(0); i < 5 ; i++)
- lowcase2uppcase((BYTE &)szCheck[i]);
- }
|