update for HEAD-2003091401
[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 j;
21     int k;
22         int nPortNum = 1;
23
24         TCHAR szPortName[MAX_PORTNAME_LEN];
25
26     if (argc > 1) {
27         //sscanf(argv[1], "%d", &dwBaud);
28         sscanf(argv[1], "%d", &nPortNum);
29     }
30         sprintf(szPortName, _T("COM%d"), nPortNum);
31
32     printf("Serial Port Test Application Version %s\n", APP_VERSION_STR);
33     printf("Attempting to open serial port %d - %s\n", nPortNum, szPortName);
34     hPort = CreateFile(szPortName,
35                        GENERIC_READ|GENERIC_WRITE,
36                        0,     // exclusive
37                        NULL,  // sec attr
38                        OPEN_EXISTING,
39                        0,     // no attributes
40                        NULL); // no template
41
42     if (hPort == (HANDLE)-1) {
43         printf("ERROR: CreateFile() failed with result: %lx\n", hPort);
44         return 1;
45     }
46     printf("CreateFile() returned: %lx\n", hPort);
47
48     printf("Fiddling with DTR and RTS control lines...\n");
49         for (i = 0; i < 100; i++) {
50         bResult = EscapeCommFunction(hPort, SETDTR);
51     if (!bResult) {
52         printf("WARNING: EscapeCommFunction(SETDTR) failed: %lx\n", bResult);
53     }
54         bResult = EscapeCommFunction(hPort, SETRTS);
55     if (!bResult) {
56         printf("WARNING: EscapeCommFunction(SETRTS) failed: %lx\n", bResult);
57     }
58         for (j = 0; j < 1000; j++) {
59                 k *= j;
60         }
61 /*
62 #define CLRDTR  (6)
63 #define CLRRTS  (4)
64 #define SETDTR  (5)
65 #define SETRTS  (3)
66 #define SETXOFF (1)
67 #define SETXON  (2)
68 #define SETBREAK        (8)
69 #define CLRBREAK        (9)
70  */
71         bResult = EscapeCommFunction(hPort, CLRDTR);
72     if (!bResult) {
73         printf("WARNING: EscapeCommFunction(CLRDTR) failed: %lx\n", bResult);
74     }
75         bResult = EscapeCommFunction(hPort, CLRRTS);
76     if (!bResult) {
77         printf("WARNING: EscapeCommFunction(CLRRTS) failed: %lx\n", bResult);
78     }
79         }
80     printf("Getting the default line characteristics...\n");
81         dcb.DCBlength = sizeof(DCB);
82         if (!GetCommState(hPort, &dcb)) {
83         printf("ERROR: failed to get the dcb: %d\n", GetLastError());
84         return 2;
85     }
86     printf("Setting the line characteristics to 9600,8,N,1\n");
87     dcb.BaudRate = dwBaud;
88     dcb.ByteSize = 8;
89     dcb.Parity = NOPARITY;
90     dcb.StopBits = ONESTOPBIT;
91
92     bResult = SetCommState(hPort, &dcb);
93     if (!bResult) {
94         printf("ERROR: failed to set the comm state: %lx\n", bResult);
95         return 3;
96     }
97         for (i = 0; i < BUFSIZE; i++) {
98         txBuffer[i] = (CHAR)i;
99         //printf(" %d ", txBuffer[i]);
100         rxBuffer[i] = 0xFF;
101     }
102     printf("\n");
103     printf("Writting transmit buffer to the serial port\n");
104     bResult = WriteFile(hPort, txBuffer, BUFSIZE, &dwNumWritten, NULL);
105     if (!bResult) {
106         printf("ERROR: failed to write to the serial port: %lx\n", bResult);
107         return 4;
108     }
109     printf("WriteFile() returned: %lx, byteswritten: %lx\n", bResult, dwNumWritten);
110 #if 0
111         printf("Attempting to read %d bytes from the serial port\n", BUFSIZE);
112     bResult = ReadFile(hPort, rxBuffer, BUFSIZE, &dwNumRead, NULL);
113         if (!bResult) {
114         printf("ERROR: failed to read from the serial port: %lx\n", bResult);
115         return 5;
116     }
117     printf("ReadFile() returned: %lx, bytesread: %lx\n", bResult, dwNumRead);
118     for (i = 0; i < BUFSIZE; i++) {
119         printf(" %d ",rxBuffer[i]);
120     }
121 #endif
122     printf("Attempting to close the serial port\n");
123     bResult = ClearCommError(hPort, &dwErrors, NULL);
124     printf("ClearCommError returned: %lx, dwErrors: %lx\n", bResult, dwErrors);
125     bResult = CloseHandle(hPort);
126     if (!bResult) {
127         printf("ERROR: failed to close the serial port: %lx\n", bResult);
128         return 6;
129     }
130     printf("Finished\n");
131     return 0;
132 }