Script interface (ddcp-script) support for windows
[digitaldcpower] / ddcp-script-windows / ddcp-script-getval-win.c
diff --git a/ddcp-script-windows/ddcp-script-getval-win.c b/ddcp-script-windows/ddcp-script-getval-win.c
new file mode 100644 (file)
index 0000000..05759b6
--- /dev/null
@@ -0,0 +1,107 @@
+/* vim: set sw=8 ts=8 si et: */\r
+/* \r
+ * Windows software to communicate with the DDCP\r
+ * Written by Guido Socher \r
+ *\r
+ * Windows file IO docs: \r
+ * http://msdn.microsoft.com/en-us/library/aa363858%28v=VS.85%29.aspx\r
+ * http://msdn.microsoft.com/en-us/library/aa365747%28VS.85%29.aspx\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 main(int argc, char *argv[])\r
+{\r
+        char CommPath[42];\r
+        char *device;\r
+        COMMTIMEOUTS timeouts={0};\r
+        DWORD numbytes_ok, temp;\r
+        COMSTAT ComState;\r
+        char lastError[512];\r
+        char c;\r
+        int state;\r
+\r
+        if (argc != 2){\r
+                printf("USAGE: ddcp-script-getval COMPORT\n");\r
+                printf("Example: ddcp-script-getval COM4\n");\r
+                exit(0);\r
+        }\r
+        device=argv[1];\r
+\r
+        snprintf(CommPath,40,"\\\\.\\%s",device); // 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
+                FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,\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
+       ClearCommError(fd,&temp,&ComState);// error if temp is not null\r
+        if (temp != 0) {\r
+                //fprintf(stderr, "ClearCommError failed: com port not working\n");\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 in ClearCommError: %s\n",lastError);\r
+                exit(1);\r
+        }\r
+        timeouts.ReadIntervalTimeout=50;\r
+        timeouts.ReadTotalTimeoutConstant=50;\r
+        timeouts.ReadTotalTimeoutMultiplier=10;\r
+        timeouts.WriteTotalTimeoutConstant=50;\r
+        timeouts.WriteTotalTimeoutMultiplier=10;\r
+        if(!SetCommTimeouts(fd, &timeouts)){\r
+                //error occureed. Inform user\r
+                fprintf(stderr, "Setting COM port timeouts failed\n");\r
+                exit(1);\r
+        }\r
+       // send empty line:\r
+       WriteFile(fd,"\r",1,&numbytes_ok,NULL)||fprintf(stderr, "WriteFile failure\n");\r
+        Sleep(100); // Sleep: units are in 1/1000 sec, commands are polled in the avr and it can take 100ms\r
+       WriteFile(fd,"\r",1,&numbytes_ok,NULL)||fprintf(stderr, "WriteFile failure\n");\r
+        Sleep(100); // Sleep: units are in 1/1000 sec, commands are polled in the avr and it can take 100ms\r
+        state=0;\r
+        while ( ReadFile(fd,&c,1,&numbytes_ok,NULL) != 0 ){\r
+                //printf(":0x%x:\n",c); // debug\r
+                if (c=='#') { // find begining of prompt\r
+                        state=1;\r
+                }\r
+                if (state<1) continue;\r
+                if (c=='>') {\r
+                        fputc(c,stdout);\r
+                        state=2;\r
+                }\r
+                if (state>1) break;\r
+                fputc(c,stdout);\r
+        }\r
+        CloseHandle(fd);\r
+        printf("\n");\r
+        Sleep(100); // Sleep: units are in 1/1000 sec, commands are polled in the avr and it can take 100ms\r
+        return(0);\r
+}\r