失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > pigpio 利用普通GPIO为树莓派增加软串口实现ttl通讯

pigpio 利用普通GPIO为树莓派增加软串口实现ttl通讯

时间:2019-01-18 02:08:39

相关推荐

pigpio 利用普通GPIO为树莓派增加软串口实现ttl通讯

树莓派串口

根据官方文档,树莓派0、1、2、3均有2个UARTS:UART、mini UART,其中 mini UART的不足:

No break detectionNo framing errors detectionNo parity bitNo receive timeout interruptNo DCD, DSR, DTR or RI signals

这时如果有多台外设需要通过串口采集数据该怎么办?

买usb转uart硬件,增加硬件串口数量;用pigpio库模拟软串口

软串口的不足是波特率高时,误包率偏高,不过根据测试,19200及以下波特率是可靠的,不需要做错误检测和恢复。

软串口可以利用26个BCM GPIO中的任意两个,除了14、15这一对自带uart,一个树莓派最多可以增加12个软串口!

PIGPIO

是一个利用CPU中断模拟脉冲的库,中文和安装方法可参考这篇博文。

//安装

sudo apt-get update

sudo apt-get install pigpio python-pigpio python3-pigpio

//设置pigpiod自启动

sudo systemctl enable pigpiod

软串口python代码

参考pyserial的调用方式,对pigpio的python方法进行了封装,增加了timeout,命名为softuart

import osimport timeimport pigpioclass softuart(object):"""soft uart(ttl) based on pigpio wiht Rx & Tx GPIO need to be set"""def __init__(self, rxPin, txPin, baud=9600, timeout=5):self._rxPin = rxPinself._txPin = txPinself._baud = baud#according to /forums/viewtopic.php?p=694626, bard>19200 is not reliableself._timeout = timeout# PIGPIOself._pi = pigpio.pi()if not self._pi.connected:os.system('sudo pigpiod')self._pi = pigpio.pi()self._pi.set_mode(self._rxPin, pigpio.INPUT)self._pi.set_mode(self._txPin, pigpio.OUTPUT)def flushInput(self):pigpio.exceptions = False#fatal exceptions off (so that closing an unopened gpio doesn't error)self._pi.bb_serial_read_close(self._rxPin)pigpio.exceptions = Trueself._pi.bb_serial_read_open(self._rxPin, self._baud, 8)#open a gpio to bit bang read, 1 byte each time.def write(self, msg):self._pi.wave_clear()self._pi.wave_add_serial(self._txPin, self._baud, msg)#create a waveform representing msgwid = self._pi.wave_create()start = time.time()self._pi.wave_send_once(wid)succeed=Truewhile self._pi.wave_tx_busy():# wait until all data sent or timeoutpassif (time.time()-start)>self._timeout:succeed=Falsebreakself._pi.wave_delete(wid)return succeeddef read(self, size):count=1text=""lt=0start = time.time()while count:(count,data)=self._pi.bb_serial_read(self._rxPin)if count:text = text+datalt = lt+countif (time.time()-start)>self._timeout:breakif len(text)==size:breaktime.sleep(0.1)# enough time to ensure more datareturn text

软串口测试

利用BCM 6(GPIO.22) 13(GPIO.23)分别作为Rx和Tx,替换噪声一文中的GPIO.15、GPIO.14,测试代码修改为:

import time#import serialimport softuartimport sysimport RPi.GPIO as GPIO#port="/dev/ttyAMA0"#usart=serial.Serial(port,9600,timeout=None)usart=softuart.softuart(6,13,9600)usart.flushInput()sendbuf = bytearray.fromhex("01 03 00 00 00 01 84 0A")while True:usart.write(sendbuf)recvbuf = bytearray(usart.read(7))b1 = int(recvbuf[3])b0 = int(recvbuf[4])result = (b1<<8) | b0print(result/10.0)#time.sleep(.05)time.sleep(1)

头2次调用之后可以正常读到数据。

sudo top可以看到,pigpiod进程占用cpu 9%左右。

如果觉得《pigpio 利用普通GPIO为树莓派增加软串口实现ttl通讯》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。