24 #include <winpr/comm.h>
25 #include <winpr/crt.h>
29 int TestControlSettings(
int argc,
char* argv[])
31 struct stat statbuf = { 0 };
36 if (stat(
"/dev/ttyS0", &statbuf) < 0)
38 (void)fprintf(stderr,
"/dev/ttyS0 not available, making the test to succeed though\n");
42 result = DefineCommDevice(
"COM1",
"/dev/ttyS0");
45 (void)fprintf(stderr,
"DefineCommDevice failure: 0x%x\n", GetLastError());
49 hComm = CreateFile(
"COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
50 if (hComm == INVALID_HANDLE_VALUE)
52 (void)fprintf(stderr,
"CreateFileA failure: 0x%x\n", GetLastError());
56 ZeroMemory(&dcb,
sizeof(
DCB));
57 dcb.DCBlength =
sizeof(
DCB);
58 if (!GetCommState(hComm, &dcb))
60 (void)fprintf(stderr,
"GetCommState failure; GetLastError(): %08x\n", GetLastError());
67 dcb.StopBits = ONESTOPBIT;
68 dcb.Parity = MARKPARITY;
70 if (!SetCommState(hComm, &dcb))
72 (void)fprintf(stderr,
"SetCommState failure; GetLastError(): %08x\n", GetLastError());
76 ZeroMemory(&dcb,
sizeof(
DCB));
77 dcb.DCBlength =
sizeof(
DCB);
78 if (!GetCommState(hComm, &dcb))
80 (void)fprintf(stderr,
"GetCommState failure; GetLastError(): %08x\n", GetLastError());
84 if ((dcb.ByteSize != 5) || (dcb.StopBits != ONESTOPBIT) || (dcb.Parity != MARKPARITY))
86 (void)fprintf(stderr,
"test1 failed.\n");
93 dcb.StopBits = ONESTOPBIT;
94 dcb.Parity = NOPARITY;
96 if (!SetCommState(hComm, &dcb))
98 (void)fprintf(stderr,
"SetCommState failure; GetLastError(): %08x\n", GetLastError());
102 ZeroMemory(&dcb,
sizeof(
DCB));
103 dcb.DCBlength =
sizeof(
DCB);
104 if (!GetCommState(hComm, &dcb))
106 (void)fprintf(stderr,
"GetCommState failure; GetLastError(): %08x\n", GetLastError());
110 if ((dcb.ByteSize != 8) || (dcb.StopBits != ONESTOPBIT) || (dcb.Parity != NOPARITY))
112 (void)fprintf(stderr,
"test2 failed.\n");
116 if (!CloseHandle(hComm))
118 (void)fprintf(stderr,
"CloseHandle failure, GetLastError()=%08x\n", GetLastError());