zappa
zappa

Reputation: 336

UNIX - list all existing serial ports

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.

Upvotes: 0

Views: 2882

Answers (1)

rodrigo
rodrigo

Reputation: 98348

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.

Upvotes: 1

Related Questions