23 #include <winpr/crt.h>
24 #include <winpr/comm.h>
25 #include <winpr/file.h>
26 #include <winpr/synch.h>
27 #include <winpr/handle.h>
29 int TestCommConfig(
int argc,
char* argv[])
33 LPCSTR lpFileName =
"\\\\.\\COM1";
35 struct stat statbuf = { 0 };
38 CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
40 if (hComm && (hComm != INVALID_HANDLE_VALUE))
43 stderr,
"CreateFileA failure: could create a handle on a not yet defined device: %s\n",
48 if (stat(
"/dev/ttyS0", &statbuf) < 0)
50 (void)fprintf(stderr,
"/dev/ttyS0 not available, making the test to succeed though\n");
54 success = DefineCommDevice(lpFileName,
"/dev/ttyS0");
57 (void)fprintf(stderr,
"DefineCommDevice failure: %s\n", lpFileName);
61 hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE,
65 if (hComm != INVALID_HANDLE_VALUE)
68 stderr,
"CreateFileA failure: could create a handle with some invalid parameters %s\n",
73 hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
75 if (!hComm || (hComm == INVALID_HANDLE_VALUE))
77 (void)fprintf(stderr,
"CreateFileA failure: %s GetLastError() = 0x%08x\n", lpFileName,
85 dcb.DCBlength =
sizeof(
DCB);
86 success = GetCommState(hComm, &dcb);
89 (void)fprintf(stderr,
"GetCommState failure: GetLastError() = Ox%x\n", GetLastError());
94 "BaudRate: %" PRIu32
" ByteSize: %" PRIu8
" Parity: %" PRIu8
" StopBits: %" PRIu8
96 dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
98 if (!GetCommProperties(hComm, &commProp))
100 (void)fprintf(stderr,
"GetCommProperties failure: GetLastError(): 0x%08x\n",
105 if ((commProp.dwSettableBaud & BAUD_57600) <= 0)
107 (void)fprintf(stderr,
"BAUD_57600 unsupported!\n");
111 if ((commProp.dwSettableBaud & BAUD_14400) > 0)
113 (void)fprintf(stderr,
"BAUD_14400 supported!\n");
117 dcb.BaudRate = CBR_57600;
119 dcb.Parity = NOPARITY;
120 dcb.StopBits = ONESTOPBIT;
122 success = SetCommState(hComm, &dcb);
126 (void)fprintf(stderr,
"SetCommState failure: GetLastError() = 0x%x\n", GetLastError());
130 success = GetCommState(hComm, &dcb);
134 (void)fprintf(stderr,
"GetCommState failure: GetLastError() = 0x%x\n", GetLastError());
138 if ((dcb.BaudRate != CBR_57600) || (dcb.ByteSize != 8) || (dcb.Parity != NOPARITY) ||
139 (dcb.StopBits != ONESTOPBIT))
141 (void)fprintf(stderr,
142 "Got an unexpeted value among: BaudRate: %" PRIu32
" ByteSize: %" PRIu8
143 " Parity: %" PRIu8
" StopBits: %" PRIu8
"\n",
144 dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
147 (void)CloseHandle(hComm);