Raspberry Pi未通过USB连接到Arduino

时间:2015-09-20 20:54:22

标签: python arduino serial-port raspberry-pi

我遵循本教程:(http://www.seeedstudio.com/recipe/index.php?controller=recipe&action=show&recipe_id=166

我通过USB将Raspberry Pi连接到Arduino

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

然而,当我将命令放在python脚本中并执行.py文件时,无论我通过的数字是多少,引脚13上的LED指示灯都会快速闪烁3或4次,然后再次暂停并缓慢闪烁。 / p>

我无法理解它为什么不按预期运行。 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")

1 个答案:

答案 0 :(得分:0)

解决!

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

我的代码是:

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

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