Mostly minor fixes up until version 0.8.10.
[irreco.git] / irtrans / irserver / src / winserio.c
blobd5724fb7e1664b46b3c012be0995ea82bbcec11f
1 /*
2 * Copyright (c) 2007, IRTrans GmbH
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of IRTrans GmbH nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY IRTrans GmbH ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL IRTrans GmbH BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifdef WIN32
32 #include <windows.h>
33 #include <stdio.h>
35 #include "remote.h"
36 #include "errcode.h"
37 #include "lowlevel.h"
38 #include "global.h"
41 #include "serio.h"
43 HANDLE hCom;
44 HANDLE hComEvent;
48 // Low Level Routinen
50 // 1. Comm-Device Bereich öffnen
51 // 2. Daten Lesen mit Timeout Wert: SetCommTimeouts
52 // 3. Transceiver zurücksetzen
54 extern char baudrate[10];
57 void msSleep (int time)
59 Sleep (time);
62 int WriteSerialStringEx (DEVICEINFO *dev,byte pnt[],int len)
64 DWORD bytes;
65 OVERLAPPED ov;
67 memset (&ov,0,sizeof (ov));
68 ov.hEvent = dev->io.event;
70 WriteFile(dev->io.comport,pnt,len,&bytes,&ov);
71 WaitForSingleObject (dev->io.event,100);
72 GetOverlappedResult (dev->io.comport,&ov,&bytes,FALSE);
73 ResetEvent (dev->io.event);
74 if ((int)bytes != len) return (ERR_TIMEOUT);
75 else return (0);
78 int GetSerialAvailableEx (DEVICEINFO *dev)
80 return (0);
83 int ReadSerialStringEx (DEVICEINFO *dev,byte pnt[],int len,word timeout)
85 int res,i = 0;
86 DWORD bytes = 0,dummy;
87 OVERLAPPED ov;
89 memset (pnt,0,len);
91 memset (&ov,0,sizeof (ov));
92 ov.hEvent = dev->io.event;
94 SetSerialTimeoutEx (dev,timeout);
95 ReadFile(dev->io.comport,pnt,len,&dummy,&ov);
96 res = WaitForSingleObject (dev->io.event,5);
97 if (res == WAIT_TIMEOUT) res = WaitForSingleObject (dev->io.event,timeout);
99 if (res != WAIT_TIMEOUT) GetOverlappedResult (dev->io.comport,&ov,&bytes,FALSE);
101 ResetEvent (dev->io.event);
103 return (bytes);
107 void FlushComEx(HANDLE fp)
109 PurgeComm(fp,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
113 void SetSerialTimeoutEx (DEVICEINFO *dev,word time)
115 COMMTIMEOUTS to;
117 memset (&to,0,sizeof (to));
119 to.ReadIntervalTimeout = time;
120 to.ReadTotalTimeoutConstant = time;
121 SetCommTimeouts (dev->io.comport,&to);
125 int OpenSerialPortEx (char Pname[],HANDLE *port,int wait)
127 DCB dcb={0};
129 char sDCB[30];
131 strcpy (sDCB,"38400,n,8,1");
133 if (!strcmp (baudrate,"4800")) strcpy (sDCB,"4800,n,8,1");
134 if (!strcmp (baudrate,"9600")) strcpy (sDCB,"9600,n,8,1");
135 if (!strcmp (baudrate,"19200")) strcpy (sDCB,"19200,n,8,1");
137 *port=CreateFile(Pname,
138 GENERIC_READ | GENERIC_WRITE,
140 NULL,
141 OPEN_EXISTING,
142 FILE_FLAG_OVERLAPPED,
143 NULL);
144 if (*port == INVALID_HANDLE_VALUE) return (ERR_OPEN);
146 BuildCommDCB(sDCB, &dcb);
147 dcb.fDtrControl = DTR_CONTROL_ENABLE;
148 dcb.fRtsControl = RTS_CONTROL_ENABLE;
149 dcb.fOutxCtsFlow = 0;
150 dcb.fOutxDsrFlow = 0;
151 SetCommState(*port,&dcb);
152 if (mode_flag & NO_RESET) EscapeCommFunction(*port,SETRTS);
153 else EscapeCommFunction(*port, CLRRTS);
154 EscapeCommFunction(*port, SETDTR);
155 Sleep (wait);
156 FlushComEx (*port);
157 return (0);
161 int OpenSerialPort(char Pname[])
163 DCB dcb={0};
165 char sDCB[]="38400,n,8,1";
167 hCom=CreateFile(Pname,
168 GENERIC_READ | GENERIC_WRITE,
170 NULL,
171 OPEN_EXISTING,
172 FILE_FLAG_OVERLAPPED,
173 NULL);
175 BuildCommDCB(sDCB, &dcb);
176 dcb.fDtrControl = DTR_CONTROL_ENABLE;
177 dcb.fRtsControl = RTS_CONTROL_ENABLE;
178 dcb.fOutxCtsFlow = 0;
179 dcb.fOutxDsrFlow = 0;
180 SetCommState(hCom,&dcb);
181 if (mode_flag & NO_RESET) EscapeCommFunction(hCom,SETRTS);
182 else EscapeCommFunction(hCom, CLRRTS);
183 EscapeCommFunction(hCom, SETDTR);
184 Sleep(1000);
185 FlushCom ();
187 hComEvent = CreateEvent (NULL,TRUE,FALSE,NULL);
188 return (0);
192 void WriteSerialString (byte pnt[],int len)
195 DWORD bytes;
196 OVERLAPPED ov;
198 memset (&ov,0,sizeof (ov));
199 ov.hEvent = hComEvent;
201 WriteFile(hCom,pnt,len,&bytes,&ov);
202 WaitForSingleObject (hComEvent,100);
203 GetOverlappedResult (hCom,&ov,&bytes,FALSE);
204 ResetEvent (hComEvent);
208 int ReadSerialString (byte pnt[],int len,word timeout)
211 DWORD bytes;
212 OVERLAPPED ov;
214 memset (&ov,0,sizeof (ov));
215 ov.hEvent = hComEvent;
217 SetSerialTimeout (timeout);
218 ReadFile(hCom,pnt,len,&bytes,&ov);
219 WaitForSingleObject (hComEvent,timeout);
220 GetOverlappedResult (hCom,&ov,&bytes,FALSE);
221 ResetEvent (hComEvent);
222 return (bytes);
225 void FlushCom(void)
227 PurgeComm(hCom,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
230 void SetSerialTimeout (word time)
232 COMMTIMEOUTS to;
234 memset (&to,0,sizeof (to));
236 to.ReadIntervalTimeout = time;
237 to.ReadTotalTimeoutConstant = time;
238 SetCommTimeouts (hCom,&to);
243 #endif