Skip to contentSkip to conversation

EOF from the N_TTY ldisc intended?

DISCUSSION [email protected] 1 participant 1 message Jun 12, 2008

lore
Share & tools
lore
#0 lore raw

Hello,

I have a program reading multiple bytes one by one after a select from a serial port opened in non-blocking mode. If I send one byte to that port, the first read delivers it, and the second one returns zero. The program treats this as the end of file and exits. Is this behavior of the default line discipline intended? I would expect it to return -EAGAIN if there is no more data in the receive queue.

I'm attaching a sample program.

Please CC to me, I'm not subscribed.

Thanks in advance,

Signature · 2 lines
Baurzhan Ismagulov
http://www.kz-easy.com/

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

static int
dev_init(int fd)

{
	struct termios t;
	t.c_iflag = IGNBRK | IGNPAR | INPCK;
	t.c_oflag = 0;
	t.c_cflag = B57600 | CLOCAL | CREAD | CS8 | PARENB;
	t.c_lflag = 0;
	memset(t.c_cc, _POSIX_VDISABLE, NCCS);
	t.c_cc[VMIN] = 0;
	t.c_cc[VTIME] = 0;
	if (tcsetattr(fd, TCSANOW, &t) == -1) {
		perror("tcsetattr");
		exit(2);
	}
	return 0;
}

static int
dev_open(const char *name)

{
	int fd = open(name, O_RDWR | O_NONBLOCK);
	if (fd == -1) {
		perror("open");
		exit(2);
	}
	return fd;
}

static ssize_t
dev_read(int fd, void *buf, size_t count)

{
	ssize_t ret = read(fd, buf, count);
	if (ret == -1) {
		perror("read");
		exit(2);
	}
	printf("%s: %d\n", __FUNCTION__, ret);
	return ret;
}

int
main(void)

{
	int fd = dev_open("/dev/ttyS0");
	dev_init(fd);

	fd_set rfds;
	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);
	if (select(fd + 1, &rfds, NULL, NULL, NULL) == -1) {
		perror("select");
		exit(2);
	}

	char buf;
	dev_read(fd, &buf, 1);
	dev_read(fd, &buf, 1);

	return 0;
}