:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / apps / tests / sertest / sertest.c
1 #include <windows.h>
2 #include <stdio.h>
3
4 #define BUFSIZE 128
5 #define MAX_PORTNAME_LEN 20
6 #define APP_VERSION_STR "0.01"
7
8 int main(int argc, char *argv[])
9 {
10     CHAR txBuffer[BUFSIZE];
11     CHAR rxBuffer[BUFSIZE];
12     DWORD dwBaud = 9600;
13     DWORD dwNumWritten;
14     DWORD dwNumRead;
15     DWORD dwErrors;
16     DCB dcb;
17     BOOL bResult;
18     HANDLE hPort;
19     int i;
20         int nPortNum = 1;
21
22         TCHAR szPortName[MAX_PORTNAME_LEN];
23
24     if (argc > 1) {
25         //sscanf(argv[1], "%d", &dwBaud);
26         sscanf(argv[1], "%d", &nPortNum);
27     }
28         sprintf(szPortName, _T("COM%d"), nPortNum);
29
30     printf("Serial Port Test Application Version %s\n", APP_VERSION_STR);
31     printf("Attempting to open serial port %d - %s\n", nPortNum, szPortName);
32     hPort = CreateFile(szPortName,
33                        GENERIC_READ|GENERIC_WRITE,
34                        0,     // exclusive
35                        NULL,  // sec attr
36                        OPEN_EXISTING,
37                        0,     // no attributes
38                        NULL); // no template
39
40     if (hPort == (HANDLE)-1) {
41         printf("ERROR: CreateFile() failed with result: %lx\n", hPort);
42         return 1;
43     }
44     printf("CreateFile() returned: %lx\n", hPort);
45
46     printf("Fiddling with DTR and RTS control lines...\n");
47         bResult = EscapeCommFunction(hPort, SETDTR);
48     if (!bResult) {
49         printf("WARNING: EscapeCommFunction(SETDTR) failed: %lx\n", bResult);
50     }
51         bResult = EscapeCommFunction(hPort, SETRTS);
52     if (!bResult) {
53         printf("WARNING: EscapeCommFunction(SETRTS) failed: %lx\n", bResult);
54     }
55
56     printf("Getting the default line characteristics...\n");
57         dcb.DCBlength = sizeof(DCB);
58         if (!GetCommState(hPort, &dcb)) {
59         printf("ERROR: failed to get the dcb: %d\n", GetLastError());
60         return 2;
61     }
62     printf("Setting the line characteristics to 9600,8,N,1\n");
63     dcb.BaudRate = dwBaud;
64     dcb.ByteSize = 8;
65     dcb.Parity = NOPARITY;
66     dcb.StopBits = ONESTOPBIT;
67
68     bResult = SetCommState(hPort, &dcb);
69     if (!bResult) {
70         printf("ERROR: failed to set the comm state: %lx\n", bResult);
71         return 3;
72     }
73     printf("INFO: preparing the transmit buffer: %lx\n", bResult);
74         for (i = 0; i < BUFSIZE; i++) {
75         txBuffer[i] = (CHAR)i;
76     }
77         for (i = 0; i < BUFSIZE; i++) {
78         printf(" %d ", txBuffer[i]);
79     }
80     for (i = 0; i < BUFSIZE; i++) {
81         rxBuffer[i] = 0xFF;
82     }
83     printf("\n");
84     printf("Writting transmit buffer to the serial port\n");
85     bResult = WriteFile(hPort, txBuffer, BUFSIZE, &dwNumWritten, NULL);
86     if (!bResult) {
87         printf("ERROR: failed to write to the serial port: %lx\n", bResult);
88         return 4;
89     }
90     printf("WriteFile() returned: %lx, byteswritten: %lx\n", bResult, dwNumWritten);
91 #if 0
92         printf("Attempting to read %d bytes from the serial port\n", BUFSIZE);
93     bResult = ReadFile(hPort, rxBuffer, BUFSIZE, &dwNumRead, NULL);
94         if (!bResult) {
95         printf("ERROR: failed to read from the serial port: %lx\n", bResult);
96         return 5;
97     }
98     printf("ReadFile() returned: %lx, bytesread: %lx\n", bResult, dwNumRead);
99     for (i = 0; i < BUFSIZE; i++) {
100         printf(" %d ",rxBuffer[i]);
101     }
102 #endif
103     printf("Attempting to close the serial port\n");
104     bResult = ClearCommError(hPort, &dwErrors, NULL);
105     printf("ClearCommError returned: %lx, dwErrors: %lx\n", bResult, dwErrors);
106     bResult = CloseHandle(hPort);
107     if (!bResult) {
108         printf("ERROR: failed to close the serial port: %lx\n", bResult);
109         return 6;
110     }
111     printf("Finished\n");
112     return 0;
113 }