////////////////////////////////////////////////////////////////////////////// ////// ////// ////// 文 件: Sda_ii.cpp ////// ////// 作 者: wangjianfeng ////// ////// 创建时间: ////// ////// 说 明: Sda_ii协议 ////// ////// ////// ////// 修改时间:2010-08-16 ////// ////// 修改说明:新版本 ////// ////// ////// ////////////////////////////////////////////////////////////////////////////// #include "StdAfx.h" #include "CommProcess.h" #include "Global.h" #include ".\sda_ii.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif CSda_ii::CSda_ii( 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_sz4CMsg[i], 0, sizeof(m_sz4CMsg[i])); memset(m_sz52Msg[i], 0, sizeof(m_sz52Msg[i])); m_devOnline[i] = TRUE; m_dwOnlineTick[i] = 0; } } CSda_ii::~CSda_ii() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL CSda_ii::AirSdaOpenComm(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 ); if (bResult == FALSE) { //LOG4C((LOG_NOTICE,"依米康OpenCommFail")); } return bResult; } //请求数据 int CSda_ii::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,Funid = 0,startbit = 0,endbit = 0; char SendCmd[SEND_MSG] = {0}; char szType[TYPE_LENGTH] = {0}; GetULFromIni(szPath, szIniName, szCmd,SendCmd, szType, nIndex, nLen,Funid,startbit, endbit); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) { //LOG4C((LOG_NOTICE,"串口句柄无效")); return -1; } int nRet = -1; if ( (strlen(m_sz4CMsg[nAddr-1]) ==0 && ( 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 || 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 || 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 || 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 || 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 )) || (strlen(m_sz52Msg[nAddr-1]) == 0 && ( strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 )) || strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ) { nRet = GetDeviceParam( nAddr,pComm, SendCmd, szCmd, szMsg, nIndex, nLen, szType,Funid ); 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 = Get4CVarMsg(szCmd, szMsg, nIndex, nLen, szType,startbit, endbit,nAddr); nRet = Get52VarMsg(szCmd, szMsg, nIndex, nLen, szType,nAddr); return nRet; } int CSda_ii::GetDeviceParam( int nAddr, CCommProcess *pComm, //串口对象指针 char SendCmd[SEND_MSG], //发送Buffer char szCmd[MAX_CMD], // 命令 char szRecvMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH], // 变量数据类型 int Funid ) { int nRet = -1; nRet = RequestReadStatus(nAddr,pComm, SendCmd,Funid); if( nRet != 0 ) { return nRet; } nRet = ResponseReadStatus(pComm, szCmd, SendCmd, nIndex, nLen, szType,nAddr); return nRet; } int CSda_ii::Get4CVarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startbit, int endbit,int nAddr) { int nRet = 0; if( strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 //温湿度; ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(szRecvMsg, m_sz4CMsg[nAddr - 1] + nIndex ,nLen); GetDataDi(szRecvMsg); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } if( 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 || 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 || 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 || 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 || 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 //9~36 告警值; ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(szRecvMsg, m_sz4CMsg[nAddr - 1] + nIndex ,nLen);//命令对应的需要的数据szRecvMsg; //GetDataDi(szRecvMsg); GetByteHex(szRecvMsg); int iStatus = ::atoi(szRecvMsg); /*LOG4C((LOG_NOTICE , "szCmd = %s, iStatus = %d, szRecvMsg = %s" ,szCmd ,iStatus, szRecvMsg));*/ GetBitStatu(iStatus,szRecvMsg,startbit,endbit); LOG4C((LOG_NOTICE , "szCmd = %s, iStatus = %d, 位信息 = %s" ,szCmd ,iStatus, szRecvMsg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CSda_ii::Get52VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH],int nAddr) { int nRet = 0; if( strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || //温度与湿度设定值; strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(szRecvMsg, m_sz52Msg[nAddr - 1] + nIndex ,nLen); //LOG4C((LOG_NOTICE, "szRecvMsg = %s",szRecvMsg)); GetDataDi(szRecvMsg); //LOG4C((LOG_NOTICE, "szRecvMsg = %s",szRecvMsg)); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } // 发送设置设备参数请求 int CSda_ii::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接口 ) { int nIndex = 0, nLen = 0,Funid = 0 ,startbit = 0,endbit = 0; char SendCmd[SEND_MSG] = {0}; char szType[TYPE_LENGTH] = {0}; GetULFromIni(szPath, szIniName, szCmd,SendCmd, szType, nIndex, nLen,Funid,startbit ,endbit); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if (strcmp(szCmd,"cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 ) { nRet = SetDeviceParam( nAddr,pComm, SendCmd, szCmd, SendCmd, nIndex, nLen, szType,Funid ); if( nRet != 0 ) return nRet; } return 0; } int CSda_ii::SetDeviceParam( int nAddr, CCommProcess *pComm, //串口对象指针 char SendCmd[SEND_MSG], //发送Buffer char szCmd[MAX_CMD], // 命令 char szRecvMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH], // 变量数据类型 int Funid) { int nRet = -1; nRet = RequestWriteStatus(nAddr,pComm, SendCmd,Funid); if( nRet != 0 ) { return nRet; } nRet = ResponseWriteStatus(pComm, szCmd, SendCmd, nIndex, nLen, szType,nAddr); return nRet; } int CSda_ii:: RequestWriteStatus( int nAddr,CCommProcess *pComm,char SendCmd[SEND_MSG], int Funid) { #if DEBUG_SDA RESPONSE_4CSTRUCT RequestPara; int iLen = sizeof(RequestPara); memset( &RequestPara, 0, iLen ); RequestPara.SOI = 0x02; if (strcmp(SendCmd,"4F") == 0 ) { RequestPara.CMD = 0x4F; } else if (strcmp(SendCmd, "46") == 0) { RequestPara.CMD = 0x46; } RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f); RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f); RequestPara.EOI = 0x03; if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 { int nDataLen = (int)sizeof(RequestPara); ResetEvent( m_hSemComm ); int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen); Sleep(3000); if( nResult != nDataLen ) { SetEvent( m_hSemComm ); return ERR_CODE_Sda_ii_COM_INVALIDRES; } } else return ERR_CODE_Sda_ii_COM_BUSY; #endif return 0; } int CSda_ii::ResponseWriteStatus( CCommProcess *pComm, char szCmd[MAX_CMD], char SendCmd[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int nAddr ) { #if DEBUG_SDA int nReceiveLen = 6; char *pBuffer = new char[ nReceiveLen ]; memset(pBuffer, 0, nReceiveLen); int nReadLen = 0; nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen); if( nReadLen <= 0) { SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_Sda_ii_COM_READ_NO_DATA; } m_dwOnlineTick[nAddr -1] = GetTickCount(); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #endif return 0; } int CSda_ii::ResponseReadStatus( CCommProcess *pComm, char szCmd[MAX_CMD], char SendCmd[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int nAddr) { #if DEBUG_SDA int nReceiveLen = 0; if (stricmp(SendCmd,"4C") == 0) { nReceiveLen = 25; } else if (strcmp(SendCmd ,"52") == 0) { nReceiveLen = 13; } if (nReceiveLen == 0) { LOG4C((LOG_NOTICE, "nReceiveLen = %d ", nReceiveLen)); return -1; } char *pBuffer = new char[ nReceiveLen ]; memset(pBuffer, 0, nReceiveLen); int nReadLen = 0; nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen); if ( !GetCheckSum(pBuffer, nReceiveLen -1 )) { //LOG4C((LOG_NOTICE, "ResponseReadStatus::请求%s校验码校验出错", szCmd)); SetEvent(m_hSemComm); if (pBuffer !=NULL) { delete []pBuffer; pBuffer = NULL; } return ERR_CODE_Sda_ii_COM_CHKSUM_LOST; //校验码校验出错 } if( nReadLen <= 0) { //LOG4C((LOG_NOTICE, "ResponseReadStatus::串口没有读到数据")); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete []pBuffer; pBuffer = NULL; } return ERR_CODE_Sda_ii_COM_READ_NO_DATA; } Set4CVarMsg( szCmd, pBuffer,nAddr); Set52VarMsg( szCmd, pBuffer,nAddr); m_dwOnlineTick[nAddr -1] = GetTickCount(); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete []pBuffer; pBuffer = NULL; } #else SimulationCommData(); #endif return 0; } int CSda_ii::RequestReadStatus(int nAddr,CCommProcess *pComm,char SendCmd[SEND_MSG], int Funid) { #if DEBUG_SDA if (stricmp(SendCmd,"4C") == 0) { RESPONSE_4CSTRUCT RequestPara; int iLen = sizeof(RequestPara); memset( &RequestPara, 0, iLen ); RequestPara.SOI = 0x02; RequestPara.CMD = 0x4C; RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);; RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f); RequestPara.EOI = 0x03; 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 != nDataLen ) { SetEvent( m_hSemComm ); return ERR_CODE_Sda_ii_COM_INVALIDRES; } } else return ERR_CODE_Sda_ii_COM_BUSY; } else if (strcmp(SendCmd,"52") == 0) { RESPONSE_53STRUCT RequestPara; int iLen = sizeof(RequestPara); memset( &RequestPara, 0, iLen ); RequestPara.SOI = 0x02; RequestPara.CMD = 0x52; RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);; RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f); RequestPara.INDEX[0] = ByteToAscii((Funid >> 4) & 0x0f);; RequestPara.INDEX[1] = ByteToAscii(Funid & 0x0f); RequestPara.EOI = 0x03; 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 != nDataLen ) { SetEvent( m_hSemComm ); return ERR_CODE_Sda_ii_COM_INVALIDRES; } } else { return ERR_CODE_Sda_ii_COM_BUSY; } } #endif return 0; } void CSda_ii::Set4CVarMsg(char szCmd[MAX_CMD], char *pBuff,int nAddr) { 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 || 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 || 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 || 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 || 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_sz4CMsg[nAddr-1],pBuff,sizeof(m_sz4CMsg[nAddr-1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CSda_ii::Set52VarMsg(char szCmd[MAX_CMD], char *pBuff,int nAddr) { if( strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_sz52Msg[nAddr-1],pBuff,sizeof(m_sz52Msg[nAddr-1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } //获取校验码; BOOL CSda_ii::GetCheckSum(char *pBuf, int iLen) { WORD iSum = 0; char chDest[3] = {0}; for(int i=1; i< iLen - 2 ; i++)//求和 { iSum += pBuf[i]; } char chTemp[3] = {0}; chTemp[0] = pBuf[iLen-2]; chTemp[1] = pBuf[iLen-1]; WORD iCompliment = iSum; chDest[0] = ByteToAscii((iSum >> 4) & 0x0f); chDest[1] = ByteToAscii(iSum & 0x0f); if ( chDest[0] == chTemp[0] && chDest[1] == chTemp[1]) { return TRUE; } return FALSE; } int CSda_ii::GetULFromIni( char szPath[MAX_PATH], //服务器程序所在目录 char szIniName[MAX_PATH], //配置文件名 char szCmd[MAX_CMD], //命令 char SendCmd[MAX_CMD], //发送Buffer char nType[MAX_CMD], int &nIndex, int &nLen, int &Funid, int &startbit, int &endbit ) { CHAR szFile[MAX_PATH + 1] = ""; wsprintf(szFile, "%s\\config\\%s", szPath, szIniName); GetPrivateProfileString(szCmd, "SendCmd", "", SendCmd, 10, szFile); SendCmd[strlen(SendCmd)] = '\0'; GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile); nType[strlen(nType)] = '\0'; Funid = GetPrivateProfileInt(szCmd, "Funid", 0, szFile); nIndex = GetPrivateProfileInt(szCmd, "index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "len", 0, szFile); startbit = GetPrivateProfileInt(szCmd, "startbit", 0 ,szFile); endbit = GetPrivateProfileInt(szCmd, "endbit", 0 ,szFile); return 0; } //获取bit位状态值 ; void CSda_ii::GetBitStatu(int iVaule,char *bytV,int startbit,int endbit) { BYTE bytValue; bytValue = iVaule; int ByteBit = 0; char tempbyte[9] = {0}; if (startbit == endbit) { switch(startbit) { case 0: ByteBit = bytValue & 0x01; break; case 1: ByteBit = (bytValue & 0x02) / 0x02 ; break; case 2: ByteBit = (bytValue & 0x04) / 0x04 ; break; case 3: ByteBit = (bytValue & 0x08) / 0x08 ; break; case 4: ByteBit = (bytValue & 0x10) / 0x10 ; break; case 5: ByteBit = (bytValue & 0x20) / 0x20 ; break; case 6: ByteBit = (bytValue & 0x40) / 0x40 ; break; case 7: ByteBit = (bytValue & 0x80) / 0x80; break; } _snprintf(bytV,sizeof(bytV),"%d",ByteBit); } else { int tempbit = startbit; int TiLen = endbit - startbit; for (int i = 0; i < TiLen;i++) { tempbit = tempbit + i; switch(tempbit) { case 0: ByteBit = bytValue & 0x01; break; case 1: ByteBit = (bytValue & 0x02) / 0x02 ; break; case 2: ByteBit = (bytValue & 0x04) / 0x04 ; break; case 3: ByteBit = (bytValue & 0x08) / 0x08 ; break; case 4: ByteBit = (bytValue & 0x10) / 0x10 ; break; case 5: ByteBit = (bytValue & 0x20) / 0x20 ; break; case 6: ByteBit = (bytValue & 0x40) / 0x40 ; break; case 7: ByteBit = (bytValue & 0x80) / 0x80; break; } _snprintf(tempbyte+i,sizeof(tempbyte),"%d",ByteBit); } strcpy(bytV,tempbyte); //bytValue = tempbyte; } } //十六进制转换十进制 void CSda_ii::GetDataDi(char *szMsg) { char chDest[2] = {0}; chDest[0] = szMsg[0]; chDest[1] = szMsg[1]; //LOG4C((LOG_NOTICE, "szMsg = %s",szMsg)); int iConvMsg = 0; int iszMsg_1 = 0 ,iszMsg_2 = 0; //16进制转换十进制 if ((szMsg[2] >= 'A')&&(szMsg[2] <= 'F')) { iszMsg_1 = szMsg[2] - 'A' + 10; } else if ((szMsg[2] >= 'a')&&(szMsg[2] <= 'f')) { iszMsg_1 = szMsg[2] - 'a' + 10; } else if ((szMsg[2] >= '0')&&(szMsg[2] <= '9')) { iszMsg_1 =szMsg[2] - '0'; } if ((szMsg[3] >= 'A')&&(szMsg[3] <= 'F')) { iszMsg_2 = szMsg[3] - 'A' + 10; } else if ((szMsg[3] >= 'a')&&(szMsg[3] <= 'f')) { iszMsg_2 = szMsg[3] - 'a' + 10; } else if ((szMsg[3] >= '0')&&(szMsg[3] <= '9')) { iszMsg_2 = szMsg[3] - '0'; } iConvMsg = iszMsg_1*16 + iszMsg_2; itoa(iConvMsg,szMsg,10); //LOG4C((LOG_NOTICE , "十六进制转换十进制 iConvMsg = %d",iConvMsg)); } //十六进制转换十进制 void CSda_ii::GetByteHex(char *szMsg) { char chDest[2] = {0}; chDest[0] = szMsg[0]; chDest[1] = szMsg[1]; //LOG4C((LOG_NOTICE, "szMsg = %s",szMsg)); int iConvMsg = 0; int iszMsg_1 = 0 ,iszMsg_2 = 0; //16进制转换十进制 if ((szMsg[0] >= 'A')&&(szMsg[0] <= 'F')) { iszMsg_1 = szMsg[0] - 'A' + 10; } else if ((szMsg[0] >= 'a')&&(szMsg[0] <= 'f')) { iszMsg_1 = szMsg[0] - 'a' + 10; } else if ((szMsg[0] >= '0')&&(szMsg[0] <= '9')) { iszMsg_1 =szMsg[0] - '0'; } if ((szMsg[1] >= 'A')&&(szMsg[1] <= 'F')) { iszMsg_2 = szMsg[1] - 'A' + 10; } else if ((szMsg[1] >= 'a')&&(szMsg[1] <= 'f')) { iszMsg_2 = szMsg[1] - 'a' + 10; } else if ((szMsg[1] >= '0')&&(szMsg[1] <= '9')) { iszMsg_2 = szMsg[1] - '0'; } iConvMsg = iszMsg_1*16 + iszMsg_2; itoa(iConvMsg,szMsg,10); //LOG4C((LOG_NOTICE , "十六进制转换十进制 iConvMsg = %d",iConvMsg)); } void CSda_ii::SimulationCommData(void) { m_sz4CMsg[0][0]=0x02 ; m_sz4CMsg[0][1]=0x4c; m_sz4CMsg[0][2]=0x30; m_sz4CMsg[0][3]=0x31; m_sz4CMsg[0][4]=0x30; m_sz4CMsg[0][5]=0x30; m_sz4CMsg[0][6]=0x43; m_sz4CMsg[0][7]=0x33; m_sz4CMsg[0][8]=0x30; m_sz4CMsg[0][9]=0x30; m_sz4CMsg[0][10]=0x32; m_sz4CMsg[0][11]=0x31; //.状态; m_sz4CMsg[0][12]=0x30; m_sz4CMsg[0][13]=0x42; m_sz4CMsg[0][14]=0x30; m_sz4CMsg[0][15]=0x30; m_sz4CMsg[0][16]=0x30; m_sz4CMsg[0][17]=0x31; m_sz4CMsg[0][18]=0x30; m_sz4CMsg[0][19]=0x30; m_sz4CMsg[0][20]=0x30; m_sz4CMsg[0][21]=0x30; m_sz4CMsg[0][22]=0x33; m_sz4CMsg[0][23]=0x39; m_sz4CMsg[0][24]=0x03; }