123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- #include "SerialPortManager.h"
- #include <Shlwapi.h>
- #pragma comment(lib, "Shlwapi.lib")
- #define CMD_SIZE 6
- #define START_APP_EVENT_NAME _T("DSD_EVENT_START_APP")
- char cmd_receive_start_app[6]{ 0xAA, 0x06, 0x11, 0x01, 0x94, 0xDE };
- char cmd_send_started_app[5]{ 0xAB, 0x05, 0x0A, 0xDF, 0x4E };
- char cmd_send_close_app[6]{ 0xAA, 0x06, 0x11, 0x02, 0xA4, 0xBD };
- SerialPortSlot::SerialPortSlot(CSerialPort& serialPort, HANDLE startAPPEvent)
- : has_slots<>()
- , m_hStartAPPEvent(startAPPEvent)
- {
- m_serialPort = serialPort;
- }
- void SerialPortSlot::onMessage()
- {
- char szBuf[CMD_SIZE] = { 0 };
- int nRecv = m_serialPort.readData(szBuf, sizeof(szBuf));
- printf("Receive %d bytes message from client:\n", nRecv);
- printMessage(szBuf, nRecv);
- handleMessage(szBuf, nRecv);
- }
- int SerialPortSlot::handleMessage(const char* szContent, int nRecv)
- {
- int ret = 1;
- if (nRecv != CMD_SIZE)
- {
- printf("Receive message size:%d is not a command size.\n", nRecv);
- return 1;
- }
- if (strncmp(cmd_receive_start_app, szContent, sizeof(cmd_receive_start_app)) == 0)
- {
- printf("Received start app command.\n");
- ret = onStartAPP();
- }
-
- return ret;
- }
- int SerialPortSlot::onStartAPP()
- {
- SetEvent(m_hStartAPPEvent);
- return 0;
- }
- void SerialPortSlot::printMessage(const char* szData, int size)
- {
- for (int i = 0; i < size; i++)
- printf("%02X ", (unsigned char)szData[i]);
- printf("\n");
- }
- //------------------------------------------------------------------------------------
- SerialPortManager::SerialPortManager()
- : m_serialPort(nullptr)
- , m_serialPortSlot(nullptr)
- , m_hStartEvent(NULL)
- {
- }
- SerialPortManager::~SerialPortManager()
- {
- release();
- }
- int SerialPortManager::initialize()
- {
- int ret = 0;
- if (m_serialPort)
- delete m_serialPort;
- m_serialPort = nullptr;
- /*
- std::list<std::string> listAvailablePorts;
- listAvailablePorts = CSerialPortInfo::availablePorts();
- for (auto port : listAvailablePorts)
- {
- m_serialPort = new CSerialPort();
- m_serialPort->init(port.c_str(), 9600);
- if (!m_serialPort->open())
- {
- delete m_serialPort;
- continue;
- }
- else
- break;
- }
- */
- std::vector<SerialPortInfo> vecAvailablePorts;
- vecAvailablePorts = CSerialPortInfo::availablePortInfos();
- for (auto port : vecAvailablePorts)
- {
- m_serialPort = new CSerialPort();
- m_serialPort->init(port.portName.c_str(), 9600);
- if (!m_serialPort->open())
- {
- delete m_serialPort;
- continue;
- }
- else
- break;
- }
- if (m_serialPort == nullptr || !m_serialPort->isOpened())
- return -1;
- if (m_serialPortSlot)
- delete m_serialPortSlot;
- if ((ret = initEvent()) != 0)
- {
- printf("Init event failed! ret=%d\n", ret);
- return ret;
- }
- m_serialPortSlot = new SerialPortSlot(*m_serialPort, m_hStartEvent);
- m_serialPort->readReady.connect(m_serialPortSlot, &SerialPortSlot::onMessage);
- return ret;
- }
- void SerialPortManager::release()
- {
- if (m_serialPort)
- delete m_serialPort;
- m_serialPort = nullptr;
- CloseHandle(m_hStartEvent);
- m_hStartEvent = NULL;
- }
- #include "Assist.h"
- int SerialPortManager::listenCOMandStartApp(const TCHAR* szAppPath, const TCHAR* szAppArgs)
- {
- int ret = 0;
- while (true)
- {
- DWORD dwRet = WaitForSingleObject(m_hStartEvent, INFINITE);
- if (dwRet == WAIT_OBJECT_0)
- {
- ResetEvent(m_hStartEvent);
- if ((ret = startAPP(szAppPath, szAppArgs)) != 0)
- Assist::WriteTextLog(_T("Error, startAPP failed! ret=%d"), ret);
- else
- Assist::WriteTextLog(_T("Success, startAPP finished!\n"));
- if ((ret = writeData(cmd_send_started_app, sizeof(cmd_send_started_app))) != 0)
- Assist::WriteTextLog(_T("Error, Respond failed! ret=%d\n"), ret);
- else
- Assist::WriteTextLog(_T("Respond success!\n"));
- }
- else
- {
- Assist::WriteTextLog(_T("Wait for start app event error! dwRet=%ld"), dwRet);
- }
- }
- return ret;
- }
- int SerialPortManager::sendStartedApp()
- {
- return writeData(cmd_send_started_app, sizeof(cmd_send_started_app));
- }
- int SerialPortManager::sendCloseApp()
- {
- return writeData(cmd_send_close_app, sizeof(cmd_send_close_app));
- }
- int SerialPortManager::writeData(const char* szBuf, int size)
- {
- int ret = 0;
- int nWrite = m_serialPort->writeData(szBuf, size);
- if (nWrite == -1)
- ret = -1;
- else if (nWrite != size)
- ret = -2;
- return ret;
- }
- int SerialPortManager::initEvent()
- {
- if(m_hStartEvent)
- CloseHandle(m_hStartEvent);
- m_hStartEvent = NULL;
- m_hStartEvent = CreateEvent(NULL, FALSE, FALSE, START_APP_EVENT_NAME);
- if (m_hStartEvent || GetLastError() == ERROR_ALREADY_EXISTS)
- return 0;
- else
- return -1;
- }
- int SerialPortManager::startAPP(const TCHAR* szAppPath, const TCHAR* szAppArgs)
- {
- if (!PathFileExists(szAppPath))
- {
- printf("Error, %ls path not exists!\n", szAppPath);
- return -1;
- }
- SHELLEXECUTEINFO ShExecInfo;
- ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
- ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
- ShExecInfo.hwnd = NULL;
- ShExecInfo.lpVerb = _T("open");
- ShExecInfo.lpFile = szAppPath;
- ShExecInfo.lpParameters = szAppArgs;
- ShExecInfo.lpDirectory = NULL;
- ShExecInfo.nShow = SW_SHOW;
- ShExecInfo.hInstApp = NULL;
- if (ShellExecuteEx(&ShExecInfo))
- {
- CloseHandle(ShExecInfo.hProcess);
- return 0;
- }
-
- return -2;
- }
|