การใช้ UART ผ่าน GPIO Pin ใน Raspberry Pi ด้วย Software Serial

You can use the standard, HW UART on the Raspberry Pi to capture UART data. 

In addition to this standard UART, as demonstrated below, you can use two GPIO pins on the Raspberry Pi to “Bit Bang” data in or as a “Software Serial” port rather than needing a 2nd Hardware one (that isn’t broken out).

The below software Serial port utilizes the great PIGPIO Library which you can download and install for free at the following URL: PIGPIO Library

Here’s the code I got working with the SparqEE GPS module running at 9600baud and the installation on the Raspberry Pi:

 

Execute this code on the Raspberry Pi to install:

wget abyz.co.uk/rpi/pigpio/pigpio.zip

unzip pigpio.zip

cd PIGPIO

make

sudo make install

 

Here’s the python code. Put it into a file such as “test.py,” change the permissions of the file “chmod 755 test.py,” then execute the script “./test.py”

With this code I simply wanted to input some GPS NMEA strings into the RasPi and print them out. I used GPIO pin 18 for the Receive (RX) side of the GPS device.

 

#!/usr/bin/python

import sys

import time

import difflib

import pigpio

 

RX=18

try:

        pi = pigpio.pi()

        pi.set_mode(RX, pigpio.INPUT)

        pi.bb_serial_read_open(RX, 9600, 8)

 

        print "DATA - SOFTWARE SERIAL:"

        while 1:

                (count, data) = pi.bb_serial_read(RX)

                if count:

                        print data

                time.sleep(1)

 

except:

        pi.bb_serial_read_close(RX)

        pi.stop()

 

 

ประเภทเนื้อหาของ article
Raspberry Pi Zero
Rating
No votes yet