24#include <winpr/comm.h>
25#include <winpr/file.h>
26#include <winpr/synch.h>
27#include <winpr/handle.h>
29int TestCommConfig(
int argc,
char* argv[])
31 DCB dcb = WINPR_C_ARRAY_INIT;
33 LPCSTR lpFileName =
"\\\\.\\COM1";
34 COMMPROP commProp = WINPR_C_ARRAY_INIT;
35 struct stat statbuf = WINPR_C_ARRAY_INIT;
37 HANDLE hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0,
nullptr, OPEN_EXISTING,
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,
nullptr, OPEN_EXISTING, 0,
76 if (!hComm || (hComm == INVALID_HANDLE_VALUE))
78 (void)fprintf(stderr,
"CreateFileA failure: %s GetLastError() = 0x%08x\n", lpFileName,
86 dcb.DCBlength =
sizeof(
DCB);
87 success = GetCommState(hComm, &dcb);
90 (void)fprintf(stderr,
"GetCommState failure: GetLastError() = Ox%x\n", GetLastError());
95 "BaudRate: %" PRIu32
" ByteSize: %" PRIu8
" Parity: %" PRIu8
" StopBits: %" PRIu8
97 dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
99 if (!GetCommProperties(hComm, &commProp))
101 (void)fprintf(stderr,
"GetCommProperties failure: GetLastError(): 0x%08x\n",
106 if ((commProp.dwSettableBaud & BAUD_57600) <= 0)
108 (void)fprintf(stderr,
"BAUD_57600 unsupported!\n");
112 if ((commProp.dwSettableBaud & BAUD_14400) > 0)
114 (void)fprintf(stderr,
"BAUD_14400 supported!\n");
118 dcb.BaudRate = CBR_57600;
120 dcb.Parity = NOPARITY;
121 dcb.StopBits = ONESTOPBIT;
123 success = SetCommState(hComm, &dcb);
127 (void)fprintf(stderr,
"SetCommState failure: GetLastError() = 0x%x\n", GetLastError());
131 success = GetCommState(hComm, &dcb);
135 (void)fprintf(stderr,
"GetCommState failure: GetLastError() = 0x%x\n", GetLastError());
139 if ((dcb.BaudRate != CBR_57600) || (dcb.ByteSize != 8) || (dcb.Parity != NOPARITY) ||
140 (dcb.StopBits != ONESTOPBIT))
142 (void)fprintf(stderr,
143 "Got an unexpected value among: BaudRate: %" PRIu32
" ByteSize: %" PRIu8
144 " Parity: %" PRIu8
" StopBits: %" PRIu8
"\n",
145 dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
148 (void)CloseHandle(hComm);