Device.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "StdAfx.h"
  2. #include "Device.h"
  3. #include <regex>
  4. std::map<int, CDevice*> g_dmap;
  5. CDevice::CDevice()
  6. {
  7. }
  8. CDevice::~CDevice()
  9. {
  10. m_obj.CloseSerialPort();
  11. }
  12. bool CDevice::Open()
  13. {
  14. return m_obj.OpenSerialPort(m_iPort, m_iBaudrate, m_iDatabit, m_iParitybit, m_iStopbit, 0, 0);
  15. }
  16. void CDevice::Close()
  17. {
  18. m_obj.CloseSerialPort();
  19. }
  20. bool CDevice::IsOpen()
  21. {
  22. return m_obj.IsOpen();
  23. }
  24. std::string CDevice::SendCommond(std::string cmd)
  25. {
  26. if ( !m_obj.IsOpen() )
  27. return std::string("串口未打开成功");
  28. DWORD dwRet = m_obj.WriteComm((byte*)cmd.data(), cmd.size());
  29. if ( dwRet != cmd.size() )
  30. return std::string("写串口失败");
  31. // 如果是tim和ptn同时运行;
  32. std::string pattern{ "run tim (\\d{1,3});run ptn (\\d{1,3});\r" }; // fixed telephone
  33. std::regex re(pattern);
  34. WORD timeout = 300;
  35. if (std::regex_match(cmd, re))
  36. timeout = 1000;
  37. byte szData[1024 * 4] = {0};
  38. dwRet = m_obj.ReadComm(szData, 1024 * 4, timeout);
  39. if (dwRet == 0 )
  40. return std::string("读串口失败");
  41. return std::string((char*)szData, dwRet);
  42. }