Script interface (ddcp-script) support for windows
[digitaldcpower] / ddcp-script-windows / ddcp-script-ttyinit-win.c
diff --git a/ddcp-script-windows/ddcp-script-ttyinit-win.c b/ddcp-script-windows/ddcp-script-ttyinit-win.c
new file mode 100644 (file)
index 0000000..7241a1c
--- /dev/null
@@ -0,0 +1,84 @@
+/* vim: set sw=8 ts=8 si et: */\r
+/* Windows software to initialize the com port\r
+*/\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <unistd.h>\r
+#include <time.h>\r
+#include <windows.h>\r
+\r
+static HANDLE fd;\r
+\r
+int open_tty(char *comport){\r
+        char CommPath[42];\r
+        char lastError[512];\r
+        DCB Dcb;\r
+        BOOL ok;\r
+\r
+        snprintf(CommPath,40,"\\\\.\\%s",comport); // COM1 or COM2 or ...\r
+       CommPath[40]='\0';\r
+        fd = CreateFile(CommPath,\r
+                      GENERIC_READ | GENERIC_WRITE,\r
+                      0,\r
+                      NULL,\r
+                      OPEN_EXISTING,\r
+                      0,\r
+                      NULL);\r
+\r
+        if (fd == INVALID_HANDLE_VALUE) {\r
+                //fprintf(stderr, "CreateFile failed: CommPath (%.8x)\n", (unsigned int)GetLastError());\r
+                FormatMessage(\r
+                        FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,\r
+                        NULL,\r
+                        GetLastError(),\r
+                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
+                        lastError,\r
+                        512,\r
+                        NULL);\r
+                fprintf(stderr,"ERROR open com-port: %s\n",lastError);\r
+                exit(1);\r
+        }\r
+\r
+        memset(&Dcb, 0x0, sizeof(DCB));\r
+        Dcb.DCBlength = sizeof (DCB);\r
+        ok = GetCommState(fd, &Dcb);\r
+        if (!ok){\r
+                fprintf(stderr, "File %s Line %d Function GetCommState failed (%.8x)\n",\r
+                       __FILE__, __LINE__,(unsigned int) GetLastError());\r
+                exit(1);\r
+        }\r
\r
+       // some reasonable defaults even if we do not use Tx/Rx:\r
+       Dcb.Parity=NOPARITY;\r
+       Dcb.BaudRate=9600;\r
+       Dcb.ByteSize=8;\r
+       Dcb.StopBits=ONESTOPBIT;\r
+\r
+        Dcb.fDtrControl = DTR_CONTROL_ENABLE;\r
+        Dcb.fRtsControl = RTS_CONTROL_ENABLE;\r
+        ok = SetCommState(fd, &Dcb);\r
+        if (!ok){\r
+                fprintf(stderr, "File %s Line %d Function SetCommState failed (%.8x)\n",\r
+                       __FILE__, __LINE__,(unsigned int) GetLastError());\r
+                exit(1);\r
+        }\r
+        return(0);\r
+}\r
+\r
+int main(int argc, char *argv[])\r
+{\r
+        char *device;\r
+\r
+        if (argc != 2){\r
+                printf("USAGE: ddcp-script-ttyinit COMPORT\n");\r
+                printf("Example: ddcp-script-ttyinit COM4\n");\r
+                exit(0);\r
+        }\r
+        device=argv[1];\r
+\r
+        open_tty(device);\r
+        Sleep(100); // sleep: units are in 1/1000 sec\r
+        CloseHandle(fd);\r
+        return(0);\r
+}\r