38

I have a laptop which has only one serial port.

I went into:

/dev 

directory, and I found:

ttyS0
ttyS1
ttyS2
ttyS3

How do I know which of those "ttyS" refers to my serial port?

1

5 Answers 5

42

I think it's this command:

dmesg | grep tty

Running that on my own Linux box (which only has 1 Serial port) produces a single ttyS0 output line. Try it on your own, you will see what I mean.

1
  • 1
    The only problem is that dmesg output can be cleared - so if you run this too late, you're out of luck. Looking at /proc/tty/driver/serial seems the more robust answer and then check for rx interrupts increasing in count as you write data to that port
    – Goblinhack
    Commented Aug 21, 2015 at 15:49
22

See which UARTs where detected in /proc/tty/driver/serial. A line with uart:unknown means: nothing detected (and likely not existent).

# cat /proc/tty/driver/serial
serinfo:1.0 driver revision:
0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
1: uart:16550A port:000002F8 irq:3 tx:111780 rx:1321 RTS|DTR|DSR
2: uart:unknown port:000003E8 irq:4
3: uart:unknown port:000002E8 irq:3

If you see any of the CTS, DSR, (D)CD or RI flags (these are input signals), like on UART no. 1 above, you can even be pretty sure that there actually is something connected and driving these lines. Same is true for the rx-byte-count.

Seeing a positive tx-byte-count, RTS and/or DTR only reveals that some software accessed the device and ordered it to set those signals or send bytes here, but not if something was listening.

Note: you might see more ports available in hardware than ports reaching the outside of your computer in form of a connector.

10

If you need to do this programmatically reading the output from dmesg can be troublesome, instead the folder /dev/serial/by-id has sym links that are named after identifiable data of your device and point to the specific /dev/tty* they are connected to.

I'm not sure if this is some special udev rule that is distribution specific, but it works well in Ubuntu, let me know if it works.

1
  • 4
    For the record, /dev/serial does not appear by default in Ubuntu 18.04 (Bionic) (no idea about previous versions)
    – ericx
    Commented Feb 1, 2020 at 16:27
8

ttyS0 through 3 correspond to COM1 through 4, respectively. They usually have the same hardware resources and are not always detectable, so they always exist.

2
  • thanks for the answer. So say I want to tell Linux that I want ttyS0 to map to my serial port hardware, what do I need to do?
    – sivabudh
    Commented Apr 14, 2010 at 21:18
  • 1
    You would use setserial to map the resources ttyS0 uses to that of your serial port. linux.die.net/man/8/setserial This isn't normally required though, since anything beyond COM4 usually has enough auxiliary hardware to allow Linux to detect it and add a serial device as appropriate. Commented Apr 14, 2010 at 21:26
2

There is also the command setserial which uses /proc/tty/driver/serial to get it's data.

# setserial -g /dev/ttyS[0123]
/dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
/dev/ttyS1, UART: 16550A, Port: 0x02f8, IRQ: 3
/dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
/dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3
1
  • setserial -a /dev/tty0 Cannot get serial info: Inappropriate ioctl for device
    – CS QGB
    Commented Sep 27, 2022 at 7:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .