2 * Copyright (c) 2007, IRTrans GmbH
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.
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
)
62 int WriteSerialStringEx (DEVICEINFO
*dev
,byte pnt
[],int len
)
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
);
78 int GetSerialAvailableEx (DEVICEINFO
*dev
)
83 int ReadSerialStringEx (DEVICEINFO
*dev
,byte pnt
[],int len
,word timeout
)
86 DWORD bytes
= 0,dummy
;
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
);
107 void FlushComEx(HANDLE fp
)
109 PurgeComm(fp
,PURGE_TXABORT
| PURGE_RXABORT
| PURGE_TXCLEAR
| PURGE_RXCLEAR
);
113 void SetSerialTimeoutEx (DEVICEINFO
*dev
,word time
)
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
)
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
,
142 FILE_FLAG_OVERLAPPED
,
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
);
161 int OpenSerialPort(char Pname
[])
165 char sDCB
[]="38400,n,8,1";
167 hCom
=CreateFile(Pname
,
168 GENERIC_READ
| GENERIC_WRITE
,
172 FILE_FLAG_OVERLAPPED
,
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
);
187 hComEvent
= CreateEvent (NULL
,TRUE
,FALSE
,NULL
);
192 void WriteSerialString (byte pnt
[],int len
)
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
)
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
);
227 PurgeComm(hCom
,PURGE_TXABORT
| PURGE_RXABORT
| PURGE_TXCLEAR
| PURGE_RXCLEAR
);
230 void SetSerialTimeout (word time
)
234 memset (&to
,0,sizeof (to
));
236 to
.ReadIntervalTimeout
= time
;
237 to
.ReadTotalTimeoutConstant
= time
;
238 SetCommTimeouts (hCom
,&to
);