Serial Port Add On
Note that my reason for adding the serial port is to attach a weather station that sends RS232 data to the RPi.
I have set up the RPi with WIFI so that it will send the weather station data to other computers on my network from a remote location. Within WIFI range of my router.
The following writeup is geared around that project, but there is allot of general information that would be of use to anyone wanting to attach a serial device to the RPi.

The RPi has a serial port built in, but in order to use it, you have to add a RS232 converter module and do some simple reconfigurations.

The module must be connected between the RPi GPIO pins and the serial device such as a data logger, GPS, weather station etc. Or you can connect another computer via a serial cable and use the serial console. See serial console section below.

You must use a converter module between the GPIO pins and the serial device. Most modules have a DB9 (9 pins serial) connector which will take a standard serial cable.





RS232 Module
RPi and RS232

What does the RS232 module do?
The microprocessor and other components on the RPi were designed with the 0 and 3.3 volt logic levels. (Older equipment in the past used 0 and 5V logic levels). These logic levels need to be converted.

The module does conventions both ways.  Data going out of the RPi is converted from the 0 and 3.3V levels to the RS232 levels and the data coming into the RPi from another device is converted from the RS232 levels back down to the 0 and 3.3V logic levels.
Connecting RS232 directly to the GPIO pins could damage the RPi. (let the smoke out).

The RS232 serial standard has been in use for decades and specifies a range between 5 and 15 Volts. Many PC's use 12 Volts. That makes a voltage difference of 24 Volts. The wide voltage swing makes it possible to run data over long cables up to 50' with standard wires and much longer with special low capacitance wire cables. (CAT5 wire can be used)
By comparison the USB standard is much faster but only good for a few feet.
 
The converter module must be compatible with 3.3V voltage levels. I chose to purchase a 5V version off of Ebay then convert it to 3.3V by swapping a chip.
My 5V version came with a MAX232 chip (16 pin DIP). I swapped it with a ADM3202  (equivalent to the MAX3232 ).
Note that the MAX3232 works with both 3.3V and 5V.  Just simply power the device with 3.3V or 5V whichever is needed.

Swapping in the new chip was easy because the chip on the module I bought was in a socket. If the chip is not socketed or has a surface mounted chip then I would not recommend doing this unless you are skilled at soldering.

I choose that route because I got... not 1... but 2 complete modules with the bonus jumper connectors for about the cost of one of the newer boards with surface mount (SMT) components.

Note that there are level converter modules that are made to convert 3.3V to 5V and back. These are meant to convert a 5V only RS232 converter into a module that works with the 3.3V levels. This is NOT what you want unless you already have a 5V RS232 module that can't be converted by swapping the chip. In that case the 3.3V to 5V module would go between the GPIO pins and the 5V RS232 module.



Shopping for an RS232 module
When searching for a module look for one that has the these items:
Note that the RPi serial port is '3 wire' or just TX & RX & Ground. This means it does not implement the 'handshaking' signals CTS & RTS.
Some of the RS232 modules will implement full 'handshaking' or CTS & RTS signals (5 wire) and some do not. The one I have does not. It does not matter if the module has the 3 or 5 wire implementation.
Check that the serial equipment that you want to connect to the RPi  DOES NOT require the full handshaking signals (CTS & RTS).


Connecting the level converter module to the RPi.
You need to make 4 connections between the module board and the RPi

GPIO
Pin#
 Module Serial
Equipment
Power
(3.3V)
 1
* Power
(3.3V)
**
GND
25
GND
GND
TX
8
TX
RX
RX
10
RX
TX

* Note that some module may have [5V] Power designation silk screened on the board especially if it has been converted. If so disregard that.... we must connect power to 3.3 Volts.

** Voltage used by your serial equipment may vary and will most likely be powered by an external power supply. A 5V device could take power from pin 2.

GPIO Header Pins

2
4
6
8
10
12
14
16
18
20
22
24
26
1
3
5
7
9
11
13
15
17
19
21
23
25
1= 3.3V, 25= GND, 8= TX, 10= RX

GPIO to RS232

This photo shows connection to the GPIO. The red wire is on pin 1 (3.3V). Black wire is on pin 25 (GND). White wire is on pin 8 (TX), Blue wire in on pin 10 (RX).

