Inspired by answer by Yangghi Min on https://social.msdn.microsoft.com/Forums/vstudio/en-US/588b7967-cce4-4412-87e8-593dfeb228b4/getting-the-list-of-available-serial-ports-in-c
| 39 | // Inspired by answer by Yangghi Min on |
| 40 | // https://social.msdn.microsoft.com/Forums/vstudio/en-US/588b7967-cce4-4412-87e8-593dfeb228b4/getting-the-list-of-available-serial-ports-in-c |
| 41 | inline SerialPortState getSerialPortStateImpl(std::string const &port) { |
| 42 | GetLastError(); // clear last error |
| 43 | HANDLE hCom = CreateFile(port.c_str(), GENERIC_READ | GENERIC_WRITE, |
| 44 | 0, // non-sharing mode |
| 45 | nullptr, // ignore security |
| 46 | OPEN_EXISTING, // required for com ports |
| 47 | 0, // just basic open for existence test. |
| 48 | nullptr); |
| 49 | |
| 50 | /// auto-close handle. |
| 51 | BOOST_SCOPE_EXIT(&hCom) { CloseHandle(hCom); } |
| 52 | BOOST_SCOPE_EXIT_END |
| 53 | |
| 54 | if (INVALID_HANDLE_VALUE == hCom) { |
| 55 | auto err = GetLastError(); |
| 56 | if (ERROR_ACCESS_DENIED == err) { |
| 57 | return SERIAL_BUSY; |
| 58 | } |
| 59 | return SERIAL_MISSING; |
| 60 | } |
| 61 | return SERIAL_AVAILABLE; |
| 62 | } |
| 63 | #else |
| 64 | |
| 65 | inline SerialPortState getSerialPortStateImpl(std::string const &port) { |
no outgoing calls
no test coverage detected