return -1;
}
- if((fd = open(port, O_RDWR | O_NONBLOCK | O_NOCTTY)) == -1) {
+ if((fd = open(port, O_RDWR | O_NOCTTY)) == -1) {
return -1;
}
- /*memset(&term, 0, sizeof term);*/
tcgetattr(fd, &term);
term.c_oflag = 0;
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
- term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL | CRTSCTS;
+ term.c_cflag = CLOCAL | CREAD | CS8 | HUPCL;
if(mode & SER_8N2) {
term.c_cflag |= CSTOPB;
}
+ if(mode & SER_HWFLOW) {
+ term.c_cflag |= CRTSCTS;
+ }
term.c_iflag = IGNBRK | IGNPAR;
return -1;
}
- if(mode & SER_HWFLOW) {
+#ifdef TIOCM_RTS
+ /* assert DTR/RTS lines */
+ {
int st;
if(ioctl(fd, TIOCMGET, &st) == -1) {
perror("ser_open: failed to get modem status");
return -1;
}
}
+#endif
return fd;
}
close(fd);
}
+int ser_block(int fd)
+{
+ return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
+}
+
+int ser_nonblock(int fd)
+{
+ return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+}
+
int ser_pending(int fd)
{
static struct timeval tv_zero;
{
static char linebuf[512];
static int widx;
- int i, rd, size;
+ int i, rd, size, offs;
size = sizeof linebuf - widx;
while(size && (rd = read(fd, linebuf + widx, size)) > 0) {
size -= rd;
}
+ linebuf[widx] = 0;
+
for(i=0; i<widx; i++) {
if(linebuf[i] == '\r' || linebuf[i] == '\n') {
size = i >= bsz ? bsz - 1 : i;
memcpy(buf, linebuf, size);
buf[size] = 0;
- memmove(linebuf, linebuf + i + 1, sizeof linebuf - i - 1);
- printf("ser_getline-> \"%s\"\n", buf);
+ offs = i + 1;
+ memmove(linebuf, linebuf + offs, widx - offs);
+ widx -= offs;
return buf;
}
}
return 0;
}
-char *ser_getline_block(int fd, char *buf, int bsz)
-{
- if(!ser_wait(fd, -1)) {
- return 0;
- }
- return ser_getline(fd, buf, bsz);
-}
-
static int baud_id(int baud)
{
switch(baud) {