按以下教程安装
http://www.cnblogs.com/qtsharp/archive/2013/01/22/2872220.html
下载: http://sourceforge.net/projects/pyserial/files/pyserial/2.5/ 解压后进入目录 运行python setup.py install |
GPS连接:
VDD->RPi的3.3V
TXD->RPi的RXD
GND->RPi的GND
以下是GPIO号
我使用了一个自己的4位的数码管
引脚如下
Gpio对应的管脚:
Gpio:24 22 25 9 10 4 8 11 23 17 27 7
管脚:a b c d e f g dp A1 A2 A3 A4
import serial import traceback import thread import time import os import commands import RPi.GPIO as GPIO import string
led_digit = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f] gpio_ucf = [24, 22, 25, 9, 10, 4, 8, 11, 23, 17,27,7] arr = [] ser = serial.Serial('/dev/ttyAMA0') print ser.isOpen()
buf = '' tim = ''
def led(num,ind): sig = led_digit[num] GPIO.output(23, False) #COM1 GPIO.output(17, False) #COM1 GPIO.output(27, False) #COM1 GPIO.output(7, False) #COM1 GPIO.output(gpio_ucf[ind], True) for i in range(0,8): if (sig>>i)&1: GPIO.output(gpio_ucf[i], False) #ON else: GPIO.output(gpio_ucf[i], True) #OFF
def show(digit): for i in range(0,4): led(int(digit[i]), i+8) time.sleep(0.004)
def show_time(i,interval): global num while True: try: show(num) except Exception as ep: print ep
if __name__ == "__main__":
#initial GPIO GPIO.setmode(GPIO.BCM) for i in gpio_ucf: GPIO.setup(i, GPIO.OUT) GPIO.output(i, False) #COM1
num='0000'
thread.start_new_thread(show_time,(1,1)) while True: try: tmp=ser.readline()
if tmp.find('GPRMC')==1:
buf = tmp.split(',') tim = buf[1] print tim num = tim[2:6] #show(num) except Exception as e: print e # ser.close() ser = serial.Serial('/dev/ttyAMA0') #time.sleep(5)
|
因为读串口时间久,把显示函数show(num)放到主循环的话,显示只闪一下。所以用一个线程来显示led。用全局变量num传递时间值。因为Gps不是每次读取都正确,所以用try进行异常处理。显示的是分钟+秒;
结果:
http://v.youku.com/v_show/id_XNTcyNTk2MTky.html
结果还行 就是有时会读不到GPS数据。