Per default the Raspberry Pi's serial port is setup as a console port. This means that you can connect a terminal, or terminal emulator, to the serial port and start sending commands to the Raspberry Pi. Back in the old days this was a great way to control a headless Linux machine. Today however we use the ethernet connection for that, preferably over ssh so it is even safe to command your Pi over the insecure internet.
If we can free the serial port from its terminal duties we can put the serial port of the Raspberry Pi to better use.
For instance to interface with real world, in real time, using a micro controller.
Remember that Linux is a multi tasking OS, which means that it may give priority to other tasks, putting your real time application on hold for a few milliseconds.
I don't say that it is impossible to achieve precise timing on the Pi alone, but a dedicated micro controller is much better suited for that task.
And it doesn't have to cost more than €1 or €2 at today's prices.
On top of that it protects the Raspberry Pi from electrical mishaps.
I'd rather frie a €2 micro controller than my precious Raspberry Pi.
When properly set up, the Pi's serial port can be used to talk to this I/O micro processor with minimal effort.
First of all we have to make sure that we are allowed to access the serial port, without having to become root.
It's not practical to have to become root every time we run a program which uses the serial port.
Taking ownership of the serial port is as easy as adding the group dialout to your login id. You do that with the following command:
sudo usermod -a -G dialout pi
In the line above pi is the user you want to add a group to.
If you are logged in as a different user you'll have to use that username instead on the line above.
You may have to add the dialout group to other, maybe less obvious, accounts too, depending on the intended use of the system. For instance if you want your web server to controll the serial port you may have to add the dialout group the the www-data user too.
Next we have to disable the Serial Console, to free the serial port for our own communication. For that you'll have to edit the file /etc/inittab.
sudo nano /etc/inittab
Then find the line below and place a # sign in front of it, to make it a comment so it will be ignored the next time the system starts. B.T.W. this is usually the last line in the file.
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
The next time you start your Raspberry Pi the console port will be disabled.
But we're not home yet.
Upon boot the entire boot log is output on the serial port, which might confuse your micro controller which doesn't expect all that gibberish.
Edit the file /boot/cmdline.txt and delete two fields from the text you see in your editor. The fields to delete are:
Be careful to leave all the remaining text on one single line! Now it's time to reboot your Raspberry Pi.
The UART, or serial port, of your Raspberry Pi can now be used for communication to whatever serial device you want to use.
Remember that the Raspberry Pi can only talk to 3.3V logic devices.
Other devices, like real RS-232 devices, will require a decent level converter.
The necessary hardware changes are beyond the scope of this tweak though.
Before we can actually use the serial port we need some software. Luckily there is a lot to choose from. I'm going to name only 2 though.
There is an easy way to test your serial interface. Simply put a jumper over pins 8 and 10 on P02, which connects the TxD and RxD lines together. All you send through the serial interface will then loop back to the input of the same serial interface. So everything you type will be echoed back, proving that it works.
Minicom is a terminal emulation program, very similar to the ancient DOS program Telix. It can be used to easily type characters to the serial port and display the characters which are received through the serial port. Installing it is as easy as typing the following command on your command line:
sudo apt-get install minicom
After that you can start minicom with the following commmand:
minicom -b 9600 -D /dev/ttyAMA0
If you're new to minicom (or Telix) you'll find the command Ctrl-A Z quite useful. It shows the commands you can give to minicom using the Ctrl-A prefix. For instance Ctrl-A A toggles the line feed, which moves the cursor one line down whenever a new line character is received. Ctrl-A Q will quit minicom, returning you to the command line.
Ser2net is a server, which accepts telnet connections from the network interface and relays all incoming characters to the serial port.
All characters received on the serial port are relayed back to the connected telnet session.
Installing ser2net is easy, just type the line below on your command line:
sudo apt-get install ser2net
Now you need to edit the file /etc/ser2net.conf and make some changes to the default settings.
The most important changes are the name of the serial device.
Replace all references to ttyS0 with ttyAMA0.
You can safely delete (or comment) the lines refering to ttyS1, because that device does not exist on the Raspberry Pi.
At the same time you may also want to change the Baud rate or time out values for instance.
You can also add new lines, with new port numbers and completely different settings to this file.
The file /etc/ser2net.conf contains ample information about what you can do with it.
After changing the settings you can start the server by typing:
sudo /etc/init.d/ser2net restart
Per default ser2net listens on 4 different TCP ports.
Each TCP port is dedicated to a particular set of settings, as described in the file /etc/ser2net.conf.
The server will allow only one connection per serial port at a time.
So, to connect to a serial device which communicates at 9600 Baud, 8 data bits, no parity and 1 stop bit, you would telnet to port 2000 like this:
telnet yourpi 2000
Where yourpi is the IP address or host name of your Raspberry Pi.
It goes without saying that you'll need to have a telnet client installed on the computer you are typing this command on.
You can disconnect from the server by typing Ctrl-] Q.