Note that pin 1 always has the square solder pad on the bottom (solder side) of the board. All the other pins are round solder pads.
Pin 1 is closest to the SD card slot and pin 25 is closer to the center of the board.

Using the Serial Console:
This can be used to test your hardware setup before reconfiguring the port.

The RPi in default configuration has the serial port configured as a console.
On the serial console during boot-up you will see all the same boot text information scroll by...... Identical to what you see on the main terminal minus the RPi logo.  When boot-up is complete you can issue commands just as you can do with the main terminal. Of course you cannot start the GUI (startx) from the serial console.

To use the serial console you must have another computer connected to the RPi via a serial cable connected between the RS232 module and the other computer. The other computer must be running a serial communications program such as Hyper Terminal which comes with Windows XP.  Many free serial terminal programs are available as well such as Real Term or Tera Term.

The console serial settings are:
Baud Rate = 115200, Data Bits = 8,  Parity = NONE,  Stop Bits = 1, Flow Control = NONE
The serial terminal software you use on your other computer must be set up to match the above settings. See the section below: Serial Terminal (Communications) Software


Reconfigure the RPi console port to to be used as a standard serial port.
In order to use the serial port on the RPi as a general purpose serial port, the port must be reconfigured by editing 2 configuration files as shown below.  You may want to save backups of these files before editing.
One way to backup is to load the files in the editor then SAVE AS a different file name such as innitab_backup.


Modify the inittab File:
Note my examples assume the gedit editor is installed. sudo apt-get install gedit
If not substitute leafpad for gedit below or use your favorite editor instead.
The Leafpad editor is pre-installed in the Raspbian 'weezy' image.

At a terminal command prompt issue this command:

sudo gedit /etc/inittab

Look near the end of the file and find the lines that look like this:

#Spawn a getty on Raspberry Pi serial line
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

'Comment out' the last line by putting a # character in front of the line.
Leave the other items in the file as they are.
The text should now look like this:

#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Save and exit


Modify the cmdline.txt File:

The following file edit is optional. If you don't do this edit then the boot time text messages will still be sent  to the port. After boot is complete the port will function as a generic serial port.

At a terminal command prompt issue this command:

sudo gedit /boot/cmdline.txt

The entire contents of the file look like this:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Note that this is a single line of text at the very beginning of the file.  If text has wrapped then try to grab the edges of the edit box with your mouse and stretch it until the text is displayed as one line.

Take out this text:
....>>> console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 <<<.........

The resulting text should look like this:
(All on one line beginning on first line and note that the comment character # does not seem to work in this file).

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Save and exit
Restart the RPi for settings to take effect.

To restore the configuration simply reverse the edits shown or restore the files from your backups.


Serial Terminal (Communications) Software
Do not confuse serial terminal software with a terminal session on your RPi.
I use the CuteCom program to communicate with the serial device or computer connected to the serial port on the RPi.

CuteCom
This is a GUI program.

sudo apt-get install cutecom

After cutecom is installed and running set up the communications parameters.
The device name of the RPi serial port is: /dev/ttyAMA0
Of course the communications settings Baud rate, data bits etc must be set to match that of your serial device or other computer.


Another serial communications program for the RPi is minicom.
Minicom is text based and runs in a terminal session window.
 sudo apt-get install minicom

With minicom or cutecom you can communicate with your serial device OR if you want you can connect the RPi to another computer with a serial cable.
The other computer must also be running a serial communications program. Characters typed on one computer should appear on the other and vis versa.
Not of much use in this day and age... Kinda like making a telephone with 2 cups and a string......

Note if you will be using ser2net to put your serial data on the network then don't leave minicom or CuteCom running as some data loss will occur over the network.


Windows Serial Terminal (Communications) Software:
For Windows XP based systems you can use Hyper Terminal
Start --> All Programs --> Accessories --> Communications --> HyperTerminal
If not there then you may need to install it from your Windows XP install CD or image.
Note: HyperTerminal does not come with Windows 7 or 8

Or you can use a different serial communication program such as Real Term or Tera Term


I use the following 3 programs in my weather station project.
I am also using a proprietary weather station software which reads and displays data from a serial port.
If your application can read  TCP/ Telnet  ports then you don't need Real Term or com0com.
 


Real Term - (Windows) A free serial terminal program. Has option to send & receive serial data over a network port. Set up the port and data parameters to match the settings in the ser2net.conf file. In my case Port=192.168.0.104:2000 your port will likely have a different IP number. See ser2net below.



com0com - Program that will generate virtual serial ports on a Windows PC. Useful when you need to use software which gets data from a serial port. For example my weather station software expects the weather station to be connected (wired) to a local serial (com) port. However my weather station is connected to the RPi port located outside.
The ser2net program sends the data out to the network from the RPi. The Real Term program gets data off of the network and echoes it to a virtual port created by com0com. The weather station software can now connect to the virtual port and run just as if  the weather station was directly connected (wired) to a local port.
With the RPi connected to my network via WIFI, I now have a remote wireless serial device outdoors which can be accessed by any computer on my network.

Why do I need com0com?: The Real Term program can echo serial data from network to a physical (or virtual) com port on a PC. If you have a 2nd physical com port on your PC then you could wire the 2 ports together and you would not need com0com. BUT my PC does not have a 2nd physical serial (com) port and I may want to use the 1st (and only) physical port for something else. The com0com creates virtual port pairs which are looped together via software. The virtual ports look and act like physical ports, so Windows and any communications software you use, sees them as actual ports. Even the Found New Hardware Wizard will activate when the virtual ports first added.



Ser2net - A Linux program that is run on the RPi. It automatically loads and is run in the background as a process.
This program will capture the serial data stream and send it over the network as raw or telnet protocol. Other computers on your network can then receive the serial data and process it just as if the device were directly connected to the other computer.
sudo apt-get install ser2net

Ser2net hints and configuration:
Location of binary file: /usr/sbin/ser2net
Location of configuration file: /etc/ser2net.conf
Location of script file: /etc/init.d/ser2net

Once installed ser2net will automatically start at each RPi boot. Once configured you don't need to do anything else.
If you want to run the binary then enter ser2net at command prompt however most of the time you will run the script file instead.
Type this to see all the command line options: ser2net -?

To run the script file then enter: sudo /etc/init.d/ser2net {parameter}
    Valid parameters:    |start|stop|restart|status|
To see parameter list enter /etc/init.d/ser2net without a parameter: /etc/init.d/ser2net
Note sudo is required for start, stop, restart but not status.

For example to see if ser2net is running enter:
/etc/init.d/ser2net status
Be sure to enter the complete path /etc/init.d/ if not you will just run the binary.

Ser2net Configuration
You must configure the ser2net as shown below.

Open the config file for edit:
sudo gedit /etc/ser2net.conf

Comment out the unneeded entries near the end of the file with # character.

Add a line like this:

192.168.1.104,2000:raw:0:/dev/ttyAMA0:9600 8DATABITS NONE 1STOPBIT banner

192.168.1.104 = IP address of RPi. This will be different for you. You can find the IP by looking in your router's configuration pages when the RPi is running and connected to your network. It is really important that you reserve an IP address for the MAC address of your RPi so that the RPi always gets the same IP address each time it boots. You can reserve MAC to IP's in the config pages of most routers.
2000 = IP Port number. write down this number you will need later. Be careful what port number. Don't use a port which is in use by something else!
raw = Raw data
0 = Timeout minutes. (0 means never timeout). Ser2net will stop if no serial activity is present for timeout minutes.
/dev/ttyAMA0 = Name of serial port device
9600 DATABITS NONE 1STOPBIT =  Parameters - Note use parameters that match your serial device
Banner = ? Not sure what this does. Can be left off.



The last few lines of my ser2net config looks like this:

...
...
BANNER:banner:\r\nser2net port \p device \d [\s] (Debian GNU/Linux)\r\n\r\n

# IP address must be reserved on router so RPi always gets the same IP.

192.168.1.104,2000:raw:0:/dev/ttyAMA0:9600 8DATABITS NONE 1STOPBIT banner

#2000:telnet:600:/dev/ttyS0:9600 8DATABITS NONE 1STOPBIT banner
#2001:telnet:600:/dev/ttyS1:9600 8DATABITS NONE 1STOPBIT banner
#3000:telnet:600:/dev/ttyS0:19200 8DATABITS NONE 1STOPBIT banner
#3001:telnet:600:/dev/ttyS1:19200 8DATABITS NONE 1STOPBIT banner

----------------- end of file -----------



Back