| 1 |
|
| 2 |
#include <winpr/crt.h> |
| 3 |
#include <winpr/comm.h> |
| 4 |
#include <winpr/file.h> |
| 5 |
#include <winpr/synch.h> |
| 6 |
#include <winpr/handle.h> |
| 7 |
|
| 8 |
int TestCommMonitor(int argc, char* argv[]) |
| 9 |
{ |
| 10 |
HANDLE hComm = nullptr; |
| 11 |
DWORD dwError = 0; |
| 12 |
BOOL fSuccess = 0; |
| 13 |
DWORD dwEvtMask = 0; |
| 14 |
OVERLAPPED overlapped = WINPR_C_ARRAY_INIT; |
| 15 |
LPCSTR lpFileName = "\\\\.\\COM1"; |
| 16 |
|
| 17 |
hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, |
| 18 |
FILE_FLAG_OVERLAPPED, nullptr); |
| 19 |
|
| 20 |
if (!hComm || (hComm == INVALID_HANDLE_VALUE)) |
| 21 |
{ |
| 22 |
printf("CreateFileA failure: %s\n", lpFileName); |
| 23 |
return -1; |
| 24 |
} |
| 25 |
|
| 26 |
fSuccess = SetCommMask(hComm, EV_CTS | EV_DSR); |
| 27 |
|
| 28 |
if (!fSuccess) |
| 29 |
{ |
| 30 |
printf("SetCommMask failure: GetLastError() = %" PRIu32 "\n", GetLastError()); |
| 31 |
return -1; |
| 32 |
} |
| 33 |
|
| 34 |
if (!(overlapped.hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr))) |
| 35 |
{ |
| 36 |
printf("CreateEvent failed: GetLastError() = %" PRIu32 "\n", GetLastError()); |
| 37 |
return -1; |
| 38 |
} |
| 39 |
|
| 40 |
if (WaitCommEvent(hComm, &dwEvtMask, &overlapped)) |
| 41 |
{ |
| 42 |
if (dwEvtMask & EV_DSR) |
| 43 |
{ |
| 44 |
printf("EV_DSR\n"); |
| 45 |
} |
| 46 |
|
| 47 |
if (dwEvtMask & EV_CTS) |
| 48 |
{ |
| 49 |
printf("EV_CTS\n"); |
| 50 |
} |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
dwError = GetLastError(); |
| 55 |
|
| 56 |
if (dwError == ERROR_IO_PENDING) |
| 57 |
{ |
| 58 |
printf("ERROR_IO_PENDING\n"); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
printf("WaitCommEvent failure: GetLastError() = %" PRIu32 "\n", dwError); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
(void)CloseHandle(hComm); |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |