1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- #include "StdAfx.h"
- #include "Device.h"
- #include <regex>
- std::map<int, CDevice*> g_dmap;
- CDevice::CDevice()
- {
- }
- CDevice::~CDevice()
- {
- m_obj.CloseSerialPort();
- }
- bool CDevice::Open()
- {
- return m_obj.OpenSerialPort(m_iPort, m_iBaudrate, m_iDatabit, m_iParitybit, m_iStopbit, 0, 0);
- }
- void CDevice::Close()
- {
- m_obj.CloseSerialPort();
- }
- bool CDevice::IsOpen()
- {
- return m_obj.IsOpen();
- }
- std::string CDevice::SendCommond(std::string cmd)
- {
- if ( !m_obj.IsOpen() )
- return std::string("串口未打开成功");
- DWORD dwRet = m_obj.WriteComm((byte*)cmd.data(), cmd.size());
- if ( dwRet != cmd.size() )
- return std::string("写串口失败");
- // 如果是tim和ptn同时运行;
- std::string pattern{ "run tim (\\d{1,3});run ptn (\\d{1,3});\r" }; // fixed telephone
- std::regex re(pattern);
- WORD timeout = 300;
- if (std::regex_match(cmd, re))
- timeout = 1000;
-
- byte szData[1024 * 4] = {0};
- dwRet = m_obj.ReadComm(szData, 1024 * 4, timeout);
- if (dwRet == 0 )
- return std::string("读串口失败");
- return std::string((char*)szData, dwRet);
- }
|