Simple test program for Serial Driver testing.
authorRobert Dickenson <robd@reactos.org>
Thu, 3 Oct 2002 19:43:31 +0000 (19:43 +0000)
committerRobert Dickenson <robd@reactos.org>
Thu, 3 Oct 2002 19:43:31 +0000 (19:43 +0000)
svn path=/trunk/; revision=3621

reactos/apps/tests/sertest/makefile [new file with mode: 0644]
reactos/apps/tests/sertest/sertest.c [new file with mode: 0644]

diff --git a/reactos/apps/tests/sertest/makefile b/reactos/apps/tests/sertest/makefile
new file mode 100644 (file)
index 0000000..0e4c6a9
--- /dev/null
@@ -0,0 +1,22 @@
+#
+# $Id: makefile,v 1.0
+
+PATH_TO_TOP = ../../..
+
+TARGET_NORC = yes
+
+TARGET_TYPE = program
+
+TARGET_APPTYPE = console
+
+TARGET_NAME = sertest
+
+TARGET_SDKLIBS = ntdll.a kernel32.a advapi32.a
+
+TARGET_OBJECTS = $(TARGET_NAME).o
+
+include $(PATH_TO_TOP)/rules.mak
+
+include $(TOOLS_PATH)/helper.mk
+
+# EOF
diff --git a/reactos/apps/tests/sertest/sertest.c b/reactos/apps/tests/sertest/sertest.c
new file mode 100644 (file)
index 0000000..dc53aad
--- /dev/null
@@ -0,0 +1,113 @@
+#include <windows.h>
+#include <stdio.h>
+
+#define BUFSIZE 128
+#define MAX_PORTNAME_LEN 20
+#define APP_VERSION_STR "0.01"
+
+int main(int argc, char *argv[])
+{
+    CHAR txBuffer[BUFSIZE];
+    CHAR rxBuffer[BUFSIZE];
+    DWORD dwBaud = 9600;
+    DWORD dwNumWritten;
+    DWORD dwNumRead;
+    DWORD dwErrors;
+    DCB dcb;
+    BOOL bResult;
+    HANDLE hPort;
+    int i;
+       int nPortNum = 1;
+
+       TCHAR szPortName[MAX_PORTNAME_LEN];
+
+    if (argc > 1) {
+        //sscanf(argv[1], "%d", &dwBaud);
+        sscanf(argv[1], "%d", &nPortNum);
+    }
+       sprintf(szPortName, _T("COM%d"), nPortNum);
+
+    printf("Serial Port Test Application Version %s\n", APP_VERSION_STR);
+    printf("Attempting to open serial port %d - %s\n", nPortNum, szPortName);
+    hPort = CreateFile(szPortName,
+                       GENERIC_READ|GENERIC_WRITE,
+                       0,     // exclusive
+                       NULL,  // sec attr
+                       OPEN_EXISTING,
+                       0,     // no attributes
+                       NULL); // no template
+
+    if (hPort == (HANDLE)-1) {
+        printf("ERROR: CreateFile() failed with result: %lx\n", hPort);
+        return 1;
+    }
+    printf("CreateFile() returned: %lx\n", hPort);
+
+    printf("Fiddling with DTR and RTS control lines...\n");
+       bResult = EscapeCommFunction(hPort, SETDTR);
+    if (!bResult) {
+        printf("WARNING: EscapeCommFunction(SETDTR) failed: %lx\n", bResult);
+    }
+       bResult = EscapeCommFunction(hPort, SETRTS);
+    if (!bResult) {
+        printf("WARNING: EscapeCommFunction(SETRTS) failed: %lx\n", bResult);
+    }
+
+    printf("Getting the default line characteristics...\n");
+       dcb.DCBlength = sizeof(DCB);
+       if (!GetCommState(hPort, &dcb)) {
+        printf("ERROR: failed to get the dcb: %d\n", GetLastError());
+        return 2;
+    }
+    printf("Setting the line characteristics to 9600,8,N,1\n");
+    dcb.BaudRate = dwBaud;
+    dcb.ByteSize = 8;
+    dcb.Parity = NOPARITY;
+    dcb.StopBits = ONESTOPBIT;
+
+    bResult = SetCommState(hPort, &dcb);
+    if (!bResult) {
+        printf("ERROR: failed to set the comm state: %lx\n", bResult);
+        return 3;
+    }
+    printf("INFO: preparing the transmit buffer: %lx\n", bResult);
+       for (i = 0; i < BUFSIZE; i++) {
+        txBuffer[i] = (CHAR)i;
+    }
+       for (i = 0; i < BUFSIZE; i++) {
+        printf(" %d ", txBuffer[i]);
+    }
+    for (i = 0; i < BUFSIZE; i++) {
+        rxBuffer[i] = 0xFF;
+    }
+    printf("\n");
+    printf("Writting transmit buffer to the serial port\n");
+    bResult = WriteFile(hPort, txBuffer, BUFSIZE, &dwNumWritten, NULL);
+    if (!bResult) {
+        printf("ERROR: failed to write to the serial port: %lx\n", bResult);
+        return 4;
+    }
+    printf("WriteFile() returned: %lx, byteswritten: %lx\n", bResult, dwNumWritten);
+#if 0
+       printf("Attempting to read %d bytes from the serial port\n", BUFSIZE);
+    bResult = ReadFile(hPort, rxBuffer, BUFSIZE, &dwNumRead, NULL);
+       if (!bResult) {
+        printf("ERROR: failed to read from the serial port: %lx\n", bResult);
+        return 5;
+    }
+    printf("ReadFile() returned: %lx, bytesread: %lx\n", bResult, dwNumRead);
+    for (i = 0; i < BUFSIZE; i++) {
+        printf(" %d ",rxBuffer[i]);
+    }
+#endif
+    printf("Attempting to close the serial port\n");
+    bResult = ClearCommError(hPort, &dwErrors, NULL);
+    printf("ClearCommError returned: %lx, dwErrors: %lx\n", bResult, dwErrors);
+    bResult = CloseHandle(hPort);
+    if (!bResult) {
+        printf("ERROR: failed to close the serial port: %lx\n", bResult);
+        return 6;
+    }
+    printf("Finished\n");
+    return 0;
+}