23 #include <winpr/comm.h>
24 #include <winpr/crt.h>
28 static void init_empty_dcb(
DCB* pDcb)
32 ZeroMemory(pDcb,
sizeof(
DCB));
33 pDcb->DCBlength =
sizeof(
DCB);
38 static BOOL test_fParity(HANDLE hComm)
44 result = GetCommState(hComm, &dcb);
47 (void)fprintf(stderr,
"GetCommState failure: 0x%08" PRIx32
"\n", GetLastError());
53 result = SetCommState(hComm, &dcb);
56 (void)fprintf(stderr,
"SetCommState failure: 0x%08" PRIx32
"\n", GetLastError());
61 result = GetCommState(hComm, &dcb);
64 (void)fprintf(stderr,
"GetCommState failure: 0x%08" PRIx32
"\n", GetLastError());
70 (void)fprintf(stderr,
"unexpected fParity: %" PRIu32
" instead of TRUE\n", dcb.fParity);
76 result = SetCommState(hComm, &dcb);
79 (void)fprintf(stderr,
"SetCommState failure: 0x%08" PRIx32
"\n", GetLastError());
84 result = GetCommState(hComm, &dcb);
87 (void)fprintf(stderr,
"GetCommState failure: 0x%08" PRIx32
"\n", GetLastError());
93 (void)fprintf(stderr,
"unexpected fParity: %" PRIu32
" instead of FALSE\n", dcb.fParity);
99 result = SetCommState(hComm, &dcb);
102 (void)fprintf(stderr,
"SetCommState failure: 0x%08" PRIx32
"\n", GetLastError());
106 init_empty_dcb(&dcb);
107 result = GetCommState(hComm, &dcb);
110 (void)fprintf(stderr,
"GetCommState failure: 0x%08" PRIx32
"\n", GetLastError());
116 (void)fprintf(stderr,
"unexpected fParity: %" PRIu32
" instead of TRUE\n", dcb.fParity);
123 static BOOL test_SerialSys(HANDLE hComm)
128 init_empty_dcb(&dcb);
129 result = GetCommState(hComm, &dcb);
132 (void)fprintf(stderr,
"GetCommState failure: 0x%x\n", GetLastError());
137 dcb.BaudRate = CBR_115200;
138 result = SetCommState(hComm, &dcb);
141 (void)fprintf(stderr,
"SetCommState failure: 0x%08x\n", GetLastError());
145 init_empty_dcb(&dcb);
146 result = GetCommState(hComm, &dcb);
149 (void)fprintf(stderr,
"GetCommState failure: 0x%x\n", GetLastError());
152 if (dcb.BaudRate != CBR_115200)
154 (void)fprintf(stderr,
"SetCommState failure: could not set BaudRate=%d (CBR_115200)\n",
161 dcb.BaudRate = CBR_57600;
162 result = SetCommState(hComm, &dcb);
165 (void)fprintf(stderr,
"SetCommState failure: 0x%x\n", GetLastError());
169 init_empty_dcb(&dcb);
170 result = GetCommState(hComm, &dcb);
173 (void)fprintf(stderr,
"GetCommState failure: 0x%x\n", GetLastError());
176 if (dcb.BaudRate != CBR_57600)
178 (void)fprintf(stderr,
"SetCommState failure: could not set BaudRate=%d (CBR_57600)\n",
184 dcb.BaudRate = CBR_128000;
185 result = SetCommState(hComm, &dcb);
188 (void)fprintf(stderr,
189 "SetCommState failure: unexpected support of BaudRate=%d (CBR_128000)\n",
197 static BOOL test_SerCxSys(HANDLE hComm)
200 return test_SerialSys(hComm);
203 static BOOL test_SerCx2Sys(HANDLE hComm)
206 return test_SerialSys(hComm);
209 static BOOL test_generic(HANDLE hComm)
215 init_empty_dcb(&dcb);
216 result = GetCommState(hComm, &dcb);
219 (void)fprintf(stderr,
"GetCommState failure: 0x%x\n", GetLastError());
224 memcpy(&dcb2, &dcb,
sizeof(
DCB));
225 result = SetCommState(hComm, &dcb);
228 (void)fprintf(stderr,
"SetCommState failure: 0x%08x\n", GetLastError());
232 result = GetCommState(hComm, &dcb);
235 (void)fprintf(stderr,
"GetCommState failure: 0x%x\n", GetLastError());
239 if (memcmp(&dcb, &dcb2,
sizeof(
DCB)) != 0)
241 (void)fprintf(stderr,
242 "DCB is different after SetCommState() whereas it should have not changed\n");
251 if (!test_fParity(hComm))
253 (void)fprintf(stderr,
"test_fParity failure\n");
260 int TestSetCommState(
int argc,
char* argv[])
262 struct stat statbuf = { 0 };
266 if (stat(
"/dev/ttyS0", &statbuf) < 0)
268 (void)fprintf(stderr,
"/dev/ttyS0 not available, making the test to succeed though\n");
272 result = DefineCommDevice(
"COM1",
"/dev/ttyS0");
275 (void)fprintf(stderr,
"DefineCommDevice failure: 0x%x\n", GetLastError());
279 hComm = CreateFile(
"COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
280 if (hComm == INVALID_HANDLE_VALUE)
282 (void)fprintf(stderr,
"CreateFileA failure: 0x%x\n", GetLastError());
286 if (!test_generic(hComm))
288 (void)fprintf(stderr,
"test_generic failure (SerialDriverUnknown)\n");
292 _comm_setServerSerialDriver(hComm, SerialDriverSerialSys);
293 if (!test_generic(hComm))
295 (void)fprintf(stderr,
"test_generic failure (SerialDriverSerialSys)\n");
298 if (!test_SerialSys(hComm))
300 (void)fprintf(stderr,
"test_SerialSys failure\n");
304 _comm_setServerSerialDriver(hComm, SerialDriverSerCxSys);
305 if (!test_generic(hComm))
307 (void)fprintf(stderr,
"test_generic failure (SerialDriverSerCxSys)\n");
310 if (!test_SerCxSys(hComm))
312 (void)fprintf(stderr,
"test_SerCxSys failure\n");
316 _comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys);
317 if (!test_generic(hComm))
319 (void)fprintf(stderr,
"test_generic failure (SerialDriverSerCx2Sys)\n");
322 if (!test_SerCx2Sys(hComm))
324 (void)fprintf(stderr,
"test_SerCx2Sys failure\n");
328 if (!CloseHandle(hComm))
330 (void)fprintf(stderr,
"CloseHandle failure, GetLastError()=%08x\n", GetLastError());