I'm writing a C program that's automatically logging in to some connected Linux device (via Serial Port) and reading all of its logfiles etc.
So here's the problem: I don't want to hardcode the serial port (in my case /dev/ttyS0 ) into my code, but give some kind of prompt at the beginning, listing all physically existing devices from which I can choose from and pass it as a parameter.
Is there a way to distinguish between logical and physical devices in the /dev folder? I don't want to use ls in the /dev folder and have all the (in my case) unnecessary information displayed; I only want the actually existing serial ports to be shown.
Asked
Active
Viewed 2,562 times
0

harrisonfooord
- 308
- 3
- 19
-
You can probably find it somewhere in the `/proc` or `/sys` directories. – Some programmer dude Nov 06 '14 at 10:25
-
Question is probably a duplicate of http://stackoverflow.com/a/9914339 – jkalden Nov 06 '14 at 10:41
-
Please consider not restricting the user to a port from the list. For certain applications it might be useful to have your program operate on a file that is not really a serial port. – fuz Nov 06 '14 at 15:26
1 Answers
1
You can enum all the ttys in the system by reading the symlinks in directory /sys/class/tty/
.
Then you can read the type
pseudofile to check if it is a real serial port or a virtual one. The possible values are in <linux/serial.h>
:
#define PORT_UNKNOWN 0
#define PORT_8250 1
#define PORT_16450 2
#define PORT_16550 3
#define PORT_16550A 4
#define PORT_CIRRUS 5 /* usurped by cyclades.c */
#define PORT_16650 6
#define PORT_16650V2 7
#define PORT_16750 8
#define PORT_STARTECH 9 /* usurped by cyclades.c */
#define PORT_16C950 10 /* Oxford Semiconductor */
#define PORT_16654 11
#define PORT_16850 12
#define PORT_RSA 13 /* RSA-DV II/S card */
Most virtual ports will not even have a type
file. Anyway, a 0
will probably mean a virtual or emulated port.

rodrigo
- 94,151
- 12
- 143
- 190
-
Thanks for your answer, but how do I read the type pseudofile for the given ports? – harrisonfooord Nov 06 '14 at 12:20
-
@Buitenlander You just open the file and read it, as with a normal file. You even can see the contents with `cat`. – rodrigo Nov 06 '14 at 13:01