Unable to read from serial port on command line
Solution 1:
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/ttyS
x 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.