Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 315
Mike Avatar asked Oct 04 '12 20:10

Mike


People also ask

How do I know which serial port to use?

Open Windows Device Manager. Find "Ports (COM & LPT)" in the list. Expand "Ports (COM & LPT)" to see the names of all serial ports.


2 Answers

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

like image 57
Martin Beckett Avatar answered Oct 19 '22 15:10

Martin Beckett


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.

like image 43
Art Swri Avatar answered Oct 19 '22 15:10

Art Swri