How do I know which /dev/ttyS* is my serial port?

33

6

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?

sivabudh

Posted 2010-04-14T21:10:13.440

Reputation: 2 875

related https://stackoverflow.com/questions/2530096/how-to-find-all-serial-devices-ttys-ttyusb-on-linux-without-opening-them

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2017-05-29T10:50:00.293

Answers

34

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.

sivabudh

Posted 2010-04-14T21:10:13.440

Reputation: 2 875

Does that mean that after running that command, if the response is ...console [tty0] enabled..., and no other, any device is connected to /dev/tty0 (sorry for the apparent silliness of the question)? – Hektor – 2015-02-15T23:49:24.027

1The 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 – Neil McGill – 2015-08-21T15:49:21.850

Sorry, what do I have to do if I don't see any output? – user3019105 – 2016-06-22T16:17:39.793

if you're currently in a terminal over said serial line, you can just type tty to get the name of your tty. I assume that's what OP meant since they did not pose the question as "which serial port(s) have getty running?" – Thom Nichols – 2017-11-07T17:36:12.060

14

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 something is connected and driving the lines CTS, DSR or CD (these are input lines) you can even be pretty sure that there actually is something... Same is true for the rx-byte-count.

Robert Siemer

Posted 2010-04-14T21:10:13.440

Reputation: 358

7

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.

santileortiz

Posted 2010-04-14T21:10:13.440

Reputation: 393

For the record, /dev/serial does not appear by default in Ubuntu 18.04 (Bionic) (no idea about previous versions) – ericx – 2020-02-01T16:27:15.550

6

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.

Ignacio Vazquez-Abrams

Posted 2010-04-14T21:10:13.440

Reputation: 100 516

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 – 2010-04-14T21:18:11.380

You would use setserial to map the resources ttyS0 uses to that of your serial port. http://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.

– Ignacio Vazquez-Abrams – 2010-04-14T21:26:45.497

1

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

Daniël W. Crompton

Posted 2010-04-14T21:10:13.440

Reputation: 1 716