通过 Raspberry Pi 3B 上的串口访问传感器
Access sensor through serial port on Raspberry Pi 3B
我遇到了以下问题,经过长时间的挫折后我无法让它工作,有人可以帮助新手吗?
我有几个 SRF02 Ranging sensors 想通过 I2C 在我的 Raspberry Pi 3B Jessie 上使用。
我按照教程将第一个传感器更改为地址 0xF2(从 Raspberry 看到的 0x79),一切都很顺利。但问题是地址 0x78 到 0x7B 是为 10 位 I2C 地址保留的,所以我必须使用另一个。 但由于我更改了地址后无法通过 I2C 访问它,所以我决定通过串口访问它。
我尝试按照 tutorial 中的描述进行更改,但它不起作用。这是代码以及我在研究后所做的工作:
我检查了传感器的地址。上电时,它会发送 1 次长闪烁和 9 次短闪烁,因此其地址在串行模式下应为 0x09。
我更新并升级了我的 Raspberry。
我将传感器 RX 引脚连接到 Rasp TX,反之亦然。传感器的 +5V 到 Rasp 的 +3V(这是问题吗?)。传感器接地和模式引脚接地。
在 raspi-config 上,我将串行控制台更改为禁用,将串行硬件端口更改为启用。
这是我的 /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
Connections as follows:
Pi 5V to device 5V Pi ground to device ground Pi ground to device mode
Pi TX to device RX
Then configure the Pi serial link to 9600.
stty -F /dev/ttyAMA0 9600
Then use echo to send commands to the device.
The first byte is the device address (0 - 15). The second byte is a
command.
To change the device from address 0 to address 5 use
echo -ne "\x00\xA0\x00\xAA\x00\xA5\x00\x05" >/dev/ttyAMA0
To change the device from address 5 back to address 0 use
echo -ne "\x05\xA0\x05\xAA\x05\xA5\x05\x00" >/dev/ttyAMA0
All the details are in the docs you linked.
我遇到了以下问题,经过长时间的挫折后我无法让它工作,有人可以帮助新手吗?
我有几个 SRF02 Ranging sensors 想通过 I2C 在我的 Raspberry Pi 3B Jessie 上使用。
我按照教程将第一个传感器更改为地址 0xF2(从 Raspberry 看到的 0x79),一切都很顺利。但问题是地址 0x78 到 0x7B 是为 10 位 I2C 地址保留的,所以我必须使用另一个。 但由于我更改了地址后无法通过 I2C 访问它,所以我决定通过串口访问它。
我尝试按照 tutorial 中的描述进行更改,但它不起作用。这是代码以及我在研究后所做的工作:
我检查了传感器的地址。上电时,它会发送 1 次长闪烁和 9 次短闪烁,因此其地址在串行模式下应为 0x09。
我更新并升级了我的 Raspberry。
我将传感器 RX 引脚连接到 Rasp TX,反之亦然。传感器的 +5V 到 Rasp 的 +3V(这是问题吗?)。传感器接地和模式引脚接地。
在 raspi-config 上,我将串行控制台更改为禁用,将串行硬件端口更改为启用。
这是我的 /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
Connections as follows:
Pi 5V to device 5V Pi ground to device ground Pi ground to device mode Pi TX to device RX
Then configure the Pi serial link to 9600.
stty -F /dev/ttyAMA0 9600
Then use echo to send commands to the device.
The first byte is the device address (0 - 15). The second byte is a command.
To change the device from address 0 to address 5 use
echo -ne "\x00\xA0\x00\xAA\x00\xA5\x00\x05" >/dev/ttyAMA0
To change the device from address 5 back to address 0 use
echo -ne "\x05\xA0\x05\xAA\x05\xA5\x05\x00" >/dev/ttyAMA0
All the details are in the docs you linked.