PiSerialPort

TESTED the serial port with this:

Note: Followed instructions on web page (below) for disconnecting the serial port from the login console, then turning off the console output to the serial port.

Installed serial drivers with:

sudo apt-get install python-serial python3-serial

This appears to have installed the serial port softward on the raspi for both Python2.* and Python3.*

Ran two simple tests.

First was to install the serial cable, and plug it into the laptop using PUTTY.

Then installed minicom, and tested to make sure I could type back and forth. This worked - though the Putty terminal didn't for some unknown reason handle linefeeds. It would do a <CR> and then type the next line of text over the previous line of text. Not sure if this was a problem on the Pi side or the putty side.

Next, wired the tx pin of the PI to the rx pin of my arduino shield that includes the 2 line display.

I was able to use the following python code to write to the display:

import serial

ser=serial.Serial('/dev/ttyAMA0', 9600, timeout=1)

print ser.name ## returned /dev/ttyAMA0

ser.write("hello\n")

Note that the above code works only for Python2

Tried running code on Python3 - and it worked except for the "ser.write("hello\n")" statement.

In Python3, the write function couldn't handle a str (??!!). Not sure what the proper syntax is for that one...

Here's a web posting on how to start Serial communication on the Pi:

http://www.hobbytronics.co.uk/raspberry-pi-serial-port

This information copied shamelessly from this web page: http://www.hobbytronics.co.uk/raspberry-pi-serial-port

Raspberry Pi and the Serial Port

By default the Raspberry Pi’s serial port is configured to be used for console input/output. Whilst this is useful if you want to login using the serial port, it means you can't use the Serial Port in your programs. To be able to use the serial port to connect and talk to other devices (e.g. Arduino), the serial port console login needs to be disabled.

Needless to say you will need some other way to login to the Raspberry Pi, and we suggest doing this over the network using an SSH connection.

Disable Serial Port Login

To enable the serial port for your own use you need to disable login on the port. There are two files that need to be edited

The first and main one is /etc/inittab

This file has the command to enable the login prompt and this needs to be disabled. Edit the file and move to the end of the file. You will see a line similar to

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Disable it by adding a # character to the beginning. Save the file.

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Disable Bootup Info

When the Raspberry Pi boots up, all the bootup information is sent to the serial port. Disabling this bootup information is optional and you may want to leave this enabled as it is sometimes useful to see what is happening at bootup. If you have a device connected (i.e. Arduino) at bootup, it will receive this information over the serial port, so it is up to you to decide whether this is a problem or not.

You can disable it by editing the file /boot/cmdline.txt

The 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

Remove all references to ttyAMA0 (which is the name of the serial port). The file will now look like this

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

Reboot

In order you enable the changes you have made, you will need to reboot the Raspberry Pi

sudo shutdown -r now

Test the Serial Port

A great way to test out the serial port is to use the minicom program. If you dont have this installed run

sudo apt-get install minicom

Connect your PC to the Raspberry Pi serial port using an appropriate serial port adapter and wiring, then open Putty or a similar serial terminal program on PC side. Setup a connection using the serial port at 9600 baud.

Now run up minicom on the Raspberry Pi using

minicom -b 9600 -o -D /dev/ttyAMA0

What you type into the minicom terminal screen should appear on the serial PC terminal and vice versa.