今天调试串口,先折腾pyserial,弄死不能装pip,好似被墙了。
推荐如下方法,秒胜!!!
从https://pypi.python.org/pypi/pyserial下载最新版本的安装包,再通过下面的命令完成安装:
- tar zxvf pyserial-2.7.tar.gz
- cd pyserial-2.7
- python setup.py install
然后把串口收发对接,自己收发,可以完成!耶!
代码参照
https://github.com/winlinvip/raspi-tools/blob/master/raspi-pl2303.py
合并代码开始,麻蛋,超声波为什么不干活了!!!
弄了两个小时也没整明白,树莓派重启也不行,最后关机掉电再开机,又干活了?!纳尼?!
很多时候就是这样的,不知道为什么,重新上电吧。。。唉。。。
po一轮代码
参考了 http://blog.chinaunix.net/uid-21658993-id-4694734.html 还有上面的串口。
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import time
import serial
def checkdist():
GPIO.output(2,GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(2,GPIO.LOW)
while not GPIO.input(3):
pass
t1 = time.time()
while GPIO.input(3):
pass
t2 = time.time()
return (t2 - t1) * 340/2
def uart_open(device="/dev/ttyUSB0"):
return serial.Serial(device,115200)
#init GPIO&UART
GPIO.setmode(GPIO.BCM)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
GPIO.setup(3,GPIO.IN)
uart = uart_open()
dist = checkdist()
print 'distance: %0.2f m' %dist
dis_cnt = 0
time.sleep(2)
data=b"\xff\x31\x15"
uart.write(data)
try:
while True:
dist = checkdist()
#print 'distance: %0.2f m' %dist
if dist<0.4:
dis_cnt += 1
else:
dis_cnt = 0
data=b"\xff\x31\x15"
uart.write(data)
data=b"\xff\x32\x15"
uart.write(data)
if dis_cnt >5:
dis_cnt = 0
print 'dis = %0.2f m ,change dirc'%dist
data=b"\xff\x31\x15"
uart.write(data)
data=b"\xff\x32\x15"
uart.write(data)
time.sleep(5)
data=b"\xff\x31\x15"
uart.write(data)
data=b"\xff\x32\x15"
uart.write(data)
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
多次遇到小于40cm,发串口去stm8控制pwm波转向。
明天终于可以上机了!耶!
好困啊,原来除了看小说不完不睡觉综合征以外,再来一条debug不通不睡觉综合征。遭老公半夜起来批评。。。