简体   繁体   English

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

[英]Access sensor through serial port on Raspberry Pi 3B

I've got the following problem and after long hours of frustration I cannot get it to work, could somebody please help a noob out? 我遇到了以下问题,经过长时间的挫败后,我无法正常工作,有人可以帮助菜鸟吗?

I've several SRF02 Ranging sensors which I wanted to use through I2C on my Raspberry Pi 3B Jessie. 我有几个SRF02测距传感器 ,希望通过I2C在Raspberry Pi 3B Jessie上使用。

I followed the tutorial and changed the first sensor to address 0xF2 (0x79 seen from Raspberry) and it all went perfectly. 我按照教程进行操作,将第一个传感器更改为地址0xF2(从Raspberry中看到的地址为0x79),一切正常。 But the problem is that the addresses 0x78 to 0x7B are reserved for 10-bit I2C Adressing, so I have to use another one. 但是问题是地址0x78到0x7B保留用于10位I2C地址,所以我必须使用另一个地址。 But since I cannot access it anymore through I2C since I changed the address I decided to do it through the serial port. 但是由于更改地址后无法再通过I2C访问它,因此决定通过串行端口进行访问。

I tried changing it as described in the tutorial but it doesn't work. 我尝试按照本教程中的描述进行更改,但是它不起作用。 Here is the code and what I did after research to get it done: 这是代码,经过研究,我将其完成:

  • I checked the address of the Sensor. 我检查了传感器的地址。 On powering up it sends one long and 9 short flashes so its address should be 0x09 in serial mode. 上电时,它将发送一长和短九次闪烁,因此在串行模式下其地址应为0x09。

  • I updated and upgraded my Raspberry. 我更新并升级了Raspberry。

  • I connected the sensors RX pin to the Rasp TX and viceversa. 我将传感器的RX引脚连接到Rasp TX,反之亦然。 +5V of the sensor to +3V of the Rasp (Is this a problem?). 传感器的+ 5V到锉刀的+ 3V(这是问题吗?)。 Sensors ground and mode pin to ground. 传感器接地,并且模式引脚接地。

  • On raspi-config I changed the serial console to disabled and the serial harware ports to enabled. 在raspi-config上,我将串行控制台更改为禁用,将串行硬件端口更改为启用。

Here is my /boot/cmdline.txt: 这是我的/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

I added the following to my /boot/config.txt: 我在/boot/config.txt文件中添加了以下内容:

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

My python code (By sending the range commands the sensor should flash once, but it doesn't, also I don't get any Result): 我的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"

Result of dmesg | dmesg的结果| grep tty: 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

Result of ls -l /dev/serial*: 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

Somebody got an idea? 有人知道吗? I would be super-duper gratefull for any hint. 任何提示我都会让我倍感欣慰。

Ok guys I solved it! 好的,我解决了! I found the solution here: https://www.raspberrypi.org/forums/viewtopic.php?t=63419 我在这里找到了解决方案: https : //www.raspberrypi.org/forums/viewtopic.php?t=63419

Connections as follows: 连接方式如下:

Pi 5V to device 5V Pi ground to device ground Pi ground to device mode Pi TX to device RX Pi 5V至设备5V Pi接地至设备接地Pi接地至设备模式Pi TX至设备RX

Then configure the Pi serial link to 9600. 然后将Pi串行链接配置为9600。

stty -F /dev/ttyAMA0 9600 stty -F / dev / ttyAMA0 9600

Then use echo to send commands to the device. 然后使用echo将命令发送到设备。

The first byte is the device address (0 - 15). 第一个字节是设备地址(0-15)。 The second byte is a command. 第二个字节是命令。

To change the device from address 0 to address 5 use 要将设备从地址0更改为地址5,请使用

echo -ne "\\x00\\xA0\\x00\\xAA\\x00\\xA5\\x00\\x05" >/dev/ttyAMA0 echo -ne“ \\ x00 \\ xA0 \\ x00 \\ xAA \\ x00 \\ xA5 \\ x00 \\ x05”> / dev / ttyAMA0

To change the device from address 5 back to address 0 use 要将设备从地址5更改回地址0,请使用

echo -ne "\\x05\\xA0\\x05\\xAA\\x05\\xA5\\x05\\x00" >/dev/ttyAMA0 echo -ne“ \\ x05 \\ xA0 \\ x05 \\ xAA \\ x05 \\ xA5 \\ x05 \\ x00”> / dev / ttyAMA0

All the details are in the docs you linked. 所有详细信息都在您链接的文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM