Raspberry Pi Bit-Bang Software UART with pigpio Library

Raspberry Pi Bit-Bang Software UART with pigpio Library


There is a great Python for Library for Raspberry Pi that allows you to easily implement a software bit-banged UART. It is located here:

http://abyz.co.uk/rpi/pigpio/

Below is a simple example of how to accomplish communication with it on arbitrary GPIOs for Tx and Rx:

#!/usr/bin/env python


# Python app to run a Cozir Sensor


import serial

import time

import signal

import sys

import pigpio


def handler(signum,frame):

print 'You pressed CTRL+C! Exiting...'

pi.bb_serial_read_close(RXD)

pi.wave_delete(wid)

pi.stop()

sys.exit(0)


TXD=17

RXD=27


pi = pigpio.pi() # Connect to local host.

print "pigpio Connected!"


pi.bb_serial_read_open(RXD, 9600)


pi.set_mode(TXD,pigpio.OUTPUT)

pi.wave_clear()


#Set Cozir mode to polling

pi.wave_add_serial(TXD,38400,b"K 2\r\n")

wid = pi.wave_create()

pi.wave_send_once(wid)

time.sleep(1)


#Send command to read CO2 measurement

pi.wave_add_serial(TXD,38400,b"Z\r\n")

wid = pi.wave_create()


#signal handles for keyboard interrupt

signal.signal(signal.SIGINT, handler)


while True:

try:

pi.wave_send_once(wid)

time.sleep(.01)

(count,resp) = pi.bb_serial_read(RXD)

print "Cozir Raw Response = " + str(resp)

resp = resp[:8]

print ""

print ""

fltCo2 = float(resp[2:])

print "Co2 = " + str(fltCo2)

time.sleep(1)


except Exception,e:


print "Error: %s"%(e)

time.sleep(1)

continue


Hope this helps someone, Happy Coding!!


ClassyBits 2016