#include "stdafx.h" #include "DataMate3000.h" #include "CommProcess.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif CDataMate3000::CDataMate3000(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_szDataMate3000_42Msg[i], 0, sizeof(m_szDataMate3000_42Msg[i])); memset(m_szDataMate3000_43Msg[i], 0, sizeof(m_szDataMate3000_43Msg[i])); memset(m_szDataMate3000_47Msg[i], 0, sizeof(m_szDataMate3000_47Msg[i])); memset(m_szDataMate3000_4DMsg[i], 0, sizeof(m_szDataMate3000_4DMsg[i])); //memset(m_szDataMate3000_4FMsg, 0, sizeof(m_szDataMate3000_4FMsg)); //memset(m_szDataMate3000_50Msg, 0, sizeof(m_szDataMate3000_50Msg)); //memset(m_szDataMate3000_51Msg, 0, sizeof(m_szDataMate3000_51Msg)); //memset(m_szDataMate3000_80Msg, 0, sizeof(m_szDataMate3000_80Msg)); //memset(m_szDataMate3000_81Msg, 0, sizeof(m_szDataMate3000_81Msg)); memset(m_szDataMate3000_82Msg[i], 0, sizeof(m_szDataMate3000_82Msg[i])); memset(m_szDataMate3000_45Msg[i], 0, sizeof(m_szDataMate3000_45Msg[i])); //memset(m_szDataMate3000_49Msg, 0, sizeof(m_szDataMate3000_49Msg)); //memset(m_szDataMate3000_4EMsg, 0, sizeof(m_szDataMate3000_4EMsg)); m_devOnline[i] = TRUE; m_dwOnlineTick[i] = 0; } } CDataMate3000::~CDataMate3000() { #if IS_USE_READMSG_CS DeleteCriticalSection( &m_csReadMsg ); #endif MTVERIFY( CloseHandle( m_hSemComm ) ); CloseComm(); } BOOL CDataMate3000::DataMate3000OpenComm(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 CDataMate3000::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 ) { int nIndex = 0, nLen = 0, StaBit = 0, EndBit = 0; char szSendMsg[DATAMATE3000_SEND_MSG]; char szType[TYPE_LENGTH] = {0}; char szCid2[CID2_LENGTH] = {0}; char szAddr[2] = {0}; RESPONSE_STRUCT szSend = {0}; memset(&szSend, 0, sizeof(RESPONSE_STRUCT)); memset(szSendMsg, 0, DATAMATE3000_SEND_MSG); memset(szMsg, 0, VAR_MSG); GetIniInfo(szPath, szIniName, szCmd, szCid2, szType, nIndex, nLen, StaBit, EndBit); szSend.Sol = 0x7E; szSend.Ver[0] = '2'; szSend.Ver[1] = '1'; //设备地址描述 sprintf(szAddr, "%x", nAddr); szAddr[0] = ByteToAscii((nAddr >> 4) & 0x0f); szAddr[1] = ByteToAscii(nAddr & 0x0f); memcpy(szSend.Adr, szAddr, 2); //Cid1 szSend.Cid1[0] = '6'; szSend.Cid1[1] = '0'; //cid2 memcpy(szSend.Cid2, szCid2, 2); //memset(szSend.Length, 0, 4); //length szSend.Length[0] = '0'; szSend.Length[1] = '0'; szSend.Length[2] = '0'; szSend.Length[3] = '0'; //chksum memset(szSend.Chksum, 0,sizeof(szSend.Chksum)); memcpy(szSendMsg, &szSend, sizeof(RESPONSE_STRUCT)); GetChkSum(szSendMsg, (char *)szSend.Chksum, sizeof(RESPONSE_STRUCT)); //eol szSend.Eol = 0x0D; memcpy(szSendMsg, &szSend, sizeof(RESPONSE_STRUCT)); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if( nAddr < 1 ) return -1; if( ( strlen(m_szDataMate3000_42Msg[nAddr-1]) == 0 && ( strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ) ) || ( strlen(m_szDataMate3000_43Msg[nAddr-1]) == 0 && strcmp(szCmd, "cmd-4") == 0 ) || ( strlen(m_szDataMate3000_47Msg[nAddr-1]) == 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 ) ) || ( strlen(m_szDataMate3000_4DMsg[nAddr-1]) == 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 ) )|| ( strlen(m_szDataMate3000_82Msg[nAddr-1]) == 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 || 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-43") == 0 ) )|| strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-17") == 0 ) { nRet = GetDeviceParam( pComm, nAddr, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); 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 = GetDataMate3000_42VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType); nRet = GetDataMate3000_43VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType); nRet = GetDataMate3000_47VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType); nRet = GetDataMate3000_4DVarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType); nRet = GetDataMate3000_82VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, StaBit, EndBit); return nRet; } int CDataMate3000::GetDataMate3000_42VarMsg( int nAddr, char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif //memcpy(szMsg, m_szDataMate3000_42Msg + nIndex, nLen); DataConversion(szType, m_szDataMate3000_42Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDataMate3000::GetDataMate3000_43VarMsg( int nAddr, char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH] // 变量数据类型 ) { int nRet = 0; if(strcmp(szCmd, "cmd-4") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif //memcpy(szMsg, m_szDataMate3000_43Msg + nIndex, nLen); DataConversion(szType, m_szDataMate3000_43Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDataMate3000::GetDataMate3000_47VarMsg( int nAddr, char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH] // 变量数据类型 ) { int nRet = 0; if( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif //memcpy(szMsg, m_szDataMate3000_47Msg + nIndex, nLen); DataConversion(szType, m_szDataMate3000_47Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDataMate3000::GetDataMate3000_4DVarMsg( int nAddr, char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH] // 变量数据类型 ) { int nRet = 0; if( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif //memcpy(szMsg, m_szDataMate3000_4DMsg + nIndex, nLen); DataConversion(szType, m_szDataMate3000_4DMsg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDataMate3000::GetDataMate3000_82VarMsg( int nAddr, char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH], // 变量数据类型 int StaBit, int EndBit ) { int nRet = 0; if( 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 || 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-43") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif //memcpy(szMsg, m_szDataMate3000_82Msg + nIndex, nLen); DataConversion(szType, m_szDataMate3000_82Msg[nAddr - 1] + nIndex, szMsg, nLen, StaBit, EndBit); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDataMate3000::GetDataMate3000_45VarMsg( int nAddr, char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH] // 变量数据类型 ) { int nRet = 0; if( strcmp(szCmd, "cmd-44") == 0) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(szMsg, m_szDataMate3000_45Msg[nAddr - 1] + nIndex, nLen); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif nRet = 0; } return nRet; } int CDataMate3000::GetDeviceParam( CCommProcess *pComm, //串口对象指针 int nAddr, char szSendMsg[DATAMATE3000_SEND_MSG], //发送Buffer char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH]) // 变量数据类型 { int nRet = -1; nRet = RequestStatus(szCmd, pComm, szSendMsg); if( nRet != 0 ) { return nRet; // 串口忙 } nRet = ResponseStatus(pComm, nAddr, szCmd, szMsg, nIndex, nLen, szType); return nRet; } int CDataMate3000::SetDeviceParam( CCommProcess *pComm, //串口对象指针 int nAddr, char szSendMsg[DATAMATE3000_SEND_MSG], //发送Buffer char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH]) // 变量数据类型 { int nRet = -1; nRet = WriteRequestStatus(szCmd, pComm, szSendMsg); if( nRet != 0 ) { return nRet; // 串口忙 } nRet = WriteResponseStatus(pComm, nAddr, szCmd, szMsg, nIndex, nLen, szType); return nRet; } void CDataMate3000::SetDataMate3000_42VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDataMate3000_42Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_42Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CDataMate3000::SetDataMate3000_43VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( strcmp(szCmd, "cmd-4") == 0) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDataMate3000_43Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_43Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CDataMate3000::SetDataMate3000_47VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDataMate3000_47Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_47Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CDataMate3000::SetDataMate3000_4DVarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( 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 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDataMate3000_4DMsg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_4DMsg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } void CDataMate3000::SetDataMate3000_82VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer) { if( 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 || 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-43") == 0 ) { #if IS_USE_READMSG_CS EnterCriticalSection( &m_csReadMsg ); #endif memcpy(m_szDataMate3000_82Msg[nAddr - 1], pBuffer, sizeof(m_szDataMate3000_82Msg[nAddr - 1])); #if IS_USE_READMSG_CS LeaveCriticalSection(&m_csReadMsg); #endif } } WORD CDataMate3000::GetDataLength(const WORD wLENID, char chLength[4]) { char szLenID[3] = {0}; //int nLen = 10; DigitToBinary(wLENID, szLenID, sizeof(szLenID)); char chCheckSum = GetLCheckSum(szLenID, sizeof(szLenID)); chLength[0] = chCheckSum; chLength[1] = szLenID[0]; chLength[2] = szLenID[1]; chLength[3] = szLenID[2]; return 0; } char CDataMate3000::GetLCheckSum(char *pBuf, int len) { //WORD iSum = 0; char chCompliment = 0; //unsigned char chCompliment[2] = {0}; for(int i=0; i> 4) & 0x0f); szAddr[1] = ByteToAscii(nAddr & 0x0f); memcpy(szSend.Adr, szAddr, 2); //cid1 szSend.Cid1[0] = '6'; szSend.Cid1[1] = '0'; //cid2 memcpy(szSend.Cid2, IniSendCMD, 2); memset(szSend.Length, 0, 4); //GetDataLength(2, szLength);//取数据长度 //szSend.Length[0] = ByteToAscii(szLength[0]); //szSend.Length[1] = szLength[1]; //szSend.Length[2] = szLength[2]; //szSend.Length[3] = szLength[3]; //length szSend.Length[1] = '0'; szSend.Length[2] = '0'; szSend.Length[3] = '2'; szSend.Length[0] = 'E'; //szInfo memcpy(szSend.szInfo,szMsg, sizeof(szSend.szInfo)); memset(szSend.Chksum, 0,sizeof(szSend.Chksum)); szSend.Eol = 0x0D; memcpy(szSendMsg, &szSend, sizeof(REQUEST_STRUCT)); GetChkSum(szSendMsg, (char *)szSend.Chksum, sizeof(REQUEST_STRUCT)); szSend.Eol = 0x0D; memcpy(szSendMsg, &szSend, sizeof(REQUEST_STRUCT)); CCommProcess *pComm = FindComm(nCommPort); if( pComm == NULL ) return -1; int nRet = -1; if( nAddr < 1 ) return -1; if( strcmp(szCmd, "cmd-44") == 0 ) { SetDeviceParam( pComm, nAddr, szSendMsg, szCmd, szMsg, nIndex, nLen, szType ); } return 0; } //读取配置文件 int CDataMate3000::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,int &nIndex,int &nLen,int &nSBit,int &nEBit,char *szDataType) { char szFile[MAX_PATH + 1] = ""; memset(szDataType, 0, TYPE_LENGTH); wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);//只读config下面的ini GetPrivateProfileString(szCmd, "SendCmd", "", szCid2, CID2_LENGTH, szFile);//读取配置文件中的一段字符串 GetPrivateProfileString(szCmd, "Type", "", szType, TYPE_LENGTH, szFile); nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);//从配置文件中取值 nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); nSBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值 nEBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile); return 0; } int CDataMate3000::RequestStatus( char szCmd[MAX_CMD], // 命令 CCommProcess *pComm, //串口对象指针 char chSendMsg[DATAMATE3000_SEND_MSG] //发送Buffer ) { int nRet = -1; #if DEBUG_DataMate3000 if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口 { int nDataLen = (int)strlen(chSendMsg); ResetEvent( m_hSemComm ); int nResult = pComm->Write((unsigned char *)chSendMsg, nDataLen); if( nResult == nDataLen ) { nRet = 0; } else { SetEvent( m_hSemComm ); return ERR_CODE_DATAMATE3000_COM_INVALIDRES; } } else { return ERR_CODE_DATAMATE3000_COM_BUSY; } #endif return nRet; } int CDataMate3000::ResponseStatus( CCommProcess *pComm, //串口对象指针 int nAddr, // 设备地址 char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH] // 变量数据类型 ) { #if DEBUG_DataMate3000 int nReceiveLen = 0; int nProcessLen = 0; int nReadLen = 0; //RESPONSE_STRUCT structResponse; //memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) ); nReceiveLen = DATEMATE3000_RECEIVE_MSG; char *pBuffer = new char[ nReceiveLen ]; memset(pBuffer, 0, nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen); if( nReadLen <= 0) { // 串口没有读到数据 //TRACE("串口没有读到数据!\r\n"); //AfxMessageBox("串口没有读到数据!"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DATAMATE3000_COM_READ_NO_DATA; } //int nTempLen = strlen(pBuffer); //CString str; //str.Format("%d", nTempLen); //AfxMessageBox(str); if (!ChkSumCheck(pBuffer, (int)strlen(pBuffer))) { // 校检错误 //AfxMessageBox("校检错误"); //TRACE("校检错误!\r\n"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DATAMATE3000_COM_CHKSUM_LOST; } if (!CheckLength(pBuffer)) { // 数据长度校检错误 //TRACE("数据长度校检错误!\r\n"); //AfxMessageBox("数据长度校检错误!"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DATAMATE3000_COM_VARLEN; } int nRet = RtnCheck(pBuffer); if(0 != nRet) { //AfxMessageBox("校验出错"); // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return nRet; } SetDataMate3000_42VarMsg(nAddr, szCmd, pBuffer); SetDataMate3000_43VarMsg(nAddr, szCmd, pBuffer); SetDataMate3000_47VarMsg(nAddr, szCmd, pBuffer); SetDataMate3000_4DVarMsg(nAddr, szCmd, pBuffer); SetDataMate3000_82VarMsg(nAddr, szCmd, pBuffer); m_dwOnlineTick[nAddr - 1] = GetTickCount(); // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(); #endif return 0; } int CDataMate3000::WriteRequestStatus( char szCmd[MAX_CMD], // 命令 CCommProcess *pComm, //串口对象指针 char chSendMsg[DATAMATE3000_SEND_MSG] //发送Buffer ) { #if DEBUG_DataMate3000 int nDataLen = (int)strlen(chSendMsg); ResetEvent( m_hSemComm ); int nResult = pComm->Write((unsigned char *)chSendMsg, nDataLen); if( nResult == nDataLen ) { } else { SetEvent( m_hSemComm ); return EER_CODE_DATAMATE3000_COM_WRITE_DATA; } #endif return 0; } int CDataMate3000::WriteResponseStatus( CCommProcess *pComm, //串口对象指针 int nAddr, // 设备地址 char szCmd[MAX_CMD], // 命令 char szMsg[VAR_MSG], // 接收Buffer int &nIndex, // 变量索引,针对接收Buffer而言 int &nLen, // 变量长度 char szType[TYPE_LENGTH] // 变量数据类型 ) { #if DEBUG_DataMate3000 int nReceiveLen = 0; int nProcessLen = 0; int nReadLen = 0; //RESPONSE_STRUCT structResponse; //memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) ); nReceiveLen = DATEMATE3000_RECEIVE_MSG; char *pBuffer = new char[ nReceiveLen ]; memset(pBuffer, 0, nReceiveLen); nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen); if( nReadLen <= 0) { // 串口没有读到数据 //TRACE("串口没有读到数据!\r\n"); //AfxMessageBox("串口没有读到数据!"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DATAMATE3000_COM_READ_NO_DATA; } #if 0 //int nTempLen = strlen(pBuffer); //CString str; //str.Format("%d", nTempLen); //AfxMessageBox(str); if (!ChkSumCheck(pBuffer, strlen(pBuffer))) { // 校检错误 //AfxMessageBox("校检错误"); //TRACE("校检错误!\r\n"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DATAMATE3000_COM_CHKSUM_LOST; } if (!CheckLength(pBuffer)) { // 数据长度校检错误 //TRACE("数据长度校检错误!\r\n"); //AfxMessageBox("数据长度校检错误!"); SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return ERR_CODE_DATAMATE3000_COM_VARLEN; } int nRet = RtnCheck(pBuffer); if(0 != nRet) { //AfxMessageBox("校验出错"); // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } return nRet; } #endif // 设置串口等待事件为有信号 SetEvent( m_hSemComm ); if( pBuffer != NULL) { delete[] pBuffer; pBuffer = NULL; } #else SimulationCommData(); #endif return 0; } void CDataMate3000::SimulationCommData(void) { /*cid2 =42 :7E 32 31 30 31 36 30 30 30 34 30 30 43 30 30 44 45 30 32 42 43 30 30 30 30 46 42 30 46 0D */ #if 0 m_szDataMate3000_42Msg[0] = 0x7E; m_szDataMate3000_42Msg[1] = 0x32; m_szDataMate3000_42Msg[2] = 0x31; m_szDataMate3000_42Msg[3] = 0x30; m_szDataMate3000_42Msg[4] = 0x31; m_szDataMate3000_42Msg[5] = 0x36; m_szDataMate3000_42Msg[6] = 0x30; m_szDataMate3000_42Msg[7] = 0x30; m_szDataMate3000_42Msg[8] = 0x30; m_szDataMate3000_42Msg[9] = 0x34; m_szDataMate3000_42Msg[10] = 0x30; m_szDataMate3000_42Msg[11] = 0x30; m_szDataMate3000_42Msg[12] = 0x43; m_szDataMate3000_42Msg[13] = 0x30; m_szDataMate3000_42Msg[14] = 0x30; m_szDataMate3000_42Msg[15] = 0x44; m_szDataMate3000_42Msg[16] = 0x45; m_szDataMate3000_42Msg[17] = 0x30; m_szDataMate3000_42Msg[18] = 0x32; m_szDataMate3000_42Msg[19] = 0x42; m_szDataMate3000_42Msg[20] = 0x43; m_szDataMate3000_42Msg[21] = 0x30; m_szDataMate3000_42Msg[22] = 0x30; m_szDataMate3000_42Msg[23] = 0x30; m_szDataMate3000_42Msg[24] = 0x30; m_szDataMate3000_42Msg[25] = 0x46; m_szDataMate3000_42Msg[26] = 0x42; m_szDataMate3000_42Msg[27] = 0x30; m_szDataMate3000_42Msg[28] = 0x46; m_szDataMate3000_42Msg[29] = 0x0D; /*cid2 = 82:7E 32 31 30 31 36 30 30 30 34 30 30 43 30 31 32 37 30 30 30 30 30 30 30 30 46 42 35 35 0D */ m_szDataMate3000_82Msg[0] = 0x7E; m_szDataMate3000_82Msg[1] = 0x32; m_szDataMate3000_82Msg[2] = 0x31; m_szDataMate3000_82Msg[3] = 0x30; m_szDataMate3000_82Msg[4] = 0x31; m_szDataMate3000_82Msg[5] = 0x36; m_szDataMate3000_82Msg[6] = 0x30; m_szDataMate3000_82Msg[7] = 0x30; m_szDataMate3000_82Msg[8] = 0x30; m_szDataMate3000_82Msg[9] = 0x34; m_szDataMate3000_82Msg[10] = 0x30; m_szDataMate3000_82Msg[11] = 0x30; m_szDataMate3000_82Msg[12] = 0x43; m_szDataMate3000_82Msg[13] = 0x30; m_szDataMate3000_82Msg[14] = 0x31; m_szDataMate3000_82Msg[15] = 0x32; m_szDataMate3000_82Msg[16] = 0x37; m_szDataMate3000_82Msg[17] = 0x30; m_szDataMate3000_82Msg[18] = 0x30; m_szDataMate3000_82Msg[19] = 0x30; m_szDataMate3000_82Msg[20] = 0x30; m_szDataMate3000_82Msg[21] = 0x30; m_szDataMate3000_82Msg[22] = 0x30; m_szDataMate3000_82Msg[23] = 0x30; m_szDataMate3000_82Msg[24] = 0x30; m_szDataMate3000_82Msg[25] = 0x46; m_szDataMate3000_82Msg[26] = 0x42; m_szDataMate3000_82Msg[27] = 0x35; m_szDataMate3000_82Msg[28] = 0x35; m_szDataMate3000_82Msg[29] = 0x0D; /*cid2 = 47:7E 32 31 30 31 36 30 30 30 35 30 31 41 30 30 30 30 30 30 30 30 30 30 46 41 30 30 31 45 30 32 35 38 30 30 36 34 30 30 46 38 36 39 0D*/ m_szDataMate3000_47Msg[0] = 0x7E; m_szDataMate3000_47Msg[1] = 0x32; m_szDataMate3000_47Msg[2] = 0x31; m_szDataMate3000_47Msg[3] = 0x30; m_szDataMate3000_47Msg[4] = 0x31; m_szDataMate3000_47Msg[5] = 0x36; m_szDataMate3000_47Msg[6] = 0x30; m_szDataMate3000_47Msg[7] = 0x30; m_szDataMate3000_47Msg[8] = 0x30; m_szDataMate3000_47Msg[9] = 0x35; m_szDataMate3000_47Msg[10] = 0x30; m_szDataMate3000_47Msg[11] = 0x31; m_szDataMate3000_47Msg[12] = 0x41; m_szDataMate3000_47Msg[13] = 0x30; m_szDataMate3000_47Msg[14] = 0x30; m_szDataMate3000_47Msg[15] = 0x30; m_szDataMate3000_47Msg[16] = 0x30; m_szDataMate3000_47Msg[17] = 0x30; m_szDataMate3000_47Msg[18] = 0x30; m_szDataMate3000_47Msg[19] = 0x30; m_szDataMate3000_47Msg[20] = 0x30; m_szDataMate3000_47Msg[21] = 0x30; m_szDataMate3000_47Msg[22] = 0x30; m_szDataMate3000_47Msg[23] = 0x46; m_szDataMate3000_47Msg[24] = 0x41; m_szDataMate3000_47Msg[25] = 0x30; m_szDataMate3000_47Msg[26] = 0x30; m_szDataMate3000_47Msg[27] = 0x31; m_szDataMate3000_47Msg[28] = 0x45; m_szDataMate3000_47Msg[29] = 0x30; m_szDataMate3000_47Msg[30] = 0x32; m_szDataMate3000_47Msg[31] = 0x35; m_szDataMate3000_47Msg[32] = 0x38; m_szDataMate3000_47Msg[33] = 0x30; m_szDataMate3000_47Msg[34] = 0x30; m_szDataMate3000_47Msg[35] = 0x36; m_szDataMate3000_47Msg[36] = 0x34; m_szDataMate3000_47Msg[37] = 0x30; m_szDataMate3000_47Msg[38] = 0x30; m_szDataMate3000_47Msg[39] = 0x46; m_szDataMate3000_47Msg[40] = 0x38; m_szDataMate3000_47Msg[41] = 0x36; m_szDataMate3000_47Msg[42] = 0x39; m_szDataMate3000_47Msg[43] = 0x0D; //m_szDataMate3000_41Msg[44] = 0x42; //m_szDataMate3000_41Msg[45] = 0x34; //m_szDataMate3000_41Msg[46] = 0x33; //m_szDataMate3000_41Msg[47] = 0x30; //m_szDataMate3000_41Msg[48] = 0x30; //m_szDataMate3000_41Msg[49] = 0x30; //m_szDataMate3000_41Msg[50] = 0x30; //m_szDataMate3000_41Msg[51] = 0x35; //m_szDataMate3000_41Msg[52] = 0x42; //m_szDataMate3000_41Msg[53] = 0x34; //m_szDataMate3000_41Msg[54] = 0x33; //m_szDataMate3000_41Msg[55] = 0x30; //m_szDataMate3000_41Msg[56] = 0x30; //m_szDataMate3000_41Msg[57] = 0x30; //m_szDataMate3000_41Msg[58] = 0x30; //m_szDataMate3000_41Msg[59] = 0x35; //m_szDataMate3000_41Msg[60] = 0x42; //m_szDataMate3000_41Msg[61] = 0x34; //m_szDataMate3000_41Msg[62] = 0x33; //m_szDataMate3000_41Msg[63] = 0x30; //m_szDataMate3000_41Msg[64] = 0x30; //m_szDataMate3000_41Msg[65] = 0x30; //m_szDataMate3000_41Msg[66] = 0x30; //m_szDataMate3000_41Msg[67] = 0x32; //m_szDataMate3000_41Msg[68] = 0x30; //m_szDataMate3000_41Msg[69] = 0x34; //m_szDataMate3000_41Msg[70] = 0x31; //m_szDataMate3000_41Msg[71] = 0x30; //m_szDataMate3000_41Msg[72] = 0x30; //m_szDataMate3000_41Msg[73] = 0x30; //m_szDataMate3000_41Msg[74] = 0x30; //m_szDataMate3000_41Msg[75] = 0x32; //m_szDataMate3000_41Msg[76] = 0x30; //m_szDataMate3000_41Msg[77] = 0x34; //m_szDataMate3000_41Msg[78] = 0x31; //m_szDataMate3000_41Msg[79] = 0x30; //m_szDataMate3000_41Msg[80] = 0x30; //m_szDataMate3000_41Msg[81] = 0x30; //m_szDataMate3000_41Msg[82] = 0x30; //m_szDataMate3000_41Msg[83] = 0x32; //m_szDataMate3000_41Msg[84] = 0x30; //m_szDataMate3000_41Msg[85] = 0x34; //m_szDataMate3000_41Msg[86] = 0x31; //m_szDataMate3000_41Msg[87] = 0x30; //m_szDataMate3000_41Msg[88] = 0x30; //m_szDataMate3000_41Msg[89] = 0x30; //m_szDataMate3000_41Msg[90] = 0x30; //m_szDataMate3000_41Msg[91] = 0x31; //m_szDataMate3000_41Msg[92] = 0x30; //m_szDataMate3000_41Msg[93] = 0x34; //m_szDataMate3000_41Msg[94] = 0x31; //m_szDataMate3000_41Msg[95] = 0x30; //m_szDataMate3000_41Msg[96] = 0x30; //m_szDataMate3000_41Msg[97] = 0x38; //m_szDataMate3000_41Msg[98] = 0x30; //m_szDataMate3000_41Msg[99] = 0x30; //m_szDataMate3000_41Msg[100] = 0x39; //m_szDataMate3000_41Msg[101] = 0x34; //m_szDataMate3000_41Msg[102] = 0x35; //m_szDataMate3000_41Msg[103] = 0x30; //m_szDataMate3000_41Msg[104] = 0x30; //m_szDataMate3000_41Msg[105] = 0x30; //m_szDataMate3000_41Msg[106] = 0x30; //m_szDataMate3000_41Msg[107] = 0x30; //m_szDataMate3000_41Msg[108] = 0x30; //m_szDataMate3000_41Msg[109] = 0x45; //m_szDataMate3000_41Msg[110] = 0x30; //m_szDataMate3000_41Msg[111] = 0x31; //m_szDataMate3000_41Msg[112] = 0x38; //m_szDataMate3000_41Msg[113] = 0x0D; //m_szDataMate3000_41Msg[114] = 0x39; //m_szDataMate3000_41Msg[115] = 0x2E; //m_szDataMate3000_41Msg[116] = 0x30; //m_szDataMate3000_41Msg[117] = 0x20; //m_szDataMate3000_41Msg[118] = 0x33; //m_szDataMate3000_41Msg[119] = 0x35; //m_szDataMate3000_41Msg[110] = 0x2E; //m_szDataMate3000_41Msg[121] = 0x35; //m_szDataMate3000_41Msg[122] = 0x20; //m_szDataMate3000_41Msg[123] = 0x4C; //m_szDataMate3000_41Msg[124] = 0x4D; //m_szDataMate3000_41Msg[125] = 0x0D; //m_szDataMate3000_41Msg[126] = 0x32; //m_szDataMate3000_41Msg[127] = 0x0D; #endif } //char *CDataMate3000::GetChkSum(char Msg[VAR_MSG]) //{ // WORD iMsg; // char iChk[4]; // int len; // // len = sizeof(Msg); //取出数据长度 lenID/2 // // iMsg = 0; // for (int i=1;i<=(len-6);i++) // { // iMsg += Msg[i]; //ASSIC码值相加 // } // iMsg = iMsg%65536; // iMsg = ~iMsg + 1; // // itoa(iMsg,iChk,16); // // return iChk; //} // //BOOL CDataMate3000::ChkSumCheck(char Msg[VAR_MSG]) //{ // WORD iMsg; // char iChk[4]; // int len; // // len = sizeof(Msg); //取出数据长度 lenID/2 // // iMsg = 0; // for (int i=1;i<=(len-6);i++) // { // iMsg += Msg[i]; //ASSIC码值相加 // } // iMsg = iMsg%65536; // iMsg = ~iMsg + 1; // // itoa(iMsg,iChk,16); // // if ((lowercase2uppercase(iChk[0])==Msg[len-5]) && (lowercase2uppercase(iChk[1]) == Msg[len-4]) // && (lowercase2uppercase(iChk[2]) == Msg[len-3]) && (lowercase2uppercase(iChk[3]) == Msg[len-2])) // { // return TRUE; // } // else // { // return FALSE; // } //} // //BOOL CDataMate3000::CheckLength(char Msg[VAR_MSG]) //{ // WORD len ,index , lenID; // len = 4; // index = 9; // char ch[4]; // // for (int i=0;i