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.
33 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
44 #include <arpa/inet.h>
54 #define BAUDRATE B38400
56 char SerialDevice
[256];
57 extern char baudrate
[10];
61 void msSleep (int time
)
65 tv
.tv_sec
= time
/ 1000;
66 tv
.tv_usec
= (time
% 1000) * 1000;
68 select (0,NULL
,NULL
,NULL
,&tv
);
71 void WriteSerialString (byte pnt
[],int len
)
75 res
= write (hCom
,pnt
,len
);
77 log_print ("IRTrans Connection lost. Aborting ...\n",LOG_FATAL
);
82 int ReadSerialString (byte pnt
[],int len
,word timeout
)
91 tv
.tv_sec
= timeout
/ 1000;
92 tv
.tv_usec
= (timeout
% 1000) * 1000;
93 bytes
= select (hCom
+1,&fs
,NULL
,NULL
,&tv
);
94 if (!bytes
) return (total
);
95 bytes
= read (hCom
,pnt
+total
,len
-total
);
101 int WriteSerialStringEx (DEVICEINFO
*dev
,byte pnt
[],int len
)
105 res
= write (dev
->io
.comport
,pnt
,len
);
107 if (dev
->io
.if_type
== IF_USB
&& !(mode_flag
& NO_RECONNECT
)) {
108 close (dev
->io
.comport
);
110 stat
= OpenSerialPortEx (dev
->io
.node
,&dev
->io
.comport
,0);
111 if (stat
) sleep (10);
115 log_print ("IRTrans Connection lost. Aborting ...\n",LOG_FATAL
);
118 return (ERR_TIMEOUT
);
124 int ReadSerialStringEx (DEVICEINFO
*dev
,byte pnt
[],int len
,word timeout
)
130 while (total
< len
) {
132 FD_SET (dev
->io
.comport
,&fs
);
133 tv
.tv_sec
= timeout
/ 1000;
134 tv
.tv_usec
= (timeout
% 1000) * 1000;
135 bytes
= select (dev
->io
.comport
+1,&fs
,NULL
,NULL
,&tv
);
136 if (!bytes
) return (total
);
137 bytes
= read (dev
->io
.comport
,pnt
+total
,len
-total
);
149 void FlushComEx(HANDLE fp
)
160 bytes
= select (fp
+1,&fs
,NULL
,NULL
,&tv
);
162 bytes
= read (fp
,dummy
,256);
167 int OpenSerialPort(char Pname
[])
169 int parnum
= 0,res
,flg
;
170 struct termios portterm
;
172 strcpy (SerialDevice
,Pname
);
173 if ((hCom
= open(Pname
, O_RDWR
| O_NOCTTY
)) < 0) return (ERR_OPEN
);
181 if (flock(hCom
, LOCK_EX
| LOCK_NB
) < 0) {
187 portterm
.c_cflag
= CS8
| CREAD
| CLOCAL
;
189 portterm
.c_cc
[VMIN
] = 1;
190 portterm
.c_cc
[VTIME
] = 0;
192 cfsetispeed(&portterm
, BAUDRATE
);
193 cfsetospeed(&portterm
, BAUDRATE
);
195 portterm
.c_lflag
= 0;
197 portterm
.c_iflag
= IGNBRK
;
198 portterm
.c_oflag
= 0;
201 tcflush(hCom
, TCIOFLUSH
);
202 if (tcsetattr(hCom
, TCSANOW
, &portterm
) < 0) {
208 tcflush(hCom
, TCIOFLUSH
);
215 int OpenSerialPortEx (char Pname
[],int *port
,int wait
)
218 struct termios portterm
;
219 if ((*port
= open(Pname
, O_RDWR
| O_NOCTTY
)) < 0) return (ERR_OPEN
);
221 if (!isatty(*port
)) {
227 if (flock(*port
, LOCK_EX
| LOCK_NB
) < 0) {
233 portterm
.c_cflag
= CS8
| CREAD
| CLOCAL
;
235 portterm
.c_cc
[VMIN
] = 1;
236 portterm
.c_cc
[VTIME
] = 0;
239 if (!strcmp (baudrate
,"4800")) {
240 cfsetispeed(&portterm
, B4800
);
241 cfsetospeed(&portterm
, B4800
);
244 else if (!strcmp (baudrate
,"9600")) {
245 cfsetispeed(&portterm
, B9600
);
246 cfsetospeed(&portterm
, B9600
);
249 else if (!strcmp (baudrate
,"19200")) {
250 cfsetispeed(&portterm
, B19200
);
251 cfsetospeed(&portterm
, B19200
);
254 cfsetispeed(&portterm
, BAUDRATE
);
255 cfsetospeed(&portterm
, BAUDRATE
);
258 portterm
.c_lflag
= 0;
260 portterm
.c_iflag
= IGNBRK
;
261 portterm
.c_oflag
= 0;
264 tcflush(*port
, TCIOFLUSH
);
265 if (tcsetattr(*port
, TCSANOW
, &portterm
) < 0) {
271 tcflush(*port
, TCIOFLUSH
);
279 tcflush (int fd
,int mode
)
283 } while (ReadSerialString (st
,1000,10) == 1000);