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