7

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.

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177
  • 1
    Chances are that your USB serial port adapter will show up as `/dev/ttyUSB0`. Also check [this question](http://stackoverflow.com/questions/2530096/) if you need a way to enumerate the serial ports. –  Oct 04 '12 at 20:20
  • pyserial might be of help here. – Prasanth Oct 04 '12 at 20:21
  • @PhilippeGauthier - That would be very unfortunate considering I won't know if the user is going to use a usb->serial cable, or a straight serial cable. :( – Mike Oct 04 '12 at 20:29
  • @goldenparrot - The target platform for this program will most likely not have python support. (uCLinux) – Mike Oct 04 '12 at 20:31
  • @PhilippeGauthier - Checked out that link, it's a good idea, but using `/sys` file system which I need to stay away from. `/sys` didn't show up until 2.6 and since I need to keep compatibility in mind with our 2.4 system, I can't use that. Thanks though! – Mike Oct 05 '12 at 02:40

3 Answers3

3

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

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 1
    Looks like reading the dmesg will work just fine: `[ 827.528859] usb 2-2: pl2303 converter now attached to ttyUSB0` So I can see my device on /dev/ttyUSB0, thanks! – Mike Oct 05 '12 at 12:36
1

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.

Art Swri
  • 2,799
  • 3
  • 25
  • 36
0

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.

ott--
  • 5,642
  • 4
  • 24
  • 27