Device.cpp 761 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "StdAfx.h"
  2. #include "Device.h"
  3. #include "Device.h"
  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. std::string CDevice::SendCommond(std::string cmd)
  21. {
  22. if ( !m_obj.IsOpen() )
  23. return std::string("串口未打开成功");
  24. DWORD dwRet = m_obj.WriteComm((byte*)cmd.data(), cmd.size());
  25. if ( dwRet != cmd.size() )
  26. return std::string("写串口失败");
  27. byte szData[1024 * 4] = {0};
  28. dwRet = m_obj.ReadComm(szData, 1024 * 4);
  29. if (dwRet == 0 )
  30. return std::string("读串口失败");
  31. return std::string((char*)szData, dwRet);
  32. }