Script interface (ddcp-script) support for windows
[digitaldcpower] / ddcp-script-windows / ddcp-script-ttyinit-win.c
1 /* vim: set sw=8 ts=8 si et: */\r
2 /* Windows software to initialize the com port\r
3 */\r
4 \r
5 #include <stdio.h>\r
6 #include <stdlib.h>\r
7 #include <unistd.h>\r
8 #include <time.h>\r
9 #include <windows.h>\r
10 \r
11 static HANDLE fd;\r
12 \r
13 int open_tty(char *comport){\r
14         char CommPath[42];\r
15         char lastError[512];\r
16         DCB Dcb;\r
17         BOOL ok;\r
18 \r
19         snprintf(CommPath,40,"\\\\.\\%s",comport); // COM1 or COM2 or ...\r
20         CommPath[40]='\0';\r
21         fd = CreateFile(CommPath,\r
22                       GENERIC_READ | GENERIC_WRITE,\r
23                       0,\r
24                       NULL,\r
25                       OPEN_EXISTING,\r
26                       0,\r
27                       NULL);\r
28 \r
29         if (fd == INVALID_HANDLE_VALUE) {\r
30                 //fprintf(stderr, "CreateFile failed: CommPath (%.8x)\n", (unsigned int)GetLastError());\r
31                 FormatMessage(\r
32                         FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,\r
33                         NULL,\r
34                         GetLastError(),\r
35                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\r
36                         lastError,\r
37                         512,\r
38                         NULL);\r
39                 fprintf(stderr,"ERROR open com-port: %s\n",lastError);\r
40                 exit(1);\r
41         }\r
42 \r
43         memset(&Dcb, 0x0, sizeof(DCB));\r
44         Dcb.DCBlength = sizeof (DCB);\r
45         ok = GetCommState(fd, &Dcb);\r
46         if (!ok){\r
47                 fprintf(stderr, "File %s Line %d Function GetCommState failed (%.8x)\n",\r
48                        __FILE__, __LINE__,(unsigned int) GetLastError());\r
49                 exit(1);\r
50         }\r
51  \r
52         // some reasonable defaults even if we do not use Tx/Rx:\r
53         Dcb.Parity=NOPARITY;\r
54         Dcb.BaudRate=9600;\r
55         Dcb.ByteSize=8;\r
56         Dcb.StopBits=ONESTOPBIT;\r
57 \r
58         Dcb.fDtrControl = DTR_CONTROL_ENABLE;\r
59         Dcb.fRtsControl = RTS_CONTROL_ENABLE;\r
60         ok = SetCommState(fd, &Dcb);\r
61         if (!ok){\r
62                 fprintf(stderr, "File %s Line %d Function SetCommState failed (%.8x)\n",\r
63                        __FILE__, __LINE__,(unsigned int) GetLastError());\r
64                 exit(1);\r
65         }\r
66         return(0);\r
67 }\r
68 \r
69 int main(int argc, char *argv[])\r
70 {\r
71         char *device;\r
72 \r
73         if (argc != 2){\r
74                 printf("USAGE: ddcp-script-ttyinit COMPORT\n");\r
75                 printf("Example: ddcp-script-ttyinit COM4\n");\r
76                 exit(0);\r
77         }\r
78         device=argv[1];\r
79 \r
80         open_tty(device);\r
81         Sleep(100); // sleep: units are in 1/1000 sec\r
82         CloseHandle(fd);\r
83         return(0);\r
84 }\r