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