SerialPortManager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "SerialPortManager.h"
  2. #include <Shlwapi.h>
  3. #pragma comment(lib, "Shlwapi.lib")
  4. #define CMD_SIZE 6
  5. #define START_APP_EVENT_NAME _T("DSD_EVENT_START_APP")
  6. char cmd_receive_start_app[6]{ 0xAA, 0x06, 0x11, 0x01, 0x94, 0xDE };
  7. char cmd_send_started_app[5]{ 0xAB, 0x05, 0x0A, 0xDF, 0x4E };
  8. char cmd_send_close_app[6]{ 0xAA, 0x06, 0x11, 0x02, 0xA4, 0xBD };
  9. SerialPortSlot::SerialPortSlot(CSerialPort& serialPort, HANDLE startAPPEvent)
  10. : has_slots<>()
  11. , m_hStartAPPEvent(startAPPEvent)
  12. {
  13. m_serialPort = serialPort;
  14. }
  15. void SerialPortSlot::onMessage()
  16. {
  17. char szBuf[CMD_SIZE] = { 0 };
  18. int nRecv = m_serialPort.readData(szBuf, sizeof(szBuf));
  19. printf("Receive %d bytes message from client:\n", nRecv);
  20. printMessage(szBuf, nRecv);
  21. handleMessage(szBuf, nRecv);
  22. }
  23. int SerialPortSlot::handleMessage(const char* szContent, int nRecv)
  24. {
  25. int ret = 1;
  26. if (nRecv != CMD_SIZE)
  27. {
  28. printf("Receive message size:%d is not a command size.\n", nRecv);
  29. return 1;
  30. }
  31. if (strncmp(cmd_receive_start_app, szContent, sizeof(cmd_receive_start_app)) == 0)
  32. {
  33. printf("Received start app command.\n");
  34. ret = onStartAPP();
  35. }
  36. return ret;
  37. }
  38. int SerialPortSlot::onStartAPP()
  39. {
  40. SetEvent(m_hStartAPPEvent);
  41. return 0;
  42. }
  43. void SerialPortSlot::printMessage(const char* szData, int size)
  44. {
  45. for (int i = 0; i < size; i++)
  46. printf("%02X ", (unsigned char)szData[i]);
  47. printf("\n");
  48. }
  49. //------------------------------------------------------------------------------------
  50. SerialPortManager::SerialPortManager()
  51. : m_serialPort(nullptr)
  52. , m_serialPortSlot(nullptr)
  53. , m_hStartEvent(NULL)
  54. {
  55. }
  56. SerialPortManager::~SerialPortManager()
  57. {
  58. release();
  59. }
  60. int SerialPortManager::initialize()
  61. {
  62. int ret = 0;
  63. if (m_serialPort)
  64. delete m_serialPort;
  65. m_serialPort = nullptr;
  66. /*
  67. std::list<std::string> listAvailablePorts;
  68. listAvailablePorts = CSerialPortInfo::availablePorts();
  69. for (auto port : listAvailablePorts)
  70. {
  71. m_serialPort = new CSerialPort();
  72. m_serialPort->init(port.c_str(), 9600);
  73. if (!m_serialPort->open())
  74. {
  75. delete m_serialPort;
  76. continue;
  77. }
  78. else
  79. break;
  80. }
  81. */
  82. std::vector<SerialPortInfo> vecAvailablePorts;
  83. vecAvailablePorts = CSerialPortInfo::availablePortInfos();
  84. for (auto port : vecAvailablePorts)
  85. {
  86. m_serialPort = new CSerialPort();
  87. m_serialPort->init(port.portName.c_str(), 9600);
  88. if (!m_serialPort->open())
  89. {
  90. delete m_serialPort;
  91. continue;
  92. }
  93. else
  94. break;
  95. }
  96. if (m_serialPort == nullptr || !m_serialPort->isOpened())
  97. return -1;
  98. if (m_serialPortSlot)
  99. delete m_serialPortSlot;
  100. if ((ret = initEvent()) != 0)
  101. {
  102. printf("Init event failed! ret=%d\n", ret);
  103. return ret;
  104. }
  105. m_serialPortSlot = new SerialPortSlot(*m_serialPort, m_hStartEvent);
  106. m_serialPort->readReady.connect(m_serialPortSlot, &SerialPortSlot::onMessage);
  107. return ret;
  108. }
  109. void SerialPortManager::release()
  110. {
  111. if (m_serialPort)
  112. delete m_serialPort;
  113. m_serialPort = nullptr;
  114. CloseHandle(m_hStartEvent);
  115. m_hStartEvent = NULL;
  116. }
  117. #include "Assist.h"
  118. int SerialPortManager::listenCOMandStartApp(const TCHAR* szAppPath, const TCHAR* szAppArgs)
  119. {
  120. int ret = 0;
  121. while (true)
  122. {
  123. DWORD dwRet = WaitForSingleObject(m_hStartEvent, INFINITE);
  124. if (dwRet == WAIT_OBJECT_0)
  125. {
  126. ResetEvent(m_hStartEvent);
  127. if ((ret = startAPP(szAppPath, szAppArgs)) != 0)
  128. Assist::WriteTextLog(_T("Error, startAPP failed! ret=%d"), ret);
  129. else
  130. Assist::WriteTextLog(_T("Success, startAPP finished!\n"));
  131. if ((ret = writeData(cmd_send_started_app, sizeof(cmd_send_started_app))) != 0)
  132. Assist::WriteTextLog(_T("Error, Respond failed! ret=%d\n"), ret);
  133. else
  134. Assist::WriteTextLog(_T("Respond success!\n"));
  135. }
  136. else
  137. {
  138. Assist::WriteTextLog(_T("Wait for start app event error! dwRet=%ld"), dwRet);
  139. }
  140. }
  141. return ret;
  142. }
  143. int SerialPortManager::sendStartedApp()
  144. {
  145. return writeData(cmd_send_started_app, sizeof(cmd_send_started_app));
  146. }
  147. int SerialPortManager::sendCloseApp()
  148. {
  149. return writeData(cmd_send_close_app, sizeof(cmd_send_close_app));
  150. }
  151. int SerialPortManager::writeData(const char* szBuf, int size)
  152. {
  153. int ret = 0;
  154. int nWrite = m_serialPort->writeData(szBuf, size);
  155. if (nWrite == -1)
  156. ret = -1;
  157. else if (nWrite != size)
  158. ret = -2;
  159. return ret;
  160. }
  161. int SerialPortManager::initEvent()
  162. {
  163. if(m_hStartEvent)
  164. CloseHandle(m_hStartEvent);
  165. m_hStartEvent = NULL;
  166. m_hStartEvent = CreateEvent(NULL, FALSE, FALSE, START_APP_EVENT_NAME);
  167. if (m_hStartEvent || GetLastError() == ERROR_ALREADY_EXISTS)
  168. return 0;
  169. else
  170. return -1;
  171. }
  172. int SerialPortManager::startAPP(const TCHAR* szAppPath, const TCHAR* szAppArgs)
  173. {
  174. if (!PathFileExists(szAppPath))
  175. {
  176. printf("Error, %ls path not exists!\n", szAppPath);
  177. return -1;
  178. }
  179. SHELLEXECUTEINFO ShExecInfo;
  180. ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  181. ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  182. ShExecInfo.hwnd = NULL;
  183. ShExecInfo.lpVerb = _T("open");
  184. ShExecInfo.lpFile = szAppPath;
  185. ShExecInfo.lpParameters = szAppArgs;
  186. ShExecInfo.lpDirectory = NULL;
  187. ShExecInfo.nShow = SW_SHOW;
  188. ShExecInfo.hInstApp = NULL;
  189. if (ShellExecuteEx(&ShExecInfo))
  190. {
  191. CloseHandle(ShExecInfo.hProcess);
  192. return 0;
  193. }
  194. return -2;
  195. }