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