#include "StdAfx.h" #include ".\leicai.h" leicai::leicai(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)); for (int i = 0 ; i < MAX_ADDR; i++) { memset(m_sz_Msg[i], 0, sizeof(m_sz_Msg[i])); m_devOnline[i] = TRUE; m_dwOnlineTick[i] = 0; } } leicai::~leicai(void) { #if IS_USE_READMSG_CS DeleteCriticalSection(& m_csReadMsg); #endif MTVERIFY(CloseHandle(m_hSemComm)); CloseComm(); } BOOL leicai::leicaiOpenComm(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 leicai::SendReadRequest( char *szPath, // 程序所在路径 char *szIniName, // 配置文件名称 int nCommPort, // 串行端口 int nAddr, // 设备地址 char *szCmd, // 请求命令 char *szMsg, // 响应的值 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接口 ) { 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_sz_Msg[nAddr -1]) == 0 && strcmp(IniSendCMD,"G1") == 0 ) || strcmp(szCmd, "cmd-1") == 0 ) { nRet = GetDeviceParam(nAddr,pComm,IniSendCMD,IniSendlen); if( nRet != 0 ) { LOG4C((LOG_NOTICE,"收发数据失败")); 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 = Getleicai_Msg(nAddr,IniSendCMD, szMsg, nIndex, nLen,szDataType,iSBit,iEBit ); LOG4C((LOG_NOTICE, "COM%d,Addr=%d, %s = %s",nCommPort,nAddr,szCmd,szMsg)); return nRet; } int leicai::GetDeviceParam( int nAddr,CCommProcess *pComm, char *IniSendCMD,const int &IniSendlen) { int nRet = -1; nRet = Send_ReadDeviceData(nAddr,pComm,IniSendCMD,IniSendlen); if( nRet != 0 ) return nRet; nRet = Recv_ReadDeviceData(nAddr,pComm,IniSendCMD); return nRet; } // IniSendCMD 2个byte; int leicai::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,const char *IniSendCMD,const int &IniSendlen) { #if DEBUG_LEICAI BYTE byArySend[9] = {0}; // Addr; byArySend[0] = nAddr; // fuc; byArySend[1] = 0x04; // Ver; byArySend[2] = 0x00; byArySend[3] = 0x00; byArySend[4] = 0x00; byArySend[5] = 0x03; int iCrc = GetModbus16CRC( byArySend, 6); BYTE high = iCrc>>8&0xff; BYTE low = iCrc&0xff; byArySend[6] = low; byArySend[7] = high; int nDatalen = 8; if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write(byArySend, nDatalen); if (nResult != nDatalen) { SetEvent(m_hSemComm); LOG4C((LOG_NOTICE,"实际写长度不一致")); return EER_CODE_LEICAI_COM_REGNUM; } } else { LOG4C((LOG_NOTICE,"线程信号外")); return ERR_CODE_LEICAI_COM_BUSY; } #endif return 0; } int leicai::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szSendCMD) { #if DEBUG_LEICAI int nReceiveLen = 260; int nReadLen = 0; char *pBuff = new char[nReceiveLen]; memset(pBuff, 0 , nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen); if (nReadLen <= 0) { SetEvent(m_hSemComm); if (pBuff != NULL) { delete [] pBuff; pBuff = NULL; } LOG4C((LOG_NOTICE,"接收数据长度 = 0")); return ERR_CODE_LEICAI_COM_READ_NO_DATA; } //长度校验出错 ; Setleicai_Msg(nAddr, szSendCMD, pBuff); m_dwOnlineTick[nAddr -1] = GetTickCount(); SetEvent( m_hSemComm ); if( pBuff != NULL) { delete[] pBuff; pBuff = NULL; } #else SimulationCommData(); #endif return 0; } void leicai::Setleicai_Msg( int nAddr, char *szSendCMD,char *pBuffer) { if ( strcmp(szSendCMD,"G1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif memcpy(m_sz_Msg[nAddr -1], pBuffer, sizeof(m_sz_Msg[nAddr -1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } int leicai::Getleicai_Msg( int nAddr,char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit ) { int nRet = 0; if ( strcmp(szSendCMD,"G1") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection(&m_csReadMsg); #endif _BYTE_2_Decimal(szMsg, (BYTE*)m_sz_Msg[nAddr -1]+nIndex); //DataConversion(szType, m_sz_Msg + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } void leicai::GetSystemTime(BYTE *szSystemTime) { // 获取当地时间; SYSTEMTIME systm; ::GetLocalTime(&systm); szSystemTime[0] = digit_to_hex(systm.wYear & 0x0F); // 年; szSystemTime[1] = digit_to_hex(systm.wMonth); // 月; szSystemTime[2] = digit_to_hex(systm.wDay); // 日; szSystemTime[3] = digit_to_hex(systm.wDayOfWeek); // 星; if ( systm.wSecond >= 54) { if ( systm.wMinute == 59) { szSystemTime[4] = digit_to_hex(systm.wHour+1); // 时; szSystemTime[5] = digit_to_hex(0); // 分; szSystemTime[6] = digit_to_hex(0); // 秒; } else { szSystemTime[4] = digit_to_hex(systm.wHour); szSystemTime[5] = digit_to_hex(systm.wMinute + 1); szSystemTime[6] = digit_to_hex(0); } } else { szSystemTime[4] = digit_to_hex(systm.wHour); szSystemTime[5] = digit_to_hex(systm.wMinute); szSystemTime[6] = digit_to_hex(systm.wSecond + 4); } } // 发送设置设备参数请求 int leicai::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 ) { if ( strcmp(szCmd,"cmd-4") != 0) return -1; 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 leicai::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 leicai::Send_WriteDeviceData( int nAddr,CCommProcess *pComm, char *szCmd, char *szSetMsg,char *IniSendCMD, const int &IniSendlen) { #if DEBUG_LEICAI BYTE byArySend[9] = {0}; // Addr; byArySend[0] = nAddr; // fuc; byArySend[1] = 0x06; // Ver; byArySend[2] = 0x00; byArySend[3] = 0x01; GetSystemTime(byArySend+4); int iCrc = GetModbus16CRC( byArySend, 11); BYTE high = iCrc>>8&0xff; BYTE low = iCrc&0xff; byArySend[11] = low; byArySend[12] = high; int iSendLen = 13; /************************************************************************/ ResetEvent( m_hSemComm ); // 读取上次未读的数据,确保空调缓存已无数据; BYTE byRecv[250] = {0}; pComm->Read(byRecv, 250); Sleep(500); int nResult = pComm->Write(byArySend, iSendLen); if( nResult != iSendLen ) { SetEvent( m_hSemComm ); return EER_CODE_LEICAI_COM_WRITE_DATA; } Sleep(300); #endif return 0; } int leicai::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg) { #if DEBUG_LEICAI 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,"写 串口没有读到数据")); return ERR_CODE_LEICAI_COM_READ_NO_DATA; } // 设置串口等待事件为有信号; SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(); #endif return 0; } void leicai::SimulationCommData(void) { //01 04 0B 02 56 03 12 11 10 06 04 10 23 35 6E 1F }