📄 monitortcpslave.cpp
字号:
#include "allmgr.h"#include "monitortcpslave.h"const WORD CMonitorTCPSlave::m_wBuffSize = 261;int CMonitorTCPSlave::processMsg(unsigned char b[], // message buffer, starting with prefix unsigned len) // length of incoming messsage // returns length of response{ // if you wish to make your processing dependent upon unit identifier // use b[6]. Many PLC devices will ignore this field, and most clients // default value to zero. However gateways or specialized programs can // use the unit number to indicate what type of precessing is desired // unsigned i; // handle the function codes 3 and 16 switch(b[7]) { case 0x7e: // read channel config { BYTE* pbyTemp = &b[8]; BYTE bySize = 0; for (unsigned i=0; i<m_pAllMgr->m_listMaster.size();i++) { CMAppService* pMas = m_pAllMgr->m_listMaster[i]; SDW((DWORD)(pMas),pbyTemp); bySize += 4; string strName = pMas->GetName(); *pbyTemp++ = strName.length(); bySize += 1; memcpy(pbyTemp,strName.data(),strName.length()); bySize += strName.length(); pbyTemp += strName.length(); } b[5] = bySize + 2; break; } case 0x7c: //Read Channel I/O stream { BYTE* pbyTemp1 = &b[8]; unsigned dwMas = CDW(pbyTemp1); BYTE bySize = 0; for (unsigned i=0; i<m_pAllMgr->m_listMaster.size();i++) { CMAppService* pMas = m_pAllMgr->m_listMaster[i]; if ((DWORD)pMas == dwMas) { BYTE* pbyTemp = &b[8]; CDataNode* pDN = NULL; if (pMas->GetStreamNode(pDN)) { *pbyTemp++ = pDN->m_bDIR ? 1 : 0; bySize += 1; memcpy(pbyTemp,pDN->m_pbyData,((pDN->m_wSize>(m_wBuffSize-9)) ? (m_wBuffSize-9) : pDN->m_wSize)); pbyTemp += pDN->m_wSize; bySize += ((pDN->m_wSize>(m_wBuffSize-9)) ? (m_wBuffSize-9) : pDN->m_wSize); delete pDN; break; } } } b[5] = bySize + 2; break; } default: // generate exception 1 - unknown function code b[7] |= 0x80; b[8] = 1; b[5] = 3; // length break; } // return the total size of the MB/TCP response // notice that bytes 0-4 and 6 will be identical to those of the request return 6+b[5];}bool CMonitorTCPSlave::OnRead(CTcpStream& stTcpStream){ BYTE cBuf[m_wBuffSize]; BYTE* pchBuffer = cBuf; int iReadAvail = stTcpStream.rdbuf()->in_avail(); //05/11/13 int iWriteSpace = stTcpStream.BufSize() - stTcpStream.rdbuf()->out_waiting(); //05/11/13 int iToRead = iReadAvail < iWriteSpace ? iReadAvail : iWriteSpace; int iToRead = iReadAvail; // if ((iToRead > 6) && (iToRead <= 261)) if (iToRead > 0) { stTcpStream.read((char *)pchBuffer, iToRead); int nLen = processMsg(pchBuffer, (unsigned)iToRead); stTcpStream.write((char *)pchBuffer,nLen); } stTcpStream<<flush; return true;}bool CMonitorTCPSlave::OnWrite(CTcpStream& stTcpStream){ return true;}void CMonitorTCPSlave::RunSlave(){ while (true) { m_dwPrev++; try { if (m_bShutdown) break; CheckConnTimeout(); CheckEvent(); OnCheck(); } catch (CException& e) {// if (!OnException(e)) // break; } }}void* MonitorTCPSlave(void *pVoid){ CMonitorTCPSlave *pSlave = (CMonitorTCPSlave *)pVoid; pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL); sleep(2); pSlave->RunSlave(); return 0;}bool CMonitorTCPSlave::Init() { bool bRet = false; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); if (pthread_create(&m_hThread,&attr,MonitorTCPSlave,(void *)this) == 0) bRet = true; else m_hThread = 0; pthread_attr_destroy(&attr); return bRet;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -