Linux - Using Serial ports

Glenn - N3USP had posted a blog and asked me about using Linux to communicate with his TNC for packet communications and at K3REM's request I will post the answer here for everyone's benefit.

I have enhanced the explanations to explain a little about Linux for those unfamiliar with this operating system.

Glenn's particular problem is common with some Linux installations:

Regular users are not granted the permissions they need to access the serial ports. The serial ports are accessible only as root(aka superuser)or a select group of users deemed worthy by the authors of the linux distribution.

While you can login as root to access the serial port, this is considered a security risk to your system and should be avoided for daily use.

We will add ourselves to this select group of users.

Files, and this is key to understanding Linux, is that all hardware is considered to be a file and therefore has a file listing in the directory structure just like all the files in your home directory.

The serial ports like all other hardware in the computer are contained in the /dev directory.

The serial ports all begin with tty, so to see a listing of serial ports in your computer we will issue the command:

ls -l /dev/ttyS*

The case of the letters IS IMPORTANT. Linux is CASE SENSITIVE, if you type a lower case when upper case is required and vice-versa, you won't get the answer you want.

Depending on the linux distribution you have, the serial port file entries may be created automatically when the computer is booted whether they actually exist or not. Most computers have two, so you may see something like this:

crw-rw---- 1 root dialout 4, 64 2008-05-17 13:01 /dev/ttyS0
crw-rw---- 1 root dialout 4, 65 2008-05-26 06:27 /dev/ttyS1
crw-rw---- 1 root dialout 4, 66 2008-05-07 20:09 /dev/ttyS2
crw-rw---- 1 root dialout 4, 67 2008-05-07 20:09 /dev/ttyS3
crw-rw-rw- 1 root tty 3, 52 2008-05-07 20:09 /dev/ttyS4
crw-rw-rw- 1 root tty 3, 53 2008-05-07 20:09 /dev/ttyS5

This listing shows six serial ports, though I only have two serial ports that actually exist in hardware.

ttyS0, and ttyS1 are the equivalent of Com1 and Com2 on a Windows machine and are the most common.

We will deal with ttyS0.

Make sure you specify a capital ess number zero(S0, not SO{letter oh),so,s0,So)

Do a long listing for the serial port you want to use:

ls -la /dev/ttyS0

You should get back something like this:
crw-rw---- 1 root dialout 4, 64 2008-04-16 14:07 /dev/ttyS0

The letter c in the the first position tells us that this is a "character" device(transfer is handled one character at a time).

The next 3 characters are the permissions(read, write, -) given to the owner of the file(root) in this case.

The next 3 characters are the permissions(read, write, -) given to anyone belonging to the group of users that are granted access to this file. (dialout) in this case.

The third position(-) in each of the above two permission examples is the execute bit, which is used for executable programs, but not used for these character devices, so it is blank.

The last three positions(---) are for everyone else using the computer if you aren't the owner, and don't belong to the group. (In other words, everyone else, does not have permission to read from, or write to the serial port).

One way to solve this problem would be to give everyone read-write access to the serial port. It is easy enough to do, but it opens up the serial port as a security risk and to make sure that it happens everytime you reboot the computer requires a few more steps that we won't go into here.

The important parts are the second rw in the permissions, and the group name (dialout) in this case.

From a command line terminal, type the "groups" command from your terminal (not as root), make sure one of the groups you belong to, is the same as the group that the serial port belongs too.

If you are not a member of that group you will have add yourself to the group.

You will have to log in as the root user to do this:

usermod -G -a (serial port group name) (your user name)

If your version of linux doesn't know the -a option, you will have to list all of the
groups that were returned from the groups command, separated by commas.

usermod -G all,group,names,here,including,the,serial,port,group username

You will have to logout and log back in for the change to the groups to
take effect.

If you are using X-windows, you will have to restart X.

If you are just using console windows, 1-6 you can just open a new one
(Alt-F1 through F6).

In most Linux distributions, minicom is the serial communications program of choice for use in a character terminal.

I will present the configuration of minicom in another forum entry.

Bill N3PZB