// DataMate3000.cpp: implementation of the CDataMate3000 class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Daikin.h" #include "CommProcess.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CDaiKin::CDaiKin( char szPath[MAX_PATH], // 程序所在路径 char szIniName[MAX_PATH], // 配置文件名称 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 < MAX_ADDR; i++ ) { memset(m_szDaKin_42Msg[i], 0, sizeof(m_szDaKin_42Msg[i])); memset(m_szDaKin_43Msg[i], 0, sizeof(m_szDaKin_43Msg[i])); memset(m_szDaKin_44Msg[i], 0, sizeof(m_szDaKin_44Msg[i])); memset(m_szDaKin_400Msg[i], 0, sizeof(m_szDaKin_400Msg[i])); m_devOnline[i] = TRUE; m_dwOnlineTick[i] = 0; } } CDaiKin::~CDaiKin() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL CDaiKin::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 CDaiKin::SendReadRequest( // char szPath[MAX_PATH], // 程序所在路径 // char szIniName[MAX_PATH], // 配置文件名称 // int nCommPort, // 串行端口 // int nAddr, // 设备地址 // char szCmd[MAX_CMD], // 请求命令 // char szMsg[VAR_MSG], // 响应的值 // int nReversed1, // 预留整形参数1接口 // int nReversed2, // 预留整形参数2接口 // int nReversed3, // 预留整形参数3接口 // int nReversed4, // 预留整形参数4接口 // int nReversed5, // 预留整形参数5接口 // float fReversed1, // 预留float参数1接口 // float fReversed2, // 预留float参数2接口 // float fReversed3, // 预留float参数3接口 // char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口 // char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口 // char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口 // char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口 // char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口 // ) //{ // int nIndex = 0, nLen = 0; // char szSendMsg[DAIKIN_SEND_MSG] = {0}; // char szType[DK_TYPE_LENGTH] = {0}; // int startBit = 0; // int endBit = 0; // // GetFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit); // // CCommProcess *pComm = FindComm(nCommPort); // // if( pComm == NULL ) return -1; // int nRet = -1; // // if( (strlen(m_szDaKin_42Msg[nAddr - 1]) == 0 && // (strcmp(szCmd, "cmd-1") == 0 ) )|| //获取系统模拟量量化数据(浮点数) // ( strlen(m_szDaKin_43Msg[nAddr - 1]) == 0 && // (strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 || // strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 || // strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ) )|| //获取开关输入状态 // ( strlen(m_szDaKin_44Msg[nAddr - 1]) == 0 && // (strcmp(szCmd, "cmd-8") == 0 ) ) || // strcmp(szCmd, "cmd-1") == 0 || // strcmp(szCmd, "cmd-2") ==0 || // strcmp(szCmd, "cmd-8") == 0 // ) //获取系统参数(定点数) // { // nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); // 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_42VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit); // nRet = GetDaiKin_43VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit); // nRet = GetDaiKin_44VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit); // //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szCmd= %s, szMsg = %s", szCmd, szMsg)); // return nRet; //} int CDaiKin::SendReadRequest( char szPath[MAX_PATH], // 程序所在路径 char szIniName[MAX_PATH], // 配置文件名称 int nCommPort, // 串行端口 int nAddr, // 设备地址 char szCmd[MAX_CMD], // 请求命令 char szMsg[VAR_MSG], // 响应的值 int nReversed1, // 预留整形参数1接口 int nReversed2, // 预留整形参数2接口 int nReversed3, // 预留整形参数3接口 int nReversed4, // 预留整形参数4接口 int nReversed5, // 预留整形参数5接口 float fReversed1, // 预留float参数1接口 float fReversed2, // 预留float参数2接口 float fReversed3, // 预留float参数3接口 char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口 char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口 char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口 char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口 char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口 ) { int nIndex = 0, nLen = 0; char szSendMsg[DAIKIN_SEND_MSG] = {0}; char szType[DK_TYPE_LENGTH] = {0}; int startBit = 0; int endBit = 0; GetFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; int iCmd = atoi(szCmd+4); if( (strlen(m_szDaKin_400Msg[nAddr - 1]) == 0) && (iCmd >=13 && iCmd <= 23) ) //获取系统参数(定点数) { nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); 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, szType, startBit, endBit); //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szCmd= %s, szMsg = %s", szCmd, szMsg)); return nRet; } int CDaiKin::GetDaiKin_42VarMsg( int nAddr, //设备地址 char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH], // 变量数据类型 int startBit, int endBit ) { int nRet = 0; if( strcmp(szCmd, "cmd-1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szDaKin_42Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit); //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDaiKin::GetDaiKin_43VarMsg( int nAddr, //设备地址 char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH],// 变量数据类型 int startBit, int endBit ) { int nRet = 0; if( strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szDaKin_43Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDaiKin::GetDaiKin_44VarMsg(int nAddr, //设备地址 char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH],// 变量数据类型 int startBit, int endBit ) { int nRet = 0; if( strcmp(szCmd, "cmd-8") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szDaKin_44Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDaiKin::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, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif return 0; } int CDaiKin::GetDeviceParam(int nAddr, CCommProcess *pComm, //串口对象指针 char szSendMsg[DAIKIN_SEND_MSG], //发送Buffer char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH]) // 变量数据类型 { int nRet = -1; nRet = RequestStatus(nAddr, szCmd, pComm, szSendMsg); if( nRet != 0 ) { return nRet; // 串口忙 } nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType); return nRet; } int CDaiKin::SetDeviceParam(int nAddr, CCommProcess *pComm, //串口对象指针 char szSendMsg[DAIKIN_SEND_MSG], //发送Buffer char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH], int &iSendLen) // 变量数据类型 { int nRet = -1; nRet = WriteRequestStatus(nAddr, szCmd, pComm, szSendMsg,iSendLen); if( nRet != 0 ) { LOG4C((LOG_NOTICE,"Write Error")); return nRet; // 串口忙 } nRet = WriteResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType); return nRet; } void CDaiKin::SetDaKin_42VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( strcmp(szCmd, "cmd-1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDaKin_42Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_42Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CDaiKin::SetDaKin_43VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDaKin_43Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_43Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CDaiKin::SetDaKin_44VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( strcmp(szCmd, "cmd-8") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDaKin_44Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_44Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } // WORD CDaiKin::GetDataLength(const WORD wLENID, char chLength[4]) // { // //char szLenID[3] = {0}; // ////int nLen = 10; // //DigitToBinary(wLENID, szLenID, sizeof(szLenID)); // // //char chCheckSum = GetLCheckSum(szLenID, sizeof(szLenID)); // // //chLength[0] = chCheckSum; // //chLength[1] = szLenID[0]; // //chLength[2] = szLenID[1]; // //chLength[3] = szLenID[2]; // // return 0; // } // 发送设置设备参数请求 int CDaiKin::SendSetReuest( char szPath[MAX_PATH], // 程序所在路径 char szIniName[MAX_PATH], // 配置文件名称 int nCommPort, // 串行端口 int nAddr, // 设备地址 char szCmd[MAX_CMD], // 请求命令 char szMsg[VAR_MSG], // 响应的值 int nReversed1, // 预留整形参数1接口 int nReversed2, // 预留整形参数2接口 int nReversed3, // 预留整形参数3接口 int nReversed4, // 预留整形参数4接口 int nReversed5, // 预留整形参数5接口 float fReversed1, // 预留float参数1接口 float fReversed2, // 预留float参数2接口 float fReversed3, // 预留float参数3接口 char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口 char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口 char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口 char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口 char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口 ) { LOG4C((LOG_NOTICE,"CtrlMsg = %s",szMsg)); int nIndex = 0, nLen = 0, StaBit = 0, EndBit = 0; char szSendMsg[DAIKIN_SEND_MSG]; char szType[TYPE_LENGTH] = {0}; char szCid2[CID2_LENGTH] = {0}; memset(szSendMsg, 0, DAIKIN_SEND_MSG); GetFromIni(szPath, szIniName, szCmd, szCid2, szType, nIndex, nLen, StaBit, EndBit); int iSendLen = 0; int iLen = sizeof(REQUESTSET_STRCUT); char chLength[4] = {0}; char chChkSum[5] = {0}; BYTE byArySend[120] = {0}; byArySend[0] = 0x7E; byArySend[1] = 0x32; byArySend[2] = 0x30; byArySend[3] = ByteToAscii((nAddr>>4) & 0x0f); byArySend[4] = ByteToAscii(nAddr & 0x0f); //Cid1 byArySend[5] = 0x36; byArySend[6] = 0x30; memcpy(byArySend + 7, szCid2, 2); if ( (strcmp(szCmd, "cmd-9") == 0)) // 开关机; { // lengchck; byArySend[9] = 0x45; byArySend[10] = 0x30; byArySend[11] = 0x30; byArySend[12] = 0x32; int iCtrl = atoi(szMsg); memset(szMsg,0,strlen(szMsg)); itoa(iCtrl,szMsg,16); for (int i(0); i < 2 ; i++) lowcase2uppcase((BYTE &)szMsg[i]); memcpy(byArySend + 13, szMsg, 2); GetCheckSum((char *)byArySend + 1, chChkSum, 14 ); memcpy(byArySend + 15, chChkSum, 4); byArySend[19] = 0x0D; iSendLen = 20; LOG4C((LOG_NOTICE,"cmd-9=空调开关控制:%s",szMsg)); } else if ( (strcmp(szCmd, "cmd-10") == 0)) // 设置风量; { int iRealLen = strlen(szMsg); if ( iRealLen != 4) { LOG4C((LOG_NOTICE,"非法指令长度:%s",szMsg)); return -1; } // lengchck; byArySend[9] = 0x38; byArySend[10] = 0x30; byArySend[11] = 0x30; byArySend[12] = 0x38; // STX; byArySend[13] = 0x36; byArySend[14] = 0x34; byArySend[15] = 0x30; // 接口卡LINEID; byArySend[16] = nAddr -1 + 48; // 设置值; memcpy(byArySend + 17, szMsg, 4); GetCheckSum((char *)byArySend + 1, chChkSum, 20 ); memcpy(byArySend + 21, chChkSum, 4); // EOL; byArySend[25] = 0x0D; iSendLen = 26; LOG4C((LOG_NOTICE,"cmd-10=空调风量设置:%s",szMsg)); } else if ( (strcmp(szCmd, "cmd-11") == 0)) // 设置温度; { byArySend[9] = 0x36; byArySend[10] = 0x30; byArySend[11] = 0x30; byArySend[12] = 0x41; byArySend[13] = 0x36; byArySend[14] = 0x33; byArySend[15] = 0x30; byArySend[16] = nAddr -1 + 48; char szTemp[10] = {0}; int iRealLen = strlen(szMsg); memcpy(szTemp,szMsg,iRealLen); if (iRealLen > 6) { LOG4C((LOG_NOTICE,"非法指令长度:%s",szMsg)); return -1; } if (iRealLen == 5) { memcpy(szMsg + 1, szTemp,iRealLen); szMsg[0] = 48; }else if (iRealLen == 4) { memcpy(szMsg + 2, szTemp,iRealLen); szMsg[0] = 48; szMsg[1] = 48; } else if ( iRealLen == 3) { memcpy(szMsg + 3, szTemp,iRealLen); szMsg[0] = 48; szMsg[1] = 48; szMsg[2] = 48; } else if ( iRealLen == 2) { memcpy(szMsg + 4, szTemp,iRealLen); szMsg[0] = 48; szMsg[1] = 48; szMsg[2] = 48; szMsg[3] = 48; } else if ( iRealLen == 1) { memcpy(szMsg + 5, szTemp,iRealLen); szMsg[0] = 48; szMsg[1] = 48; szMsg[2] = 48; szMsg[3] = 48; szMsg[4] = 48; } else if ( iRealLen == 0) memcpy(szMsg , "000000", 6); memcpy(byArySend + 17, szMsg, 6); GetCheckSum((char *)byArySend + 1, chChkSum, 22 ); memcpy(byArySend + 23, chChkSum, 4); byArySend[27] = 0x0D; iSendLen = 28; LOG4C((LOG_NOTICE,"cmd-11=空调温度设置:%s",szMsg)); }else if ( strcmp(szCmd, "cmd-12") == 0) // 设置运转模式; { // lengchck; byArySend[9] = 0x42; byArySend[10] = 0x30; byArySend[11] = 0x30; byArySend[12] = 0x35; // STX; byArySend[13] = 0x36; byArySend[14] = 0x32; byArySend[15] = 0x30; // 接口卡LINEID; 范围:0~2 byArySend[16] = nAddr -1 + 48; // 设置值; 范围:0~7 memcpy(byArySend + 17, szMsg, 1); GetCheckSum((char *)byArySend + 1, chChkSum, 17 ); memcpy(byArySend + 18, chChkSum, 4); // EOL; byArySend[22] = 0x0D; iSendLen = 23; LOG4C((LOG_NOTICE,"cmd-12=空调运转模式设置:%s",szMsg)); } memcpy(szSendMsg, byArySend, sizeof(byArySend)); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; if( nAddr < 1 ) return -1; SetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType, iSendLen ); return 0; } //读取配置文件 int CDaiKin::GetFromIni( char szPath[MAX_PATH], //服务器程序所在目录 char szIniName[MAX_PATH], //配置文件名 char szCmd[MAX_CMD], //命令 char szCid2[CID2_LENGTH], //发送Buffer char szType[TYPE_LENGTH], int &nIndex, int &nLen, int &nStaBit, int &nEndBit ) { char szFile[MAX_PATH + 1] = ""; memset(szType, 0, TYPE_LENGTH); wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);//只读config下面的ini GetPrivateProfileString(szCmd, "SendCmd", "", szCid2, CID2_LENGTH, szFile);//读取配置文件中的一段字符串 GetPrivateProfileString(szCmd, "Type", "", szType, TYPE_LENGTH, szFile); nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);//从配置文件中取值 nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); nStaBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值 nEndBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile); return 0; } //int CDaiKin::RequestStatus( int nAddr, char *szCmd, CCommProcess *pComm,char *chSendMsg ) //{ // int nRet = -1; //#if DEBUG_DAIKIN // int iLen = sizeof(REQUEST_STRUCT); // char chLength[4] = {0}; // char chChkSum[5] = {0}; // // REQUEST_STRUCT RequestPara; // // memset( &RequestPara, 0, iLen ); // // //起始位 // RequestPara.headMes.Start = 0x7E; // // //通讯协议版本 // RequestPara.headMes.Version[0] = 0x32; // RequestPara.headMes.Version[1] = 0x30; // // //设备地址描述 // // RequestPara.headMes.Address[0] = ByteToAscii((nAddr>>4) & 0x0f); // RequestPara.headMes.Address[1] = ByteToAscii(nAddr & 0x0f); // // //Cid1 // RequestPara.headMes.Cid1[0] = 0x36; // RequestPara.headMes.Cid1[1] = 0x30; // // //Cid2 // RequestPara.headMes.Cid2[0] = ByteToAscii(chSendMsg[0] & 0x0f); // RequestPara.headMes.Cid2[1] = ByteToAscii(chSendMsg[1] & 0x0f); // // RequestPara.headMes.Lenth[0] = 0x30; // RequestPara.headMes.Lenth[1] = 0x30; // RequestPara.headMes.Lenth[2] = 0x30; // RequestPara.headMes.Lenth[3] = 0x30; // // //校验码 // BYTE *pDataBuf = new BYTE[ iLen - sizeof(RequestPara.chkSum) - 1 ]; // memset(pDataBuf, 0, iLen - sizeof(RequestPara.chkSum) - 1 ); // // memcpy(pDataBuf, &RequestPara, iLen - sizeof(RequestPara.chkSum) - 1 ); // // GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.chkSum) - 2 ); // // RequestPara.chkSum[0] = chChkSum[0]; // RequestPara.chkSum[1] = chChkSum[1]; // RequestPara.chkSum[2] = chChkSum[2]; // RequestPara.chkSum[3] = chChkSum[3]; // // //结束符 // RequestPara.End = 0x0D; // // if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 // { // int nDataLen = (int)sizeof(RequestPara); // ResetEvent( m_hSemComm ); // int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen); // // if( nResult == iLen ) // { // } // else // { // delete []pDataBuf; // SetEvent( m_hSemComm ); // return EER_CODE_DAIKIN_COM_WRITE_DATA; // } // } // else // { // delete []pDataBuf; // return ERR_CODE_DAIKIN_COM_BUSY; // } // // delete[]pDataBuf; // //#endif // return 0; //} int CDaiKin::RequestStatus( int nAddr, char *szCmd, CCommProcess *pComm,char *chSendMsg ) { int nRet = -1; #if DEBUG_DAIKIN char chChkSum[5] = {0}; BYTE byArySend[60] = {0}; // 电信头部; byArySend[0] = 0x7E; // 电信版本; byArySend[1] = 0x32; byArySend[2] = 0x30; // 地址; byArySend[3] = ByteToAscii((nAddr>>4) & 0x0f); byArySend[4] = ByteToAscii(nAddr & 0x0f); // Cid1; byArySend[5] = 0x36; byArySend[6] = 0x30; // Cid2; byArySend[7] = 0x45; byArySend[8] = 0x30; // LENGTH; byArySend[9] = 0x41; byArySend[10] = 0x30; byArySend[11] = 0x30; byArySend[12] = 0x36; // STX; byArySend[13] = 0x34; byArySend[14] = 0x30; byArySend[15] = 0x30; // LINE No; 范围:0~2 byArySend[16] = nAddr -1 + 48; // Unit No; byArySend[17] = 0x30; byArySend[18] = 0x30; // check; GetCheckSum((char*)byArySend + 1, chChkSum, 18); memcpy(byArySend + 19, chChkSum, 4); // End; byArySend[23] = 0x0D; int iSendLen = 24; if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 { ResetEvent( m_hSemComm ); int nResult = pComm->Write((BYTE *)&byArySend, iSendLen); if( nResult == iSendLen ) { SetEvent( m_hSemComm ); LOG4C((LOG_NOTICE,"实际写长度不等")); return EER_CODE_DAIKIN_COM_WRITE_DATA; } } else { LOG4C((LOG_NOTICE,"无信号写")); return ERR_CODE_DAIKIN_COM_BUSY; } #endif return 0; } int CDaiKin::ResponseStatus(int nAddr,CCommProcess *pComm,char *szCmd, char *szMsg, int &nIndex,int &nLen,char *szType) { #if DEBUG_DAIKIN int iSendLen = 90; int iRecvLen = 0; BYTE *byAryRecv = new BYTE[iSendLen]; ZeroMemory(byAryRecv,iSendLen); iRecvLen = pComm->Read(byAryRecv,iSendLen); if( iRecvLen <= 0) { SetEvent( m_hSemComm ); if( byAryRecv != NULL) { delete[]byAryRecv; byAryRecv = NULL; } LOG4C((LOG_NOTICE, "串口没有读到数据")); return ERR_CODE_DAIKIN_COM_READ_NO_DATA; } if (!ChkSumCheck( (char*)byAryRecv, iRecvLen)) { SetEvent(m_hSemComm); if (byAryRecv != NULL) { delete []byAryRecv; byAryRecv = NULL; } LOG4C((LOG_NOTICE, "校验码校验出错")); return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错 } if (RtnCheck( (char*)byAryRecv) != 0) { SetEvent(m_hSemComm); if (byAryRecv != NULL) { delete []byAryRecv; byAryRecv = NULL; } LOG4C((LOG_NOTICE, "RTN校验出错")); return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错 } memcpy(m_szDaKin_400Msg, byAryRecv, iRecvLen); m_dwOnlineTick[nAddr - 1] = GetTickCount(); SetEvent( m_hSemComm ); if( byAryRecv != NULL) { delete []byAryRecv; byAryRecv = NULL; } #else SimulationCommData(nAddr); #endif return 0; } //int CDaiKin::ResponseStatus(int nAddr,CCommProcess *pComm,char *szCmd, char *szMsg, int &nIndex,int &nLen,char *szType) //{ //#if DEBUG_DAIKIN // int nReceiveLen = 0; // int nProcessLen = 0; // int nReadLen = 0; // // RESPONSE_STRUCT structResponse; // memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) ); // // nReceiveLen = sizeof(RESPONSE_STRUCT); // // char *pBuffer = new char[ nReceiveLen ]; // memset(pBuffer, 0, nReceiveLen); // // // nReadLen = pComm->Read((BYTE *)pBuffer,nReceiveLen); ////LOG4C((LOG_NOTICE, " nLen= %d, nReadLen = %d", nLen, nReadLen)); // if( nReadLen <= 0) // { // // 串口没有读到数据 // //TRACE("串口没有读到数据!\r\n"); // SetEvent( m_hSemComm ); // // if( pBuffer != NULL) // { // delete[] pBuffer; // pBuffer = NULL; // } // // LOG4C((LOG_NOTICE, "串口没有读到数据")); // return ERR_CODE_DAIKIN_COM_READ_NO_DATA; // } // // if (!ChkSumCheck(pBuffer, nReadLen)) // { // TRACE("校验码校验出错"); // SetEvent(m_hSemComm); // // if (pBuffer != NULL) // { // delete [] pBuffer; // pBuffer = NULL; // } // // LOG4C((LOG_NOTICE, "校验码校验出错")); // return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错 // } // // // if (RtnCheck(pBuffer) != 0) // { // SetEvent(m_hSemComm); // TRACE("RTN校验出错"); // if (pBuffer != NULL) // { // delete [] pBuffer; // pBuffer = NULL; // } // // LOG4C((LOG_NOTICE, "RTN校验出错")); // return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错 // } // // SetDaKin_42VarMsg(nAddr, szCmd, pBuffer); // SetDaKin_43VarMsg(nAddr, szCmd, pBuffer); // SetDaKin_44VarMsg(nAddr, szCmd, pBuffer); // //SetDaKin_45VarMsg(nAddr, szCmd, pBuffer); // //SetDaKin_47VarMsg(nAddr, szCmd, pBuffer); // // m_dwOnlineTick[nAddr - 1] = GetTickCount(); // // // 设置串口等待事件为有信号 // SetEvent( m_hSemComm ); // // if( pBuffer != NULL) // { // delete[] pBuffer; // pBuffer = NULL; // } // //#else // SimulationCommData(nAddr); //#endif // return 0; //} int CDaiKin::WriteRequestStatus( int nAddr, char *szCmd, // 命令 CCommProcess *pComm, //串口对象指针 char *chSendMsg , //发送Buffer int &iSendLen ) { #if DEBUG_DAIKIN ResetEvent( m_hSemComm ); // 读取上次未读的数据,确保空调缓存已无数据; BYTE byRecv[250] = {0}; pComm->Read(byRecv, 250); Sleep(1000); int nResult = pComm->Write((unsigned char *)chSendMsg, iSendLen); LOG4C((LOG_NOTICE,"%s %d",chSendMsg,iSendLen)); if( nResult == iSendLen ) { } else { SetEvent( m_hSemComm ); return EER_CODE_DAIKIN_COM_WRITE_DATA; } Sleep(300); #endif return 0; } int CDaiKin::WriteResponseStatus( int nAddr, // 设备地址 CCommProcess *pComm, //串口对象指针 char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH] // 变量数据类型 ) { #if DEBUG_DAIKIN 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); LOG4C((LOG_NOTICE,"写 nReadLen = %d, nReceiveLen = %d",nReadLen, nReceiveLen)); if( nReadLen <= 0) { SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"写 串口没有读到数据")); return ERR_CODE_DAIKIN_COM_READ_NO_DATA; } #if 0 //int nTempLen = strlen(pBuffer); //CString str; //str.Format("%d", nTempLen); //AfxMessageBox(str); if (!ChkSumCheck(pBuffer, strlen(pBuffer))) { SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"写 校检错误")); return ERR_CODE_DAIKIN_COM_CHKSUM_LOST; } if (!CheckLength(pBuffer)) { SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"写 数据长度校检错误")); return ERR_CODE_DAIKIN_COM_VARLEN; } int nRet = RtnCheck(pBuffer); if(0 != nRet) { SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"写 返回码出错")); return nRet; } #endif // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(nAddr); #endif return 0; } void CDaiKin::SimulationCommData(int nAddr) { } WORD CDaiKin::RtnCheck(char Msg[VAR_MSG]) { int len ,index; len = 2; index = 7; char ch[2]; ch[0] = Msg[index]; ch[1] = Msg[index+1]; if (atoi(ch)==ERR_CID_RTN_VAR) { return ERR_CID_RTN_VAR; } else if (atoi(ch)==ERR_CID_RTN_CHKSUM) { return ERR_CID_RTN_CHKSUM; } else if (atoi(ch)==ERR_CID_RTN_LCHKSUM) { return ERR_CID_RTN_LCHKSUM; } else if (atoi(ch)==ERR_CID_RTN_CID2) { return ERR_CID_RTN_CID2; } else if (atoi(ch)==ERR_CID_RTN_FORMAT) { return ERR_CID_RTN_FORMAT; } else if (atoi(ch)==ERR_CID_RTN_INVALI_DATA) { return ERR_CID_RTN_INVALI_DATA; } else /*if (atoi(ch)==ERR_CID_RTN_NORMAL)*/ { return ERR_CID_RTN_NORMAL; } } void CDaiKin::lowcase2uppcase(BYTE &btSrc) { if( btSrc >= 'a' && btSrc <= 'z' ) { btSrc = btSrc - 'a' + 'A'; } } void CDaiKin::GetCheckSum(char *szData, char *szCheck ,int nlen /* = 12 */) { DWORD dwSum(0); nlen = strlen(szData); 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]); } } #if 0 UINT CDaiKin::GetCheckSum(char *pBuf, char chDest[5], int len) { WORD iSum = 0; //unsigned char chCompliment[2] = {0}; for(int i=0; i