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];
22 TCHAR szPortName[MAX_PORTNAME_LEN];
25 //sscanf(argv[1], "%d", &dwBaud);
26 sscanf(argv[1], "%d", &nPortNum);
28 sprintf(szPortName, _T("COM%d"), nPortNum);
30 printf("Serial Port Test Application Version %s\n", APP_VERSION_STR);
31 printf("Attempting to open serial port %d - %s\n", nPortNum, szPortName);
32 hPort = CreateFile(szPortName,
33 GENERIC_READ|GENERIC_WRITE,
40 if (hPort == (HANDLE)-1) {
41 printf("ERROR: CreateFile() failed with result: %lx\n", hPort);
44 printf("CreateFile() returned: %lx\n", hPort);
46 printf("Fiddling with DTR and RTS control lines...\n");
47 bResult = EscapeCommFunction(hPort, SETDTR);
49 printf("WARNING: EscapeCommFunction(SETDTR) failed: %lx\n", bResult);
51 bResult = EscapeCommFunction(hPort, SETRTS);
53 printf("WARNING: EscapeCommFunction(SETRTS) failed: %lx\n", bResult);
56 printf("Getting the default line characteristics...\n");
57 dcb.DCBlength = sizeof(DCB);
58 if (!GetCommState(hPort, &dcb)) {
59 printf("ERROR: failed to get the dcb: %d\n", GetLastError());
62 printf("Setting the line characteristics to 9600,8,N,1\n");
63 dcb.BaudRate = dwBaud;
65 dcb.Parity = NOPARITY;
66 dcb.StopBits = ONESTOPBIT;
68 bResult = SetCommState(hPort, &dcb);
70 printf("ERROR: failed to set the comm state: %lx\n", bResult);
73 printf("INFO: preparing the transmit buffer: %lx\n", bResult);
74 for (i = 0; i < BUFSIZE; i++) {
75 txBuffer[i] = (CHAR)i;
77 for (i = 0; i < BUFSIZE; i++) {
78 printf(" %d ", txBuffer[i]);
80 for (i = 0; i < BUFSIZE; i++) {
84 printf("Writting transmit buffer to the serial port\n");
85 bResult = WriteFile(hPort, txBuffer, BUFSIZE, &dwNumWritten, NULL);
87 printf("ERROR: failed to write to the serial port: %lx\n", bResult);
90 printf("WriteFile() returned: %lx, byteswritten: %lx\n", bResult, dwNumWritten);
92 printf("Attempting to read %d bytes from the serial port\n", BUFSIZE);
93 bResult = ReadFile(hPort, rxBuffer, BUFSIZE, &dwNumRead, NULL);
95 printf("ERROR: failed to read from the serial port: %lx\n", bResult);
98 printf("ReadFile() returned: %lx, bytesread: %lx\n", bResult, dwNumRead);
99 for (i = 0; i < BUFSIZE; i++) {
100 printf(" %d ",rxBuffer[i]);
103 printf("Attempting to close the serial port\n");
104 bResult = ClearCommError(hPort, &dwErrors, NULL);
105 printf("ClearCommError returned: %lx, dwErrors: %lx\n", bResult, dwErrors);
106 bResult = CloseHandle(hPort);
108 printf("ERROR: failed to close the serial port: %lx\n", bResult);
111 printf("Finished\n");