Serial Port on Linux

1. Specify the serial port name

First of all, we need to know the port name (like COM 1/2/3 on a PC) of our device on linux system. To do that use dmesg (display message or driver message) command:

The dmesg command is used to write the kernel messages in Linux and other Unix-like operating systems to standard output (which by default is the display screen).

Because of the length of the output of dmesg, it can be convenient to pipe its output to grep (globally search a regular expression and print), a filter which searches for any lines that contain the string (i.e., sequence of characters) following it. The -i option can be used to tell grep to ignore the case.

And the following tells dmesg to show all serial ports (which are represented by the string tty – teletypewriter):

dmesg | grep -i tty 

through the use of a pipe (designated by the vertical barcharacter), thereby allowing the startup messages to be viewed one screenful at a time:

Type in

dmesg | grep -i tty

gives the results

[ 0.000000] console [tty0] enabled
[ 1.363275] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
[ 12.502936] usb 2-2.1: ch341-uart converter now attached to ttyUSB0

As shown, there are two names. It turns out the first ttyS0 is just a dummy serial port and has nothing to do with out own USB-to-serial convertor pluged-in. In fact, I have tried for hours to configure ttyS0 with no success. So please do remember the converter serial port name is ttyUSB0, not otherwise.

2. The following step to be skipped

some tutorials mentioned about setserial command:

setserial is a program designed to set and/or report the configuration information associated with a serial port. This information includes what I/O port and IRQ a particular serial port is using, and whether or not the break key should be interpreted as the Secure Attention Key, and so on.

and ask you to perform

setserial -g /dev/ttyS[0123]

to verify the port name and baud rate. This is misleading, because the real ttyUSB0 is not shown in this. So please be reminded that setserial command in our case, usb-to-serial convertor.

3. Set up minicom

Replacing graphical serial terminal in a PC, in linux here we use a serial communication application called minicom:

sudo apt-get install minicom

And then configure the minicom by (-c on means enable color mode)

minicom -s -c on

Set the “Serial Port Setup” in the following manner. Both hardware and software flow control to be turned off. In fact, Hardware Flow Control actually means RTS/CTS function, which is not present in our case.

minicom setting

Remember to save as dfl before exit minicom (may need root access to save the file). Finally, we can launch the serial connection by

minicom -c on

4. Problems with /dev/ttyS0 and /dev/ttyUSB0

After numerous tries and research, I realised the serial port i need to connect to is not ttyS0, and it is ttyUSB0 instead.

However there is a permission limitation of ttyUSB0 orignially, by checcking the permission using the command

ls -l

give the output:

crw-rw---- 1 root dialout 188, 0 Jun 11 10:55 ttyUSB0

This means that in order to access ttyUSB0 port, the user must be in root or dialout group. Therefore to grant out current user with access, include it inside dialout group:

sudo adduser <user you want to add> dialout
sudo reboot

To verify, see which groups your user belongs to:

groups <username>

===============================

Alternatively, but not suggested, you may change the permission of the file directly, by

chmod 777 /dev/ttyUSB0

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s