Score:1

Unable to read from serial port on command line

de flag

I tried reading from /dev/ttyS0 on 1 terminal as root with cat:

cat < /dev/ttyS0

and sending message to the serial port from another terminal as root with echo:

echo -ne "hello" > /dev/ttyS0

But I got nothing from the cat. Can someone enlighten me to the cause of the problem?

guiverc avatar
cn flag
Only supported releases of Ubuntu (*standard or public support*) are on-topic for this site. Ubuntu 14.04 LTS is EOL (*end-of-life*) thus off-topic, and Ubuntu 14.04 ESM is in *extended* support and only supported by Canonical via Ubuntu Advantage thus also off-topic here. Refer https://askubuntu.com/help/on-topic https://help.ubuntu.com/community/EOLUpgrades https://fridge.ubuntu.com/2019/05/02/ubuntu-14-04-trusty-tahr-reached-end-of-life-on-april-25-2019-esm-available/
ablan avatar
de flag
Wait this site is by Canonical? I thought it is by volunteers?
FedKad avatar
cn flag
It is by volunteers. However there is no point spending time on an unsupported release... Anyway, what is on the other side of the serial port you are trying to read and write? A modem, another computer?
guiverc avatar
cn flag
It's an official site affiliated with Ubuntu yes, why I can use the site without logging in via Stack Exchange (ie. using my Ubuntu-ONE SSO), but it's volunteers who are here too.
ablan avatar
de flag
Nothing is on the other side. I just ```ls /dev/```, saw ttyS0 is listed, then give it a try, but did not work.
Score:1
cn flag

The device /dev/ttyS0 normally corresponds to your computer's first serial port. Nowadays, computers do not have serial ports, but some USB devices may appear at a /dev/ttySx port of your system. If the device was connected to your computer during boot, you can find the device name probably by running:

journalctl -b | grep ttyS

If the device is not connected yet, first run the command:

journalctl -f | grep ttyS

and then connect the device; you should see a message indicating the port number x.


If you are trying to send data to the virtual consoles of a desktop system, first press the key combination Ctrl+Alt+F4 and log in to the virtual console displayed in text mode. Then run the command

who am i

to verify that the device you have logged in is actually /dev/tty4 (the second word of the above command's output should be tty4 in this case).

On the main graphical screen (which can be reached by pressing the key combination Ctrl+Alt+F2) open a terminal and run the command:

echo "Hello" >/dev/tty4

Switch back to the console using Ctrl+Alt+F4 and see that message displayed.

Similarly run the following command on the main graphical screen:

cat </dev/tty4

Switch back to the console and try to enter some characters. Some of the characters will be echoed to the screen (by bash), some will not. The characters not echoed will be displayed on the main graphical screen as the output of the cat command. The reason for this is that the same device is being read by two processes (one on the text console which is bash and the other on the graphical screen which is the cat command).


Note:

For a non-root user to be able to read & write to a serial port, that user needs to have read & write permission for that device. In Ubuntu, this can easily be done by adding the current user to the dialout group using the

sudo addgroup _your_user_ dialout

You may have to log out and then log back in for this change to take effect.

ablan avatar
de flag
Does this also work if there is no actual hardware connected to /dev/ttyS0?
FedKad avatar
cn flag
@ablan No. There must be some device "connected" to this port... And, if there is something extra info you need in my answer, please inform me so.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.