#include "stdafx.h" #include "SanTak.h" #include "CommProcess.h" CSanTak::CSanTak(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)); memset(m_szSanTak_3C_Q1Msg, 0, sizeof(m_szSanTak_3C_Q1Msg)); memset(m_szSanTak_3C_Q4Msg, 0, sizeof(m_szSanTak_3C_Q4Msg)); m_dwOnlineTick = 0; m_devOnline = TRUE; } CSanTak::~CSanTak() { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL CSanTak::UpsSanTakOpenComm(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 ); //LOG4C((LOG_NOTICE, "OpenComm = %d",bResult)); return bResult; } int CSanTak::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", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的; GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile); szDataType[strlen(szDataType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); iSBit = GetPrivateProfileInt(szCmd, "iSBit", 0, szFile);//从配置文件中取值 iEBit = GetPrivateProfileInt(szCmd, "iEBit", 0, szFile); //LOG4C((LOG_NOTICE, "Send Command = %s,%d",IniSendCMD,IniSendlen)); return 0; } int CSanTak::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接口 ) { 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); //LOG4C((LOG_NOTICE, "Send Command")); int iCmd = atoi(szCmd + 4); if( (strlen(m_szSanTak_3C_Q1Msg) == 0 && (iCmd >= 1 && iCmd <= 13)) || iCmd == 1 || (strlen(m_szSanTak_3C_Q4Msg) == 0 && (iCmd >= 14 && iCmd <= 19)) || iCmd == 14 ) { nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen ); if ( nRet != 0) return nRet; } if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 ) m_devOnline = FALSE; else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 ) m_devOnline = TRUE; if( m_devOnline == FALSE ) return -1; nRet = GetSanTak_3C_Q1Msg( nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit); nRet = GetSanTak_3C_Q4Msg( nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit); //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szCmd= %s, szMsg = %s", szCmd, szMsg)); return nRet; } int CSanTak::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 CSanTak::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, char *IniSendCMD,const int &IniSendlen) { #if DEBUG_SANTAK //开关量,用于测试模拟数据 //if ( strcmp(IniSendCMD,"Q1") == 0) //{ // IniSendCMD[0] = 0x51; // IniSendCMD[1] = 0x31; // //IniSendCMD[2] = 0x0D; // LOG4C((LOG_NOTICE, "Send_ReadDeviceData = %s",IniSendCMD)); //} //else //{ // IniSendCMD[0] = 0x51; // IniSendCMD[1] = 0x34; // //IniSendCMD[2] = 0x0D; // LOG4C((LOG_NOTICE, "Send_ReadDeviceData = %s",IniSendCMD)); //} IniSendCMD[2] = 0x0D; if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write((BYTE *)IniSendCMD, IniSendlen+1); if (nResult != (IniSendlen+1)) { SetEvent(m_hSemComm); return EER_CODE_COM_REGNUM; } } else { return ERR_CODE_COM_BUSY; //串口忙 } #endif return 0; } int CSanTak::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd) { #if DEBUG_SANTAK int nReadLen = 0; RESPONSE_STRUCT strcutResponse; memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT)); int nReceiveLen = sizeof(RESPONSE_STRUCT); char *pBuff = new char[nReceiveLen]; ZeroMemory(pBuff,nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen); if (nReadLen <= 0) { SetEvent(m_hSemComm); if (pBuff != NULL) { delete [] pBuff; pBuff = NULL; } return ERR_CODE_COM_READ_NO_DATA; } //长度校验出错 SetSanTakQ1Msg(nAddr, szCmd, pBuff); SetSanTakQ4Msg(nAddr, szCmd, pBuff); m_dwOnlineTick = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } int CSanTak::GetSanTak_3C_Q1Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 1 && iCmd <= 13 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szDataType, m_szSanTak_3C_Q1Msg + nIndex, szRecvMsg, nLen, iSBit, iEBit); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } return 0; } int CSanTak::GetSanTak_3C_Q4Msg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 14 && iCmd <= 19) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif DataConversion(szDataType, m_szSanTak_3C_Q4Msg + nIndex, szRecvMsg, nLen, iSBit, iEBit); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } return 0; } void CSanTak::SetSanTakQ1Msg( int nAddr,char *szCmd, char *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 1 && iCmd <= 13 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szSanTak_3C_Q1Msg, pBuffer, sizeof(m_szSanTak_3C_Q1Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CSanTak::SetSanTakQ4Msg( int nAddr,char *szCmd, char *pBuffer) { int iCmd = atoi(szCmd + 4); if ( iCmd >= 14 && iCmd <= 19) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_szSanTak_3C_Q4Msg,pBuffer,sizeof(m_szSanTak_3C_Q4Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int CSanTak::SendSetReuest( char *szPath, // 程序所在路径 char *szIniName, // 配置文件名称 int nCommPort, // 串行端口 int nAddr, // 设备地址 char *szCmd, // 请求命令 char *szSetMsg, // 响应的值 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, // 预留字符数组参数1接口 char *szReversed2, // 预留字符数组参数2接口 char *szReversed3, // 预留字符数组参数3接口 char *szReversed4, // 预留字符数组参数4接口 char *szReversed5 // 预留字符数组参数5接口 // 预留字符数组参数5接口 ) { 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); if( nAddr < 1 ) return -1; if( strcmp(szCmd, "cmd-20") == 0 ) { char szRecvMsg[10] = {0}; SetDeviceParam( nAddr, pComm, szCmd, szSetMsg, szRecvMsg, IniSendCMD, IniSendlen ); } return 0; } int CSanTak::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 ) { LOG4C((LOG_NOTICE,"Write Error")); return nRet; // 串口忙 } nRet = Recv_WriteDeviceData(nAddr, pComm, szCmd, szRecvMsg); return nRet; } int CSanTak::Send_WriteDeviceData(int naddr, CCommProcess *pComm, char *szCmd, char *szSetMsg, char *IniSendCMD, const int &IniSendlen ) { #if DEBUG_DAIKIN ResetEvent( m_hSemComm ); // 读取上次未读的数据,确保空调缓存已无数据; BYTE byRecv[250] = {0}; pComm->Read(byRecv, 250); Sleep(1000); int nResult = pComm->Write(IniSendCMD, IniSendlen); if( nResult != IniSendlen ) { SetEvent( m_hSemComm ); return EER_CODE_COM_WRITE_DATA; } Sleep(300); #endif return 0; } int CSanTak::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg ) { int nReceiveLen = 0; int nProcessLen = 0; int nReadLen = 0; char *pBuffer = new char[ nReceiveLen ]; memset(pBuffer, 0, nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen); if( nReadLen <= 0) { SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_COM_READ_NO_DATA; } return 0; } void CSanTak::SimulationCommData(void) { }