5 #define MAX_PORTNAME_LEN 20
6 #define APP_VERSION_STR "0.01"
8 int main(int argc, char *argv[])
10 CHAR txBuffer[BUFSIZE];
11 CHAR rxBuffer[BUFSIZE];
24 TCHAR szPortName[MAX_PORTNAME_LEN];
27 //sscanf(argv[1], "%d", &dwBaud);
28 sscanf(argv[1], "%d", &nPortNum);
30 sprintf(szPortName, _T("COM%d"), nPortNum);
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,
42 if (hPort == (HANDLE)-1) {
43 printf("ERROR: CreateFile() failed with result: %lx\n", hPort);
46 printf("CreateFile() returned: %lx\n", hPort);
48 printf("Fiddling with DTR and RTS control lines...\n");
49 for (i = 0; i < 100; i++) {
50 bResult = EscapeCommFunction(hPort, SETDTR);
52 printf("WARNING: EscapeCommFunction(SETDTR) failed: %lx\n", bResult);
54 bResult = EscapeCommFunction(hPort, SETRTS);
56 printf("WARNING: EscapeCommFunction(SETRTS) failed: %lx\n", bResult);
58 for (j = 0; j < 1000; j++) {
71 bResult = EscapeCommFunction(hPort, CLRDTR);
73 printf("WARNING: EscapeCommFunction(CLRDTR) failed: %lx\n", bResult);
75 bResult = EscapeCommFunction(hPort, CLRRTS);
77 printf("WARNING: EscapeCommFunction(CLRRTS) failed: %lx\n", bResult);
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());
86 printf("Setting the line characteristics to 9600,8,N,1\n");
87 dcb.BaudRate = dwBaud;
89 dcb.Parity = NOPARITY;
90 dcb.StopBits = ONESTOPBIT;
92 bResult = SetCommState(hPort, &dcb);
94 printf("ERROR: failed to set the comm state: %lx\n", bResult);
97 for (i = 0; i < BUFSIZE; i++) {
98 txBuffer[i] = (CHAR)i;
99 //printf(" %d ", txBuffer[i]);
103 printf("Writting transmit buffer to the serial port\n");
104 bResult = WriteFile(hPort, txBuffer, BUFSIZE, &dwNumWritten, NULL);
106 printf("ERROR: failed to write to the serial port: %lx\n", bResult);
109 printf("WriteFile() returned: %lx, byteswritten: %lx\n", bResult, dwNumWritten);
111 printf("Attempting to read %d bytes from the serial port\n", BUFSIZE);
112 bResult = ReadFile(hPort, rxBuffer, BUFSIZE, &dwNumRead, NULL);
114 printf("ERROR: failed to read from the serial port: %lx\n", bResult);
117 printf("ReadFile() returned: %lx, bytesread: %lx\n", bResult, dwNumRead);
118 for (i = 0; i < BUFSIZE; i++) {
119 printf(" %d ",rxBuffer[i]);
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);
127 printf("ERROR: failed to close the serial port: %lx\n", bResult);
130 printf("Finished\n");