////////////////////////////////////////////////////////////////////////////// ////// ////// ////// 文 件: UL33.cpp ////// ////// 作 者: wangjianfeng ////// ////// 创建时间: ////// ////// 说 明: UL33协议 ////// ////// ////// ////// 修改时间:2010-05-11 ////// ////// 修改说明:新版本 ////// ////// ////// ////////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CommProcess.h" #include "Global.h" #include "UL33.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CUL33::CUL33(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()创建或打开一个命名的或无名的事件对象 memset(m_szUL33_41Msg, 0, sizeof(m_szUL33_41Msg)); memset(m_szUL33_43Msg, 0, sizeof(m_szUL33_43Msg)); memset(m_szUL33_44Msg, 0, sizeof(m_szUL33_44Msg)); memset(m_szUL33_C0Msg, 0, sizeof(m_szUL33_C0Msg)); memset(m_szUL33_C1Msg, 0, sizeof(m_szUL33_C1Msg)); memset(m_szUL33_C2Msg, 0, sizeof(m_szUL33_C2Msg)); memset(m_szUL33_C3Msg, 0, sizeof(m_szUL33_C3Msg)); memset(m_szUL33_C4Msg, 0, sizeof(m_szUL33_C4Msg)); memset(m_szUL33_C7Msg, 0, sizeof(m_szUL33_C7Msg)); memset(m_szUL33_C8Msg, 0, sizeof(m_szUL33_C8Msg)); m_devOnline = TRUE; m_dwOnlineTick = 0; } CUL33::~CUL33() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL CUL33::UpsULOpenComm(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 CUL33::SendReadRequest( char szPath[MAX_PATH], char szIniName[MAX_PATH], int nCommPort, int nAddr, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int nReversed1, int nReversed2, int nReversed3, int nReversed4, int nReversed5, float fReversed1, float fReversed2, float fReversed3, char szReversed1[MAX_RESERVED1], char szReversed2[MAX_RESERVED2], char szReversed3[MAX_RESERVED3], char szReversed4[MAX_RESERVED4], char szReversed5[MAX_RESERVED5] ) { int nIndex = 0, nLen = 0; char szSendMsg[UL33_SEND_MSG] = {0}; char szType[TYPE_LENGTH] = {0}; GetULFromIni(szPath, szIniName, szCmd,szSendMsg, szType, nIndex, nLen); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if ( (strlen(m_szUL33_41Msg) ==0 && ( strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ) ) || (strlen(m_szUL33_C0Msg) ==0 && ( strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ) ) || (strlen(m_szUL33_C1Msg) ==0 && ( strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0|| strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 ) ) || (strlen(m_szUL33_C2Msg) ==0 && ( strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ) ) || (strlen(m_szUL33_C3Msg) ==0 && ( strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 ) ) || (strlen(m_szUL33_43Msg) ==0 && ( strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ) ) || (strlen(m_szUL33_44Msg) ==0 && ( strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ) ) || (strlen(m_szUL33_C7Msg) ==0 && ( strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 || strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 || strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 || strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0 || strcmp(szCmd, "cmd-69") == 0 || strcmp(szCmd, "cmd-70") == 0 || strcmp(szCmd, "cmd-71") == 0 || strcmp(szCmd, "cmd-72") == 0 || strcmp(szCmd, "cmd-73") == 0 || strcmp(szCmd, "cmd-74") == 0 || strcmp(szCmd, "cmd-75") == 0 || strcmp(szCmd, "cmd-76") == 0 || strcmp(szCmd, "cmd-77") == 0 || strcmp(szCmd, "cmd-78") == 0 || strcmp(szCmd, "cmd-79") == 0 || strcmp(szCmd, "cmd-80") == 0 || strcmp(szCmd, "cmd-81") == 0 || strcmp(szCmd, "cmd-82") == 0 || strcmp(szCmd, "cmd-83") == 0 || strcmp(szCmd, "cmd-84") == 0 || strcmp(szCmd, "cmd-85") == 0 || strcmp(szCmd, "cmd-86") == 0 || strcmp(szCmd, "cmd-87") == 0 || strcmp(szCmd, "cmd-88") == 0 ) ) || (strlen(m_szUL33_C8Msg) ==0 && ( strcmp(szCmd, "cmd-89") == 0 || strcmp(szCmd, "cmd-90") == 0 || strcmp(szCmd, "cmd-91") == 0 || strcmp(szCmd, "cmd-92") == 0 || strcmp(szCmd, "cmd-93") == 0 || strcmp(szCmd, "cmd-94") == 0 || strcmp(szCmd, "cmd-95") == 0 || strcmp(szCmd, "cmd-96") == 0 || strcmp(szCmd, "cmd-97") == 0 || strcmp(szCmd, "cmd-98") == 0 ) ) || (strlen(m_szUL33_C4Msg) == 0 && ( strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 )) || strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-89") == 0 ) { nRet = GetDeviceParam( nAddr,pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); 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 = GetULQ41VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQ43VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQ44VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQC0VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQC1VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQC2VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQC3VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQC4VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQC7VarMsg(szCmd, szMsg, nIndex, nLen, szType); nRet = GetULQC8VarMsg(szCmd, szMsg, nIndex, nLen, szType); LOG4C((LOG_NOTICE,"nRet = %d,cmd = %s,值 = %s",nRet,szCmd,szMsg)); return nRet; } int CUL33::GetDeviceParam( int nAddr, CCommProcess *pComm, //串口对象指针 char szSendMsg[UL33_SEND_MSG], //发送Buffer char szCmd[MAX_CMD], // 命令 char szRecvMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH]) // 变量数据类型 { int nRet = -1; nRet = RequestReadStatus(nAddr,pComm, szSendMsg); if( nRet != 0 ) { return nRet; // 串口忙 } nRet = ResponseReadStatus(pComm, szCmd, szRecvMsg, nIndex, nLen, szType); return nRet; } int CUL33::GetULQ41VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-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 || strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_41Msg + nIndex, szRecvMsg, nLen,0,0 ); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQC0VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_C0Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQC1VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_C1Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQC2VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_C2Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQC3VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_C3Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQ43VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_43Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQ44VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_44Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQC7VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if(strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 || strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 || strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 || strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0 || strcmp(szCmd, "cmd-69") == 0 || strcmp(szCmd, "cmd-70") == 0 || strcmp(szCmd, "cmd-71") == 0 || strcmp(szCmd, "cmd-72") == 0 || strcmp(szCmd, "cmd-73") == 0 || strcmp(szCmd, "cmd-74") == 0 || strcmp(szCmd, "cmd-75") == 0 || strcmp(szCmd, "cmd-76") == 0 || strcmp(szCmd, "cmd-77") == 0 || strcmp(szCmd, "cmd-78") == 0 || strcmp(szCmd, "cmd-79") == 0 || strcmp(szCmd, "cmd-80") == 0 || strcmp(szCmd, "cmd-81") == 0 || strcmp(szCmd, "cmd-82") == 0 || strcmp(szCmd, "cmd-83") == 0 || strcmp(szCmd, "cmd-84") == 0 || strcmp(szCmd, "cmd-85") == 0 || strcmp(szCmd, "cmd-86") == 0 || strcmp(szCmd, "cmd-87") == 0 || strcmp(szCmd, "cmd-88") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType,m_szUL33_C7Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQC8VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-89") == 0 || strcmp(szCmd, "cmd-90") == 0 || strcmp(szCmd, "cmd-91") == 0 || strcmp(szCmd, "cmd-92") == 0 || strcmp(szCmd, "cmd-93") == 0 || strcmp(szCmd, "cmd-94") == 0 || strcmp(szCmd, "cmd-95") == 0 || strcmp(szCmd, "cmd-96") == 0 || strcmp(szCmd, "cmd-97") == 0 || strcmp(szCmd, "cmd-98") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_C8Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CUL33::GetULQC4VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH]) { int nRet = 0; if( strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif DataConversion(szType, m_szUL33_C4Msg + nIndex, szRecvMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } // 发送设置设备参数请求 int CUL33::SendSetReuest( char szPath[MAX_PATH], // 程序所在路径 char szIniName[MAX_PATH], // 配置文件名称 int nCommPort, // 串行端口 int nAddr, // 设备地址 char szCmd[MAX_CMD], // 请求命令 char szRecvMsg[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接口 ) { return 0; } int CUL33::ResponseReadStatus( CCommProcess *pComm, char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH] ) { #if DEBUG_UL33 //LOG4C((LOG_NOTICE,"->0")); RESPONSE_STRUCT structResponse; memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) ); int nReceiveLen = 0; nReceiveLen = sizeof(RESPONSE_STRUCT); char *pBuffer = new char[ nReceiveLen ]; memset(pBuffer, 0, nLen); int nReadLen = 0; nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen); if (!CheckLength(pBuffer)) { SetEvent(m_hSemComm); if (pBuffer !=NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"->长度校验出错")); return ERR_CODE_UL33_COM_CHKSUM_LOST; //长度校验出错 } if (!ChkSumCheck(pBuffer,nReadLen)) { SetEvent(m_hSemComm); if (pBuffer !=NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"->校验码校验出错")); return ERR_CODE_UL33_COM_CHKSUM_LOST; //校验码校验出错 } int nRet = RtnCheck(pBuffer, nReadLen); if (nRet != 0) { SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"->RTN校验出错")); return nRet; //RTN校验出错 } if( nReadLen <= 0) { // 串口没有读到数据 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } LOG4C((LOG_NOTICE,"->串口没有读到数据")); return ERR_CODE_UL33_COM_READ_NO_DATA; } SetULQ41VarMsg( szCmd, pBuffer); SetULQ43VarMsg( szCmd, pBuffer); SetULQ44VarMsg( szCmd, pBuffer); SetULQC0VarMsg( szCmd, pBuffer); SetULQC1VarMsg( szCmd, pBuffer); SetULQC2VarMsg( szCmd, pBuffer); SetULQC3VarMsg( szCmd, pBuffer); SetULQC4VarMsg( szCmd, pBuffer); SetULQC7VarMsg( szCmd, pBuffer); SetULQC8VarMsg( szCmd, pBuffer); m_dwOnlineTick = GetTickCount(); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(); #endif return 0; } int CUL33::RequestReadStatus(int nAddr,CCommProcess *pComm,char chSendMsg[UL33_SEND_MSG]) { #if DEBUG_UL33 #if 0 char chLength[4] = {0}; char chChkSum[5] = {0}; REQUESTPARAM RequestPara; int iLen = sizeof(RequestPara); memset( &RequestPara, 0, iLen ); //起始位 RequestPara.RequestChild.bySoi = 0x7E; //通讯协议版本 RequestPara.RequestChild.byVer[0] = 0x31; RequestPara.RequestChild.byVer[1] = 0x37; //设备地址描述 RequestPara.RequestChild.byAdr[0] = ByteToAscii((nAddr >> 4) & 0x0f); RequestPara.RequestChild.byAdr[1] = ByteToAscii(nAddr & 0x0f); //Cid1 RequestPara.RequestChild.byCid1[0] = 0x32; RequestPara.RequestChild.byCid1[1] = 0x41; //Cid2 memcpy(RequestPara.RequestChild.byCid2, chSendMsg, 2); RequestPara.RequestChild.byLength[0] = 0x45; RequestPara.RequestChild.byLength[1] = 0x30; RequestPara.RequestChild.byLength[2] = 0x30; RequestPara.RequestChild.byLength[3] = 0x32; //Info RequestPara.RequestChild.byInfo[0] = 0x30; RequestPara.RequestChild.byInfo[1] = 0x31; int nLen = sizeof(RequestPara.byCheckSum) -1; //校验码 BYTE *pDataBuf = new BYTE[ iLen - nLen]; memset(pDataBuf, 0, iLen -nLen ); memcpy(pDataBuf, &RequestPara, iLen - nLen); GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.byCheckSum) - 2 ); delete []pDataBuf; pDataBuf = NULL; RequestPara.byCheckSum[0] = chChkSum[0]; RequestPara.byCheckSum[1] = chChkSum[1]; RequestPara.byCheckSum[2] = chChkSum[2]; RequestPara.byCheckSum[3] = chChkSum[3]; //结束符 RequestPara.byEoi = 0x0D; //LOG4C((LOG_NOTICE,"sendmsg = %s",(char*)RequestPara)); //WaitForSingObject();等待有信号; if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 { //求得数组字符实际长度; int nDataLen = (int)sizeof(RequestPara); //ResetEvent()设置无信号状态 ResetEvent( m_hSemComm ); int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen); if( nResult != nDataLen ) { //SetEvent设置有信号; SetEvent( m_hSemComm ); //返回 错误值; return EER_CODE_UL33_COM_REGNUM; } } else { //如果m_hSemCom无信号,返回错误; return ERR_CODE_UL33_COM_BUSY; } #endif BYTE szSendMSG[32] = {0}; // 对数组进行赋值; szSendMSG[0] = 0x7E; szSendMSG[1] = 0x31; szSendMSG[2] = 0x37; szSendMSG[3] = ByteToAscii( (nAddr >> 4) & 0x0F); szSendMSG[4] = ByteToAscii( nAddr & 0x0F); szSendMSG[5] = 0x32; szSendMSG[6] = 0x41; memcpy(szSendMSG + 7, chSendMsg, 2); // 进行lengcheck判断; if ( nAddr == 4 ) { BYTE szCheck[5] = {0}; memcpy(szSendMSG + 9, "E002", 4); // LENID; memcpy(szSendMSG + 13, "01", 2); // DATAF; GetEmersonCheck((char *)szSendMSG, (char*)szCheck); memcpy(szSendMSG + 15,szCheck,4); szSendMSG[strlen((char*)szSendMSG)] = 0x0D; } else if ( nAddr == 3) { BYTE szCheck[5] = {0}; memcpy(szSendMSG + 9, "E002", 4); // LENID; memcpy(szSendMSG + 13, "02", 2); // DATAF; GetEmersonCheck((char *)szSendMSG, (char*)szCheck); memcpy(szSendMSG + 15,szCheck,4); szSendMSG[strlen((char*)szSendMSG)] = 0x0D; } int iLen = strlen((char*)szSendMSG); if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) { ResetEvent(m_hSemComm); int nResult = pComm->Write(szSendMSG, iLen); if (nResult != iLen){ SetEvent(m_hSemComm); return EER_CODE_UL33_COM_REGNUM; } } else{ return ERR_CODE_UL33_COM_BUSY; } LOG4C((LOG_NOTICE,"发送命令=%s",szSendMSG)); #endif return 0; } void CUL33::GetEmersonCheck(char *szData, char *szCheck ,int nlen /* = 12 */) { DWORD dwSum(0); nlen = strlen(szData); for (int i(0); i < nlen; i++) { dwSum += szData[i+1] ; } WORD iCompliment = dwSum; iCompliment = ~iCompliment;//取反 iCompliment++; itoa(iCompliment, szCheck, 16); for (int i(0); i < 5 ; i++) { lowcase2uppcase((BYTE &)szCheck[i]); } } void CUL33::lowcase2uppcase(BYTE &btSrc) { if( btSrc >= 'a' && btSrc <= 'z' ) { btSrc = btSrc - 'a' + 'A'; } } void CUL33::SetULQ41VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-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 || strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_41Msg, pBuff, sizeof(m_szUL33_41Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQC0VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_C0Msg, pBuff, sizeof(m_szUL33_C0Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQC1VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_C1Msg, pBuff, sizeof(m_szUL33_C1Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQC2VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-27") == 0|| strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_C2Msg, pBuff, sizeof(m_szUL33_C2Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQC3VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_C3Msg, pBuff, sizeof(m_szUL33_C3Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQ43VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-42") == 0 || strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 || strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 || strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 || strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 || strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_43Msg, pBuff, sizeof(m_szUL33_43Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQ44VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 || strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 || strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_44Msg, pBuff, sizeof(m_szUL33_44Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQC7VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 || strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 || strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 || strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 || strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0 || strcmp(szCmd, "cmd-69") == 0 || strcmp(szCmd, "cmd-70") == 0 || strcmp(szCmd, "cmd-71") == 0 || strcmp(szCmd, "cmd-72") == 0 || strcmp(szCmd, "cmd-73") == 0 || strcmp(szCmd, "cmd-74") == 0 || strcmp(szCmd, "cmd-75") == 0 || strcmp(szCmd, "cmd-76") == 0 || strcmp(szCmd, "cmd-77") == 0 || strcmp(szCmd, "cmd-78") == 0 || strcmp(szCmd, "cmd-79") == 0 || strcmp(szCmd, "cmd-80") == 0 || strcmp(szCmd, "cmd-81") == 0 || strcmp(szCmd, "cmd-82") == 0 || strcmp(szCmd, "cmd-83") == 0 || strcmp(szCmd, "cmd-84") == 0 || strcmp(szCmd, "cmd-85") == 0 || strcmp(szCmd, "cmd-86") == 0 || strcmp(szCmd, "cmd-87") == 0 || strcmp(szCmd, "cmd-88") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_C7Msg, pBuff, sizeof(m_szUL33_C7Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQC8VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-89") == 0 || strcmp(szCmd, "cmd-90") == 0 || strcmp(szCmd, "cmd-91") == 0 || strcmp(szCmd, "cmd-92") == 0 || strcmp(szCmd, "cmd-93") == 0 || strcmp(szCmd, "cmd-94") == 0 || strcmp(szCmd, "cmd-95") == 0 || strcmp(szCmd, "cmd-96") == 0 || strcmp(szCmd, "cmd-97") == 0 || strcmp(szCmd, "cmd-98") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_C8Msg, pBuff, sizeof(m_szUL33_C8Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CUL33::SetULQC4VarMsg(char szCmd[MAX_CMD], char *pBuff) { if( strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szUL33_C4Msg, pBuff, sizeof(m_szUL33_C4Msg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } UINT CUL33::GetCheckSum(char *pBuf, char chDest[5], int len) { WORD iSum = 0;; for(int i=0; i