Gentoo Archives: gentoo-embedded

From: Bob Dunlop <bob.dunlop@×××××××××.uk>
To: gentoo-embedded@l.g.o
Subject: Re: [gentoo-embedded] displaying serial port status
Date: Wed, 21 Apr 2010 19:03:36
Message-Id: 20100421185129.GA20279@xyzzy.org.uk
In Reply to: [gentoo-embedded] displaying serial port status by "Relson
1 Hi again,
2
3 On Wed, Apr 21 at 01:14, Relson, David wrote:
4 > Gretings,
5 >
6 > In the old days when we used direct port I/O to perform serial port actions, code like the following would display the port's status:
7 >
8 > #define BASE 0x03F8 /* COM1 port base address */
9 > #define LS_REG (BASE+5) /* Line Status Register */
10 >
11 > void ShowLineStatus(void)
12 > {
13 > int status = inportb(LS_REG);
14 > if (status & 0x02) printf("Overrun Error\n");
15 > if (status & 0x04) printf("Parity Error\n");
16 > if (status & 0x08) printf("Framing Error\n");
17 > if (status & 0x10) printf("Break Interrupt\n");
18 > if (status & 0x80) printf("Timeout Error\n");
19 > }
20 >
21 > How does one perform the same thing for /dev/ttyS0 (or any other serial port)?
22
23 #include <termios.h> /* or ioctl.h (can't remember) */
24
25 int fd = open("/dev/ttyS0", O_RDWR);
26 int status;
27
28 ioctl (fd, TIOCSERGETLSR, &status);
29 /* Print as above */
30
31 You might want to add error checking. Also remember the live LSR may not be
32 syncronised with the characters your are currently reading if there is a
33 queue anywhere. To get an inline marker of an error use PARMRK in your
34 termios structure.
35
36
37 Alternativly you might want to look at the TIOCGICOUNT ioctl. This fills out
38 a serial_icounter_struct (see /usr/include/linux/serial.h) with counts of the
39 number of overrun errors etc.
40
41
42 --
43 Bob Dunlop

Replies