Monday, December 31, 2012

Raspberry Pi UART with PySerial

OS: Occidentalis v0.2
Hardware: Raspberry Pi Revision B with Cobbler
Setup: Serial loopback (connect RX and TX pins on GPIO pins)

Linux attempts to treat all devices as file system like devices, the UART that is available on the GPIO pins is located at:

/dev/ttyAMA0

Configure the operating system:

Occidentalis comes pre-configured to allow you to console into the Raspberry Pi using the external UART. If you intend to use the UART for your own software you will have to disable this functionality.

Below is a summary of This Post

First backup the two files you are going to edit with:

sudo cp /boot/cmdline.txt /boot/cmdline.txt.bak
sudo cp /etc/inittab /etc/inittab.bak


Then use your favorite editor to remove these two settings from /boot/cmdline.txt:

console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

Then comment out the line that mentions ttyAMA0 in /etc/inittab. (place a # at the start of the line.

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

Install PySerial
Reference: http://pyserial.sourceforge.net/index.html

PySerial is a python library for interfacing with serial interfaces, it does not come standard with Occidentalis. You can download it at pyserial-2.6.tar.gz. To extract the file and install use the following commands:

mkdir pyserial-2.6
tar -zxvf pyserial-2.6.tar.gz pyserial-2.6
cd pyserial-2.6
python setup.py install

Using PySerial
Reference: http://pyserial.sourceforge.net/index.html

Here is a simple script that tests the serial connection in loopback. Note that you need to wait for a bit in between sending characters and receiving them.  This is because the command to send serial characters uses interrupts and does not wait for the output to be put on the bus before it returns.  If you print the input and output strings you will see that the last character gets dropped once the output sting is longer than can be sent in the given delay.  On mine it fails when writing strings of characters longer than 46 with a 0.5 second delay.


from serial import Serial
import time

serialPort = Serial("/dev/ttyAMA0", 9600, timeout=2)
if (serialPort.isOpen() == False):
    serialPort.open()

outStr = ''
inStr = ''

serialPort.flushInput()
serialPort.flushOutput()

for i, a in enumerate(range(33, 126)):
    outStr += chr(a)

    serialPort.write(outStr)
    time.sleep(0.05)
    inStr = serialPort.read(serialPort.inWaiting())

    #print "inStr =  " + inStr
    #print "outStr = " + outStr

    if(inStr == outStr):
        print "WORKED! for length of %d" % (i+1)
    else:
        print "failed"

serialPort.close()

4 comments:

  1. Buried in here was the answer I needed. I have a Raspberry pi remotely monitoring a machine with a mod bus line driver off the ttyAMA0 port. I use encrypted UDP for quick messages to/from it. And sometimes it just dies on me. Putting a delay between serial send and receive saved the day. A very long search for this one. High level languages shouldn't have inturrupt issues buried in them! THANKS!

    ReplyDelete
    Replies
    1. Hi PwrEng

      I have a Rpi B and want to use it to communicate with a PLC using modbus. I have pyserial /pymodbus installed, I'm using a 485en SIPEX half duplex chip to to interface between the PLC and the PI. I have checked the seria.Serial is working fine.
      I'm a noob to modbus and I don't see any good examples of python code to set up the protocol. Would you have any advice on where to go from here?

      Delete
  2. Hey,
    I am communicating to my LCD screen from Rpi via UART.
    I am unable to send integers from Rpi to LCD. Only strings are accepted. Any solution?
    I'm using serial.write()

    ReplyDelete
    Replies
    1. I believe that is because serial.write() only takes byte arrays or other objects that behave like a byte array (duck typing). Here is the documentation for it: http://pyserial.readthedocs.io/en/latest/pyserial_api.html. To send an integer you could do something like this serial.write("{}".format(number)).

      Delete