通过Raspberry Pi 3B上的串行端口访问传感器



我遇到了以下问题,经过长时间的挫败感,我无法工作,有人可以帮助菜鸟吗?

我想在Raspberry Pi 3B Jessie上通过I2C使用几个SRF02范围传感器。

我遵循教程,然后更改了第一个传感器以地址为0xf2(从Raspberry中看到的0x79(,一切都很好。但是问题在于,地址为0x78至0x7b是为10位i2c的地址保留的,因此我必须使用另一个。,但是由于我无法通过I2C访问它,因为我更改了地址,因此决定通过串行端口进行操作。

我尝试按照教程中的描述进行更改,但它不起作用。这是代码以及我在研究之后所做的工作:

  • 我检查了传感器的地址。电源时,它会发送一个长闪存和9个短闪光灯,因此其地址应在串行模式下为0x09。

  • 我更新并升级了我的覆盆子。

  • 我将传感器Rx引脚连接到RASP TX和Viceversa。传感器的 5V到RASP的 3V(这是一个问题吗?(。传感器接地和模式销接地。

  • 在raspi-config上,我将串行控制台更改为禁用的串行控制台,并启用了串行竖琴端口。

这是my/boot/cmdline.txt:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

我将以下内容添加到我的/boot/config.txt:

enable_uart=1
dtoverlay=pi3-disable-bt
core_freq=250

我的python代码(通过发送范围命令,传感器应该闪烁一次,但这不是,我也不会得到任何结果(:

import serial
import time
ser = serial.Serial(port='/dev/serial0', baudrate = 9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_TWO, timeout=1, write_timeout=5)
USED_ADDRESS = '0x09'
TARGET_ADDRESS = '0x03'
# GET DISTANCE IN CM
ser.write(USED_ADDRESS)
ser.write('0x51')
time.sleep(0.07)
ser.write(USED_ADDRESS)
ser.write('0x5E')
results = ser.read(2)
if(results != None and len(results) > 0):
        print 'RESULTS:'
        for result in results:
                print result
else:
        print 'NO RESULT'
#CHANGING ADDRESS
#First command
ser.write(USED_ADDRESS)
ser.write('0xA0')
#Second command
ser.write(USED_ADDRESS)
ser.write('0xAA')
#Third command
ser.write(USED_ADDRESS)
ser.write('0xA5')
#Target Address
ser.write(USED_ADDRESS)
ser.write(TARGET_ADDRESS)
print "DONE"

DMESG的结果|grep tty:

[    0.000000] Kernel command line: 8250.nr_uarts=1 bcm2708_fb.fbwidth=640 bcm2708_fb.fbheight=480 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
[    0.000312] console [tty1] enabled
[    0.748748] 3f201000.serial: ttyAMA0 at MMIO 0x3f201000 (irq = 87, base_baud = 0) is a PL011 rev2

LS -L/dev/serial*的结果:

lrwxrwxrwx 1 root root 7 Feb 16 15:06 /dev/serial0 -> ttyAMA0
lrwxrwxrwx 1 root root 5 Feb 16 15:06 /dev/serial1 -> ttyS0

有人有一个主意吗?对于任何提示,我都会非常多。

好的,我解决了!我在这里找到了解决方案:https://www.raspberrypi.org/forums/viewtopic.php?t=63419

连接如下:

pi 5V到设备5V PI接地到设备接地pi接地到设备模式 PI TX到设备RX

然后将PI串行链接配置为9600。

stty -f/dev/ttyama0 9600

然后使用回声将命令发送到设备。

第一个字节是设备地址(0-15(。第二个字节是 命令。

将设备从地址0更改为地址5使用

echo -ne" x00 xa0 x00 xaa x00 x00 xa5 x00 x00 x05">/dev/dev/ttyama0

将设备从地址5返回地址0使用

echo -ne" x05 xa0 x05 xaa x05 xa5 xa5 x05 x00">/dev/dev/ttyama0

所有详细信息都在您链接的文档中。

最新更新