使用pySerial等待Arduino自动重置 [英] Wait on Arduino auto-reset using pySerial

查看:131
本文介绍了使用pySerial等待Arduino自动重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Linux上用非常简单的代码(为了展示问题)从Arduino板上读取行.

I'm trying to read lines from an Arduino board with a very simple code (for the sake of showcasing the problem) on Linux.

Python代码:

# arduino.py
import serial
arduino = serial.Serial('/dev/ttyACM0')

with arduino:
    while True:
        print(arduino.readline())

Arduino代码:

Arduino code:

// simpleWrite.ino
long ii = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  Serial.println(ii);
  ii++;
}

由于打开串行连接时板会自动复位,因此前几个字节很可能是垃圾.一两秒钟后,一切正常.

As the board auto-resets when the serial connection is opened, the first bytes are likely garbage. After a second or two everything works fine.

这是典型的输出:

$ python arduino.py 
b'09\r\n'
b'540\r\n'
b'541\r\n'
b'542\r\n'
b'543\r\n'
b'544\r\n'
b'545\r\n'
b'546\r\n'
b'547\r\n'
b'548\r\n'
b'549\r\n'
b'550\r\n'
b'551\r\n'
b'552\r\n'
b'553\r\n'
b'554\r\n'
b'555\r\n'
b'556\r\n'
b'557\r\n'
b'55\xfe0\r\n'  # <---- Here the board restarted
b'1\r\n'
b'2\r\n'
b'3\r\n'
b'4\r\n'
b'5\r\n'
b'6\r\n'
b'7\r\n'
b'8\r\n'
b'9\r\n'
b'10\r\n'

但是,我看到Arduino IDE串行监视器没有这个问题,并正确显示了延迟(重新启动时),然后打印了从第一个开始的所有行.

However, I see the Arduino IDE Serial Monitor doesn't have this problem, and properly shows a delay (while restarting) and then prints all the lines starting from the first one.

是否可以使用pySerial在Python中模拟此行为?,也就是说,在重新启动之前丢弃所有输出,仅此而已?也许通过一些底层功能?

Is there a way to emulate this behaviour in Python using pySerial? That is, discarding all the output before restarting and nothing more? Perhaps through some low-level functions?

我尝试查看相关的Arduino源代码,但我不知道Java,它也无济于事.

I tried looking at the relevant Arduino source code, but I don't know Java and it didn't help.

注意:当然,我可以睡三秒钟,丢弃所有内容,然后从那里开始,但是我可能也会丢弃一些第一行.

Note: Of course I could sleep for, say, three seconds, discard everything and start from there, but I would probably discard some of the first lines too.

编辑:显然,Windows上不存在此问题,因此不需要接受的解决方案.

Edit: Apparently, this problem doesn't exist on Windows and the accepted solution was not necessary.

推荐答案

连接时,Arduino IDE的监视器切换器是端口的分配DTR引脚.这种切换会导致Arduino复位.请注意,在显示器打开串行端口并准备接收数据之后,将切换DTR.在您的情况下,以下示例应执行相同的操作.

The Arduino IDE's monitor toggle's the assigned DTR pin of the port when connected. Where this toggling causes a reset on the Arduino. Noting that the DTR is toggled after the Monitor has opened the Serial port and is ready to receive data. In your case the below example should do the same.

Import serial

arduino = serial.Serial('/dev/ttyS0',
                     baudrate=9600,
                     bytesize=serial.EIGHTBITS,
                     parity=serial.PARITY_NONE,
                     stopbits=serial.STOPBITS_ONE,
                     timeout=1,
                     xonxoff=0,
                     rtscts=0
                     )
# Toggle DTR to reset Arduino
arduino.setDTR(False)
sleep(1)
# toss any data already received, see
# http://pyserial.sourceforge.net/pyserial_api.html#serial.Serial.flushInput
arduino.flushInput()
arduino.setDTR(True)

with arduino:
    while True:
        print(arduino.readline())

我还将在具有内置USB的AVR(如Leonoardo,Esplora等)的Arduino的DTR上添加称赞. setup()应该具有以下时间,以等待主机打开USB.

I would also add the compliment to the DTR for the Arduino's with AVR's using built-in USB, such as the Leonoardo, Esplora and alike. The setup() should have the following while, to wait for the USB to be opened by the Host.

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

这对于基于FTDI的UNO等等都没有影响.

It will have no effect for FTDI's based UNO's and such.

这篇关于使用pySerial等待Arduino自动重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