| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: SK6000.cpp //////
- ////// 作 者: Suguobing //////
- ////// 创建时间: //////
- ////// 说 明: 氰气检测协议 //////
- ////// //////
- ////// 修改时间:2010-11-27 //////
- ////// 修改说明: //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommProcess.h"
- #include <math.h>
- #include "SK6000.h"
- CSk6000::CSk6000( 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));
- for( int i = 0; i < MAX_ADDR; i++ )
- {
- memset(m_szSk_Msg[i], 0, sizeof(m_szSk_Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CSk6000::~CSk6000()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection(& m_csReadMsg);
- #endif
- MTVERIFY(CloseHandle(m_hSemComm));
- CloseComm();
- }
- BOOL CSk6000::SkOpenComm(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 CSk6000::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]
- )
- {
- //LOG4C((LOG_NOTICE, "进入SK6000"));
- int nIndex = 0, nLen = 0;
- char szSendMsg[SK_SEND_MSG] = {0};
- char szCid[SK_SEND_MSG] = {0};
- char szType[TYPE_LENGTH] = {0};
- int startBit = 0;
- int endBit = 0;
- GetSkFromIni(szPath, szIniName, szCmd, szCid, szType, nIndex, nLen, startBit, endBit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
-
- if ( strlen(m_szSk_Msg[nAddr - 1]) == 0 &&
- (strcmp(szCmd, "cmd-1") == 0 ) ||
- strcmp(szCmd, "cmd-1") == 0 )//UPS工作状态及传送顺序
- {
-
- nRet = GetDeviceParam(nAddr, pComm, 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 = GetSkMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- return nRet;
- }
- int CSk6000::GetSkMsg(int nAddr, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit)
- {
- int nRet = 0;
- if (strcmp(szCmd, "cmd-1") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- GetData(szType, m_szSk_Msg[nAddr - 1] , szMsg);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSk6000::GetDeviceParam(
- int nAddr,
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[SK_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(nAddr, pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- int CSk6000::GetSkFromIni( char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char szSanTakSendMsg[MAX_CMD], //发送Buffer
- char nType[MAX_CMD],
- int &nIndex,
- int &nLen,
- int &startBit,
- int &endBit)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- TRACE("szFile =%s\n",szFile);
- GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg, 10, szFile);
- szSanTakSendMsg[strlen(szSanTakSendMsg)] = 0x0D;
- GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
- nType[strlen(nType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- return 0;
- }
- int CSk6000::RequestStatus(int nAddr, CCommProcess *pComm, char chSendMsg[SK_SEND_MSG] )
- {
- #if DEBUG_SK //开关量,用于测试模拟数据
- char SendMsg[1];
- SendMsg[0] = nAddr | 0x80;
- if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
- {
- int nDatalen = 1 ;//(int)strlen(chSendMsg);
- ResetEvent(m_hSemComm);
- int nResult = pComm->Write((BYTE *)SendMsg, nDatalen);
- //LOG4C((LOG_NOTICE,"write"));
- if (nResult == nDatalen)
- {
- }
- else
- {
- SetEvent(m_hSemComm);
- return EER_CODE_SK_COM_WRITE_DATA;
- }
- }
- else
- {
- return ERR_CODE_SK_COM_BUSY; //串口忙
- }
- #endif
- return 0;
- }
- int CSk6000::ResponseStatus(
- int nAddr,
- CCommProcess *pComm,
- char szCmd[MAX_CMD],
- char szMsg[VAR_MSG],
- int &nIndex,
- int &nLen,
- char szType[TYPE_LENGTH]
- )
- {
- #if DEBUG_SK
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- RESPONSE_STRUCT strcutResponse;
- memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
- nReceiveLen = sizeof(RESPONSE_STRUCT);
- 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;
- }
- return ERR_CODE_SK_COM_READ_NO_DATA;
- }
-
- //校验
- if (LengthCheck(pBuff))
- {
- SetEvent(m_hSemComm);
- if (pBuff != NULL)
- {
- delete [] pBuff;
- pBuff = NULL;
- }
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- SetSkMsg(nAddr, szCmd, pBuff);
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuff != NULL)
- {
- delete[] pBuff;
- pBuff = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- void CSk6000::SetSkMsg(int nAddr, char szCmd[MAX_CMD], char *pBuff)
- {
- if (strcmp(szCmd, "cmd-1") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection(&m_csReadMsg);
- #endif
- memcpy(m_szSk_Msg[nAddr - 1], pBuff, sizeof(m_szSk_Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSk6000::SimulationCommData(int nAddr)
- {
- m_szSk_Msg[nAddr - 1][0] = 0x01;
- m_szSk_Msg[nAddr - 1][1] = 0x07;
- m_szSk_Msg[nAddr - 1][2] = 0x68;
- m_szSk_Msg[nAddr - 1][3] = 0x70;
- }
- int CSk6000::SendSetReuest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szMsg[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 CSk6000::LengthCheck(char szMsg[])
- {
- char ch;
- ch = szMsg[0] + szMsg[1] + szMsg[2];
- //ch &= 0x7F;
- if (ch == szMsg[3])
- {
- return 0;
- }
- else
- return 1;
- }
- int CSk6000::GetData(char *szType, char *szMsg, char *szConvMsg)
- {
- int sum = 0;
- CString sTotal;
- itoa(szMsg[1],szConvMsg,2);
- sTotal = szConvMsg;
- memset(szConvMsg,0,10);
- itoa(szMsg[2],szConvMsg,2);
- sTotal += szConvMsg;
- char a;
- int Len = sTotal.GetLength();
- for (int i = 0; i < Len; i++)
- {
- a = sTotal.GetAt(i);
- sum += (a-48) * static_cast<int>(pow(2.0,Len-i-1));
- }
- sprintf(szConvMsg, "%d", sum);
-
- return 0;
- }
|