Mike
Mike

Reputation: 49473

How do I know which serial port to use on Linux?

I'm going to be writing a program in C which is going to open a connection over serial port, and "listen" for incoming commands, then it will take some action and report back a status.

The connection is going to be over RS232 (Serial port) and I'm trying to understand how to know which port to open.

When using windows, if I hook up my usb-serial device I see "Prolific USB-to-Serial Comm Port (COM4)" show up in the device manager... but on the Linux side I don't see any changes in the /sys/class/tty or /dev area, I see ttyS0 through ttyS7 present all the time (I'm assuming the S stands for serial based on what I've read).

So how do I know which one to connect to?

EDIT
While I'm developing this on a OpenSUSE 12.1 box (3.1 kernel), the final program will be run on uCLinux on a board running a 2.4Linux kernel, so I'm looking for pure C solutions which will work on older kernels

FYI: the /sys file system as noted in the answer to this post didn't exist until the 2.6 kernel and my constraints force me to stick to things available in the 2.4 kernel.

Upvotes: 7

Views: 14831

Answers (3)

ott--
ott--

Reputation: 5722

Check out /proc/tty/driver/serial - you should see an uart like 16550A instead of unknown and rx should be > 0 for existing ports. If you must guess which port will be used, open all available ports. After that, you need to set up the port for your needs (baudrate, parity, bits etc) or try to guess the incoming baudrate etc.

Upvotes: 0

Martin Beckett
Martin Beckett

Reputation: 96157

The command dmesg will show you the kernel message when the module is plugged in which will give it's device name.

The /proc file system is like the device manager on windows - somewhere in there will be a list of tty devices

Upvotes: 3

Art Swri
Art Swri

Reputation: 2814

Two things that I have used: (a) look for control lines (DTR, DSR, etc) and (b) open 'all' of the ports and find out which one(s) appear to be active. In the latter case it helps if you can send a message to the serial device and have it respond; this obviously works only if the device will respond to a message.

Upvotes: 1

Related Questions