5 #include <sys/select.h>
17 #define BUFFERSIZE BUFSIZ*16
19 #define BADSPEED 999999
20 #define SERIAL_TIMEOUT 3
21 #define SREAD_TIMEOUT 70
41 static unsigned int sp[][2] =
90 for (n = 0; sp[n][0] != speed; n++)
105 const char *pszDevice,
110 int DesiredAccess = O_RDWR|O_NOCTTY|O_NONBLOCK;
112 char DeviceName[512];
113 struct termios oldserp, nwserp;
130 DesiredAccess |= O_RDONLY;
133 DesiredAccess |= O_WRONLY;
138 DesiredAccess |= O_RDWR;
142 DeviceName[0] =
'\0';
146 if( strcasecmp(pszDevice,
"COM") == 0 )
148 strcpy( DeviceName,
"/dev/modem" );
152 strcpy( DeviceName, pszDevice );
157 sprintf(DeviceName,
"/dev/ttyS%d", (iPort-1));
168 if( dev_testlock(DeviceName) )
173 plckid = dev_lock(DeviceName);
181 SerialFD = open( DeviceName, DesiredAccess );
186 dev_unlock(DeviceName, plckid);
197 if( tcgetattr ( SerialFD, &oldserp ) )
203 if( tcflush( SerialFD, TCIOFLUSH) )
209 if( tcgetattr ( SerialFD, &nwserp ) )
225 nwserp.c_cflag |= CREAD;
226 nwserp.c_iflag &= ~(IXON | IXOFF | IXANY);
227 nwserp.c_cflag |= CREAD;
232 nwserp.c_lflag |= (ICANON|OPOST|ONLCR);
238 nwserp.c_cc[VMIN] = 1 ;
239 nwserp.c_cc[VTIME] = 0 ;
243 nwserp.c_cflag &= ~(HUPCL);
245 nwserp.c_cflag |= (HUPCL);
255 nwserp.c_cflag &= ~(CLOCAL);
259 nwserp.c_cflag |= CLOCAL;
266 nwserp.c_cflag |= CRTSCTS;
268 nwserp.c_cflag &= ~CRTSCTS;
271 nwserp.c_cflag |= TermSpeed ;
273 nwserp.c_cflag &= ~(CSIZE);
278 nwserp.c_cflag |= CS5 ;
281 nwserp.c_cflag |= CS6 ;
284 nwserp.c_cflag |= CS7 ;
289 nwserp.c_cflag |= CS8 ;
293 switch ( options->
Parity )
296 nwserp.c_cflag &= ~(PARENB);
301 nwserp.c_cflag &= ~(PARENB);
302 nwserp.c_cflag |= CS8;
307 nwserp.c_iflag |= (PARMRK);
311 nwserp.c_iflag |= (INPCK | ISTRIP);
312 nwserp.c_cflag |= PARENB;
316 nwserp.c_iflag |= (INPCK | ISTRIP);
317 nwserp.c_cflag |= (PARENB|PARODD);
323 nwserp.c_iflag |= (IGNPAR);
325 nwserp.c_iflag &= ~(IGNPAR);
331 nwserp.c_cflag &= ~(CSTOPB);
339 nwserp.c_cflag |= CSTOPB;
346 nwserp.c_iflag &= ~(IXON | IXOFF | IXANY);
347 nwserp.c_iflag |= (IXON | IXANY);
353 if( tcsetattr( SerialFD, TCSAFLUSH, &nwserp ) )
362 dev_unlock(DeviceName, plckid);
364 tcsetattr( SerialFD, TCSAFLUSH, &oldserp);
372 pInfo->
sfd = SerialFD;
373 pInfo->oldtty = oldserp;
376 pInfo->pplckid = plckid;
378 pInfo->
iPort = iPort;
391 SerialFD = pInfo->
sfd;
392 if( ioctl(SerialFD, FIONREAD, &rBytes) )
411 int SerialFD = pInfo->
sfd;
413 (void) signal(SIGALRM,
alrm);
415 rlng=write(SerialFD, data, length);
433 struct timeval tmout;
435 SerialFD = pInfo->
sfd;
438 FD_SET( SerialFD, &rfds );
443 select( SerialFD+1, &rfds,
NULL,
NULL, &tmout );
444 if ( FD_ISSET(SerialFD, &rfds) )
446 if ( (count = read(SerialFD, data, *pLength)) < 0 )
460 struct termios oserp;
466 SerialFD = pInfo->
sfd;
467 oserp = pInfo->oldtty;
469 plckid = pInfo->pplckid;
472 dev_unlock(DeviceName, plckid);
476 tcsetattr( SerialFD, TCSAFLUSH, &oserp);