// Stulz6000.cpp: implementation of the CStulz6000 class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Stulz6000.h" #include "CommProcess.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif CStulz6000::CStulz6000(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 < MAX_ADDR; i++ ) { memset(m_szStulz6000_12Msg[i], 0, sizeof(m_szStulz6000_12Msg[i])); m_devOnline[i] = TRUE; m_dwOnlineTick[i] = 0; } } CStulz6000::~CStulz6000() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL CStulz6000::Stulz6000OpenComm(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 CStulz6000::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, "StaBit", 0, szFile);//从配置文件中取值 iEBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile); return 0; } // 发送读取设备参数请求 int CStulz6000::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); if( ( strlen(m_szStulz6000_12Msg[nAddr-1]) == 0 && strcmp(IniSendCMD,"12")==0 ) || strcmp(szCmd, "cmd-1") == 0) { 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 ) { LOG4C((LOG_NOTICE,"Stulz->SendReadRequest::返回超时")); return -1; } nRet = GetStulz6000_12VarMsg(nAddr,szCmd,IniSendCMD,szMsg,nIndex,nLen,szDataType,iSBit,iEBit); LOG4C((LOG_NOTICE,"Stulz-> Port = %d, Addr = %d, %s = %s",nCommPort, nAddr, szCmd, szMsg)); return nRet; } int CStulz6000::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,IniSendCMD); return nRet; } int CStulz6000::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen) { #if DEBUG_Stulz6000 int nRet = -1; RESPONSE_STRUCT szSend = {0}; //设备地址描述; szSend.Adr = nAddr; //cid2 szSend.Cid2[0] = IniSendCMD[0] - '0'; szSend.Cid2[1] = IniSendCMD[1] - '0'; //chksum; szSend.Chksum[0] = ~(szSend.Adr + szSend.Cid2[0] + szSend.Cid2[1]) + 1; szSend.Chksum[1] = 255; if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) { int nDataLen = (int)sizeof(szSend); ResetEvent( m_hSemComm ); int nResult = pComm->Write((unsigned char *)&szSend, nDataLen); if( nResult == nDataLen ) { nRet = 0; } else { LOG4C((LOG_NOTICE,"Stulz->Send_ReadDeviceData::Wirte Error")); SetEvent( m_hSemComm ); return ERR_CODE_COM_INVALIDRES; } } else { LOG4C((LOG_NOTICE,"Stulz->Send_ReadDeviceData::Thread busy")); return ERR_CODE_COM_BUSY; } return nRet; #endif return 0; } int CStulz6000::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd,char *szSendMsg) { #if DEBUG_Stulz6000 int nReceiveLen = 0,nReadLen = 0; nReceiveLen = Stulz6000_RECEIVE_MSG; 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; } LOG4C((LOG_NOTICE,"Stulz->Recv_ReadDeviceData::No data to read")); return ERR_CODE_COM_READ_NO_DATA; } SetStulz6000_12VarMsg(nAddr, szCmd,szSendMsg,pBuffer); m_dwOnlineTick[nAddr - 1] = GetTickCount(); // 设置串口等待事件为有信号; SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(nAddr); #endif return 0; } void CStulz6000::SetStulz6000_12VarMsg( int nAddr, char *szCmd, char *szSendCMD, char *pBuffer) { if( strcmp(szSendCMD, "12") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szStulz6000_12Msg[nAddr - 1], pBuffer, sizeof(m_szStulz6000_12Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int CStulz6000::GetStulz6000_12VarMsg( int nAddr, char *szCmd, char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if( strcmp(szSendCMD, "12") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif if ( strcmp(szType,"DWORD") == 0) { //WORD dvl = ReturnNASCIIWORD( (BYTE*)(m_szStulz6000_12Msg[nAddr - 1] + nIndex)); sprintf(szMsg,"%d",ReturnNASCIIWORD((BYTE*)(m_szStulz6000_12Msg[nAddr - 1] + nIndex))); } else { DataConversion(szType, m_szStulz6000_12Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit); } #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } // 发送设置设备参数请求 int CStulz6000::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}; if( strcmp(IniSendCMD, "73") == 0 ) { LOG4C((LOG_NOTICE,"Stulz->Control ON/OFF, %s",szSetMsg)); SetDeviceParam(nAddr,pComm,szCmd,szSetMsg,szRecvMsg,IniSendCMD,IniSendlen); } return 0; } int CStulz6000::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 CStulz6000::Send_WriteDeviceData(int naddr, CCommProcess *pComm, char *szCmd, char *szSetMsg, char *IniSendCMD, const int &IniSendlen ) { #if DEBUG_Stulz6000 REQUEST_STRUCT szSend = {0}; //设备地址描述 szSend.Adr = naddr; //cid2; szSend.Cid2[0] = IniSendCMD[0] - '0'; szSend.Cid2[1] = IniSendCMD[1] - '0'; //szInfo; szSend.szInfo = atoi(szSetMsg); //chksum; szSend.Chksum[0] = ~(szSend.Adr + szSend.Cid2[0] + szSend.Cid2[1] + szSend.szInfo) + 1; szSend.Chksum[1] = 255; int nDataLen = (int)sizeof(REQUEST_STRUCT); ResetEvent( m_hSemComm ); int nResult = pComm->Write((unsigned char *)&szSend, nDataLen); if( nResult == nDataLen ) { } else { SetEvent( m_hSemComm ); LOG4C((LOG_NOTICE,"Stulz->Send_WriteDeviceData::Write Error")); return EER_CODE_COM_WRITE_DATA; } #endif return 0; } int CStulz6000::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg ) { #if DEBUG_Stulz6000 int nReceiveLen = 0; int nReadLen = 0; nReceiveLen = Stulz6000_RECEIVE_MSG; 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; } LOG4C((LOG_NOTICE,"Stulz->Recv_WriteDeviceData::No data to read")); return ERR_CODE_COM_READ_NO_DATA; } // 设置串口等待事件为有信号; SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(nAddr); #endif return 0; } void CStulz6000::SimulationCommData(int nAddr) { //CString str = "01 01 89 FF FF E0 00 00 00 0A 02 00 00 FF FF FF FF 00 00 00 00 00 00 00 BA 52 78 48 78 48 78 48 78 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 73 32 0D 01 11 0C 01 23 23 05 05 2D EC 5A 5A 0A 05 07 07 00 05 01 07 03 17 01 06 0F 05 14 05 0A 05 0F 05 0A 05 0A 05 08 05 0B 07 04 05 01 14 05 19 05 0F 05 0F 0A 0A 05 0F 07 08 05 01 19 05 1E 05 14 05 14 0A 0A 05 13 07 0C 05 01 1E 05 23 05 19 05 19 0A 0A 05 0F 00 00 00 07 EF"; //""; CString str = ""; CString str1 = "01 01 89 "; CString str2 = "FF FF E0 00 00 00 0A 02 00 00 FF FF FF FF "; CString str3 = "00 00 "; // 温湿度设置偏移; CString str4 = "00 00 00 "; //预留3字节; CString str5 = "00 00 "; //模块压缩机2位状态 CString str6 = "BA "; // 版本V1.86; CString str7 = "52 78 48 78 48 78 48 78 00 00 00 00 00 00 00 00 ";//模块输入输出状态; CString str8 = "00 00 00 00 00 00 00 73 32 "; CString str9 = "0D 01 11 0C 01 "; //年月日时分; CString strA = "23 23 05 05 2D EC 5A 5A 0A 05 "; // 温湿度报警 CString strB = "07 07 00 05 01 07 03 17 01 06 0F 05 14 05 0A 05 0F 05 0A 05 "; // 模块1信息; CString strC = "0A 05 08 05 0B 07 04 05 01 14 05 19 05 0F 05 0F 0A 0A 05 0F 07 08 05 01 19 05 1E 05 14 05 14 0A 0A 05 13 07 0C 05 01 1E 05 23 05 19 05 19 0A 0A 05 ";//模块2~4信息; CString strD = "1F 00 AA 55 07 EF"; // 状态1/2 + 错误码1/2 + 校验1/2; str = str1 + str2 +str3 +str4 +str5 +str6 +str7 +str8 +str9 +strA+ strB + strC +strD; int nLen = str.GetLength(); for ( int i = 0,j =0; i < nLen; i = i+3) { m_szStulz6000_12Msg[nAddr -1][j++] = ASCII_to_Byte(str.GetAt(i)) * 16 + ASCII_to_Byte(str.GetAt(i+1)); } }