// 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])); 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); 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::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 nRet = -1; nRet = WriteRequestStatus(nAddr, szCmd, pComm, szSendMsg); if( nRet != 0 ) { 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接口 ) { int nIndex = 0, nLen = 0, StaBit = 0, EndBit = 0; char szSendMsg[DAIKIN_SEND_MSG]; char szType[TYPE_LENGTH] = {0}; char szCid2[CID2_LENGTH] = {0}; char szAddr[2] = {0}; char szLength[4] = {0}; REQUESTSET_STRCUT szSend = {0}; memset(szSendMsg, 0, DAIKIN_SEND_MSG); GetFromIni(szPath, szIniName, szCmd, szCid2, szType, nIndex, nLen, StaBit, EndBit); int iLen = sizeof(REQUESTSET_STRCUT); char chLength[4] = {0}; char chChkSum[5] = {0}; REQUESTSET_STRCUT RequestPara; memset( &RequestPara, 0, iLen ); //起始位 RequestPara.Sol = 0x7E; //通讯协议版本 RequestPara.Ver[0] = 0x32; RequestPara.Ver[1] = 0x30; //设备地址描述 RequestPara.Adr[0] = ByteToAscii((nAddr>>4) & 0x0f); RequestPara.Adr[1] = ByteToAscii(nAddr & 0x0f); //Cid1 RequestPara.Cid1[0] = 0x36; RequestPara.Cid1[1] = 0x30; //Cid2 RequestPara.Cid2[0] = ByteToAscii(szCid2[0] & 0x0f); RequestPara.Cid2[1] = ByteToAscii(szCid2[1] & 0x0f); RequestPara.Length[0] = 0x45; RequestPara.Length[1] = 0x30; RequestPara.Length[2] = 0x30; RequestPara.Length[3] = 0x30; memcpy(RequestPara.szInfo, szMsg, sizeof(RequestPara.szInfo)); memset(RequestPara.Chksum, 0, sizeof(RequestPara.Chksum)); //校验码 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.Eol = 0x0D; memcpy(szSendMsg, &RequestPara, sizeof(REQUESTSET_STRCUT)); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if( nAddr < 1 ) return -1; //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szSendMsg= %s, cmd = %s", szSendMsg, szCmd)); if( (strcmp(szCmd, "cmd-9") == 0)) { SetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); } 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[MAX_CMD], // 命令 CCommProcess *pComm, //串口对象指针 char chSendMsg[DAIKIN_SEND_MSG] //发送Buffer ) { 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::ResponseStatus( 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 = 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, nLen); nReadLen = pComm->Read((BYTE *)pBuffer,nLen); if( nReadLen <= 0) { // 串口没有读到数据 TRACE("串口没有读到数据!\r\n"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DAIKIN_COM_READ_NO_DATA; } else if( nReadLen < nLen ) { TRACE("长度没有收够,断续接收,止到收完为止!\r\n"); SetEvent( m_hSemComm ); if( pBuffer ) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DAIKIN_COM_FAULT; } else if( nReadLen > nLen ) { // 完全代码,不一定能执行到 TRACE("接收的长度超时所需的长度,认为是非法包,扔掉\r\n"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DAIKIN_COM_READ_LEN_OVER; } if (!ChkSumCheck(pBuffer, nReadLen)) { TRACE("校验码校验出错"); SetEvent(m_hSemComm); if (pBuffer != NULL) { delete [] pBuffer; pBuffer = NULL; } return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错 } if (RtnCheck(pBuffer) != 0) { SetEvent(m_hSemComm); TRACE("RTN校验出错"); if (pBuffer != NULL) { delete [] pBuffer; pBuffer = NULL; } 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[MAX_CMD], // 命令 CCommProcess *pComm, //串口对象指针 char chSendMsg[DAIKIN_SEND_MSG] //发送Buffer ) { #if DEBUG_DAIKIN int nDataLen = (int)strlen(chSendMsg); ResetEvent( m_hSemComm ); int nResult = pComm->Write((unsigned char *)chSendMsg, nDataLen); if( nResult == nDataLen ) { } else { SetEvent( m_hSemComm ); return EER_CODE_DAIKIN_COM_WRITE_DATA; } #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 = 0; int nProcessLen = 0; int nReadLen = 0; RESPONSE_STRUCT structResponse; memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) ); nReceiveLen = sizeof(RESPONSE_STRUCT) - sizeof(structResponse); char *pBuffer = new char[ nReceiveLen ]; memset(pBuffer, 0, nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen); if( nReadLen <= 0) { // 串口没有读到数据 //TRACE("串口没有读到数据!\r\n"); //AfxMessageBox("串口没有读到数据!"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } 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))) { // 校检错误 //AfxMessageBox("校检错误"); //TRACE("校检错误!\r\n"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DAIKIN_COM_CHKSUM_LOST; } if (!CheckLength(pBuffer)) { // 数据长度校检错误 //TRACE("数据长度校检错误!\r\n"); //AfxMessageBox("数据长度校检错误!"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DAIKIN_COM_VARLEN; } int nRet = RtnCheck(pBuffer); if(0 != nRet) { //AfxMessageBox("校验出错"); // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return nRet; } #endif // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(nAddr); #endif return 0; } void CDaiKin::SimulationCommData(int nAddr) { m_szDaKin_42Msg[nAddr - 1][0] = 0x7E; m_szDaKin_42Msg[nAddr - 1][1] = 0x32; m_szDaKin_42Msg[nAddr - 1][2] = 0x30; m_szDaKin_42Msg[nAddr - 1][3] = 0x30; m_szDaKin_42Msg[nAddr - 1][4] = 0x31; m_szDaKin_42Msg[nAddr - 1][5] = 0x36; m_szDaKin_42Msg[nAddr - 1][6] = 0x30; m_szDaKin_42Msg[nAddr - 1][7] = 0x30; m_szDaKin_42Msg[nAddr - 1][8] = 0x30; m_szDaKin_42Msg[nAddr - 1][9] = 0x39; m_szDaKin_42Msg[nAddr - 1][10] = 0x30; m_szDaKin_42Msg[nAddr - 1][11] = 0x33; m_szDaKin_42Msg[nAddr - 1][12] = 0x34; m_szDaKin_42Msg[nAddr - 1][13] = 0x30; m_szDaKin_42Msg[nAddr - 1][14] = 0x30; m_szDaKin_42Msg[nAddr - 1][15] = 0x20; m_szDaKin_42Msg[nAddr - 1][16] = 0x20; m_szDaKin_42Msg[nAddr - 1][17] = 0x20; m_szDaKin_42Msg[nAddr - 1][18] = 0x20; m_szDaKin_42Msg[nAddr - 1][19] = 0x20; m_szDaKin_42Msg[nAddr - 1][20] = 0x20; m_szDaKin_42Msg[nAddr - 1][21] = 0x20; m_szDaKin_42Msg[nAddr - 1][22] = 0x20; m_szDaKin_42Msg[nAddr - 1][23] = 0x20; m_szDaKin_42Msg[nAddr - 1][24] = 0x20; m_szDaKin_42Msg[nAddr - 1][25] = 0x20; m_szDaKin_42Msg[nAddr - 1][26] = 0x20; m_szDaKin_42Msg[nAddr - 1][27] = 0x20; m_szDaKin_42Msg[nAddr - 1][28] = 0x20; m_szDaKin_42Msg[nAddr - 1][29] = 0x20; m_szDaKin_42Msg[nAddr - 1][30] = 0x20; m_szDaKin_42Msg[nAddr - 1][31] = 0x20; m_szDaKin_42Msg[nAddr - 1][32] = 0x20; m_szDaKin_42Msg[nAddr - 1][33] = 0x20; m_szDaKin_42Msg[nAddr - 1][34] = 0x20; m_szDaKin_42Msg[nAddr - 1][35] = 0x20; m_szDaKin_42Msg[nAddr - 1][36] = 0x20; m_szDaKin_42Msg[nAddr - 1][37] = 0x20; m_szDaKin_42Msg[nAddr - 1][38] = 0x20; m_szDaKin_42Msg[nAddr - 1][39] = 0x20; m_szDaKin_42Msg[nAddr - 1][40] = 0x20; m_szDaKin_42Msg[nAddr - 1][41] = 0x20; m_szDaKin_42Msg[nAddr - 1][42] = 0x20; m_szDaKin_42Msg[nAddr - 1][43] = 0x30; m_szDaKin_42Msg[nAddr - 1][44] = 0x38; m_szDaKin_42Msg[nAddr - 1][45] = 0x44; m_szDaKin_42Msg[nAddr - 1][46] = 0x31; m_szDaKin_42Msg[nAddr - 1][47] = 0x20; m_szDaKin_42Msg[nAddr - 1][48] = 0x20; m_szDaKin_42Msg[nAddr - 1][49] = 0x20; m_szDaKin_42Msg[nAddr - 1][50] = 0x20; m_szDaKin_42Msg[nAddr - 1][51] = 0x20; m_szDaKin_42Msg[nAddr - 1][52] = 0x20; m_szDaKin_42Msg[nAddr - 1][53] = 0x20; m_szDaKin_42Msg[nAddr - 1][54] = 0x20; m_szDaKin_42Msg[nAddr - 1][55] = 0x20; m_szDaKin_42Msg[nAddr - 1][56] = 0x20; m_szDaKin_42Msg[nAddr - 1][57] = 0x20; m_szDaKin_42Msg[nAddr - 1][58] = 0x20; m_szDaKin_42Msg[nAddr - 1][59] = 0x20; m_szDaKin_42Msg[nAddr - 1][60] = 0x20; m_szDaKin_42Msg[nAddr - 1][61] = 0x20; m_szDaKin_42Msg[nAddr - 1][62] = 0x20; m_szDaKin_42Msg[nAddr - 1][63] = 0x30; m_szDaKin_42Msg[nAddr - 1][64] = 0x30; m_szDaKin_42Msg[nAddr - 1][65] = 0x46; m_szDaKin_42Msg[nAddr - 1][66] = 0x36; m_szDaKin_42Msg[nAddr - 1][67] = 0x38; m_szDaKin_42Msg[nAddr - 1][68] = 0x41; m_szDaKin_42Msg[nAddr - 1][69] = 0x0D; m_szDaKin_43Msg[nAddr - 1][0] = 0x7E; m_szDaKin_43Msg[nAddr - 1][1] = 0x32; m_szDaKin_43Msg[nAddr - 1][2] = 0x30; m_szDaKin_43Msg[nAddr - 1][3] = 0x30; m_szDaKin_43Msg[nAddr - 1][4] = 0x31; m_szDaKin_43Msg[nAddr - 1][5] = 0x36; m_szDaKin_43Msg[nAddr - 1][6] = 0x30; m_szDaKin_43Msg[nAddr - 1][7] = 0x30; m_szDaKin_43Msg[nAddr - 1][8] = 0x30; m_szDaKin_43Msg[nAddr - 1][9] = 0x37; m_szDaKin_43Msg[nAddr - 1][10] = 0x30; m_szDaKin_43Msg[nAddr - 1][11] = 0x31; m_szDaKin_43Msg[nAddr - 1][12] = 0x38; m_szDaKin_43Msg[nAddr - 1][13] = 0x30; m_szDaKin_43Msg[nAddr - 1][14] = 0x30; m_szDaKin_43Msg[nAddr - 1][15] = 0x30; m_szDaKin_43Msg[nAddr - 1][16] = 0x31; m_szDaKin_43Msg[nAddr - 1][17] = 0x30; m_szDaKin_43Msg[nAddr - 1][18] = 0x39; m_szDaKin_43Msg[nAddr - 1][19] = 0x30; m_szDaKin_43Msg[nAddr - 1][20] = 0x30; m_szDaKin_43Msg[nAddr - 1][21] = 0x30; m_szDaKin_43Msg[nAddr - 1][22] = 0x30; m_szDaKin_43Msg[nAddr - 1][23] = 0x30; m_szDaKin_43Msg[nAddr - 1][24] = 0x31; m_szDaKin_43Msg[nAddr - 1][25] = 0x30; m_szDaKin_43Msg[nAddr - 1][26] = 0x31; m_szDaKin_43Msg[nAddr - 1][27] = 0x30; m_szDaKin_43Msg[nAddr - 1][28] = 0x30; m_szDaKin_43Msg[nAddr - 1][29] = 0x30; m_szDaKin_43Msg[nAddr - 1][30] = 0x30; m_szDaKin_43Msg[nAddr - 1][31] = 0x30; m_szDaKin_43Msg[nAddr - 1][32] = 0x30; m_szDaKin_43Msg[nAddr - 1][33] = 0x30; m_szDaKin_43Msg[nAddr - 1][34] = 0x30; m_szDaKin_43Msg[nAddr - 1][35] = 0x30; m_szDaKin_43Msg[nAddr - 1][36] = 0x32; m_szDaKin_43Msg[nAddr - 1][37] = 0x46; m_szDaKin_43Msg[nAddr - 1][38] = 0x39; m_szDaKin_43Msg[nAddr - 1][39] = 0x31; m_szDaKin_43Msg[nAddr - 1][40] = 0x38; m_szDaKin_43Msg[nAddr - 1][41] = 0x0D; m_szDaKin_44Msg[nAddr - 1][0] = 0x7E; m_szDaKin_44Msg[nAddr - 1][1] = 0x32; m_szDaKin_44Msg[nAddr - 1][2] = 0x30; m_szDaKin_44Msg[nAddr - 1][3] = 0x30; m_szDaKin_44Msg[nAddr - 1][4] = 0x31; m_szDaKin_44Msg[nAddr - 1][5] = 0x36; m_szDaKin_44Msg[nAddr - 1][6] = 0x30; m_szDaKin_44Msg[nAddr - 1][7] = 0x30; m_szDaKin_44Msg[nAddr - 1][8] = 0x30; m_szDaKin_44Msg[nAddr - 1][9] = 0x33; m_szDaKin_44Msg[nAddr - 1][10] = 0x30; m_szDaKin_44Msg[nAddr - 1][11] = 0x31; m_szDaKin_44Msg[nAddr - 1][12] = 0x43; m_szDaKin_44Msg[nAddr - 1][13] = 0x30; m_szDaKin_44Msg[nAddr - 1][14] = 0x30; m_szDaKin_44Msg[nAddr - 1][15] = 0x20; m_szDaKin_44Msg[nAddr - 1][16] = 0x20; m_szDaKin_44Msg[nAddr - 1][17] = 0x20; m_szDaKin_44Msg[nAddr - 1][18] = 0x20; m_szDaKin_44Msg[nAddr - 1][19] = 0x20; m_szDaKin_44Msg[nAddr - 1][20] = 0x20; m_szDaKin_44Msg[nAddr - 1][21] = 0x20; m_szDaKin_44Msg[nAddr - 1][22] = 0x20; m_szDaKin_44Msg[nAddr - 1][23] = 0x20; m_szDaKin_44Msg[nAddr - 1][24] = 0x20; m_szDaKin_44Msg[nAddr - 1][25] = 0x20; m_szDaKin_44Msg[nAddr - 1][26] = 0x20; m_szDaKin_44Msg[nAddr - 1][27] = 0x30; m_szDaKin_44Msg[nAddr - 1][28] = 0x32; m_szDaKin_44Msg[nAddr - 1][29] = 0x20; m_szDaKin_44Msg[nAddr - 1][30] = 0x20; m_szDaKin_44Msg[nAddr - 1][31] = 0x20; m_szDaKin_44Msg[nAddr - 1][32] = 0x20; m_szDaKin_44Msg[nAddr - 1][33] = 0x20; m_szDaKin_44Msg[nAddr - 1][34] = 0x20; m_szDaKin_44Msg[nAddr - 1][35] = 0x20; m_szDaKin_44Msg[nAddr - 1][36] = 0x30; m_szDaKin_44Msg[nAddr - 1][37] = 0x31; m_szDaKin_44Msg[nAddr - 1][38] = 0x30; m_szDaKin_44Msg[nAddr - 1][39] = 0x30; m_szDaKin_44Msg[nAddr - 1][40] = 0x30; m_szDaKin_44Msg[nAddr - 1][41] = 0x46; m_szDaKin_44Msg[nAddr - 1][42] = 0x39; m_szDaKin_44Msg[nAddr - 1][43] = 0x39; m_szDaKin_44Msg[nAddr - 1][44] = 0x44; m_szDaKin_44Msg[nAddr - 1][45] = 0x0D; } 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; } } UINT CDaiKin::GetCheckSum(char *pBuf, char chDest[5], int len) { WORD iSum = 0; //unsigned char chCompliment[2] = {0}; for(int i=0; i