【问题标题】:Raspberry Pi Not Connecting to Arduino via USBRaspberry Pi 未通过 USB 连接到 Arduino
【发布时间】:2015-12-17 12:03:53
【问题描述】:

我正在关注本教程:(http://www.seeedstudio.com/recipe/index.php?controller=recipe&action=show&recipe_id=166)

我将 Raspberry Pi 通过 USB 连接到 Arduino

当我在终端中运行 python 时,命令按预期工作,arduino 会按指示闪烁。

但是,当我将命令放入 python 脚本并执行 .py 文件时,无论我传递的数字是多少,引脚 13 上的 LED 都会快速闪烁 3 或 4 次,然后暂停并再次缓慢闪烁。

我不明白为什么它没有按预期运行。 arduino 解释通过终端 python (>>>) 发送的串行消息和执行 arduino.py 的方式有明显的区别。

当我运行 dmesg 时,我得到:

pi@raspberrypi ~ $ dmesg | grep tty

[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2709.boardrev=0xa01041 bcm2709.serial=0x4c5954ea smsc95xx.macaddr=B8:27:EB:59:54:EA bcm2708_fb.fbswap=1 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
[    0.002073] console [tty1] enabled
[    0.195158] 3f201000.uart: ttyAMA0 at MMIO 0x3f201000 (irq = 83, base_baud = 0) is a PL011 rev2
[    6.986035] usb 1-1.3: ch341-uart converter now attached to ttyUSB0
[  271.283091] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[  273.926013] usb 1-1.5: ch341-uart converter now attached to ttyUSB0
[  455.883149] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[  530.727358] usb 1-1.3: ch341-uart converter now attached to ttyUSB0

当我在终端运行 python --version 时,我得到 2.7.3

当我在脚本中打印 sys.version 时,它显示 [GCC 4.6.3]

arduino.py:

#!/usr/bin/env python
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.write("3")

【问题讨论】:

  • 如果您按照教程进行操作,pi 部分会显示 ser.write('3'),因此它会闪烁 3 次。如果您将其更改为 ser.write('9') 它将闪烁 9 次。你期待它做什么?
  • 它没有。它总是快速闪烁 3 次,等待一秒钟然后再闪烁一次。无论我放 ser.write("3") 还是 ser.write("30")
  • 看一下代码:它的作用是从传入的字符中减去'0'。它只接受一个字符。因此,如果您发送了介于“1”和“9”之间的任何内容,它将闪烁该次数。如果你知道 ASCII 集合,那么你就会知道 9 之后的序列是 :;?。因此,如果您发送“?”,您将获得 15 次闪烁。如果你想让它把“30”解释为30,那么Loop中的代码就需要改变了。
  • 更新:如果我在终端中加载 python,它会按预期工作。如果我将相同的命令放在 arduino.py 中并运行“sudo python arduino.py”,则会出现上述错误
  • 你能发布 arduino.py 吗?它有 #!/usr/bin/python 作为第一行吗?此外,您不需要 sudo 来运行脚本 - sudo chmod 666 /dev/ttyAMA0 。之后,您无需进入 sudo 即可运行 arduino.py。

标签: python arduino serial-port raspberry-pi


【解决方案1】:

解决了!

似乎打开串行连接导致我的 Arduino 重置...打开连接后我放了一个 time.sleep(3)。

我的代码是:

import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600)
time.sleep(3)
ser.write('2')

很奇怪,这个问题发生在可执行文件中,但不在控制台中。

【讨论】:

  • 可能是因为写入 ser= 和 ser.write 的时间大约是 3 秒?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
相关资源
最近更新 更多