使用 pySerial 从 Python 获取输入/输出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/994538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:14:04  来源:igfitidea点击:

Getting an input/output error from Python with pySerial

pythonserial-portarduinopyserial

提问by Janusz

I have a Python script that writes data packets to an Arduino board through pySerial. Sometimes while writing the code to the board pySerial raises an input/output error with errno 5.

我有一个 Python 脚本,它通过pySerial将数据包写入 Arduino 板。有时在将代码写入板时 pySerial 会引发输入/输出错误,错误号为 5。

Some research says that this indicates an error while writing in the file representing the connection to the Arduino board.

一些研究表明,这表明在写入表示与 Arduino 板的连接的文件时出现错误。

The code that sends, sends only single byte packets:

发送的代码仅发送单字节数据包:

try:
    # Check if it's already a single byte
    if isinstance(byte, str):
        if len(byte) == 1: # It is. Send it.
            self.serial.write(byte)
        else: # It's not
            raise PacketException
    # Check if it's an integer
    elif isinstance(byte, int):
        self.serial.write(chr(byte)) # It is; convert it to a byte and send it
    else: raise PacketException # I don't know what this is.
except Exception as ex:
    print("Exception is: " + ex.__getitem__() + " " + ex.__str__())

The error printed by this code is:

此代码打印的错误是:

OS Error Input/Output Error Errno 5

操作系统错误输入/输出错误 Errno 5

Is there something wrong in my code while sending? Do I need to check if the serial connection is ready to send something or should there be a delay after the sending? Or could there be a problem with the hardware or the connection with the hardware?

发送时我的代码有问题吗?我是否需要检查串行连接是否准备好发送某些内容,或者发送后是否应该有延迟?还是硬件或与硬件的连接有问题?

Edit: I looked into the Linux implementation from pyserial and the implementation is only passing the error to my code. So no new real insights from there. Is there a good way to test what is happening in the program?

编辑:我从 pyserial 中查看了 Linux 实现,该实现只是将错误传递给我的代码。所以没有新的真正见解。有没有好的方法来测试程序中发生的事情?

采纳答案by Janusz

Sorry to have bothered you but I'm very sure that the error is caused by the arduino resetting itself and therefore closing the connection to the computer.

很抱歉打扰您,但我非常确定该错误是由 arduino 自行重置并因此关闭了与计算机的连接引起的。

回答by Alex Martelli

The only issue I can immediately see in your code is an indentation problem -- change your code as follows:

我可以立即在您的代码中看到的唯一问题是缩进问题——按如下方式更改您的代码:

try:
    # Check if it's already a single byte
    if isinstance(byte, str):
        if len(byte) == 1: # It is. Send it.
            self.serial.write(byte)
        else: # It's not
            raise PacketException
    # else, check if it's an integer
    elif isinstance(byte, int): 
        self.serial.write(chr(byte)) # It is; convert it to a byte and send it 
    else: 
        raise PacketException # I don't know what this is.
except Exception as ex:
    print("Exception is: " + ex.__getitem__() + " " + ex.__str__())

I doubt your error comes from this, but try it this way and let us know! You were checking if byteis an intonly in the case in which it's a str, so the elifalwaysfailed by definition. But I think if you realcode indentation had been like this, you'd have gotten a SyntaxError, so I think you just erred in posting and your real problem remains hidden.

我怀疑您的错误来自于此,但请以这种方式尝试并告诉我们!您正在检查 if byteis an intonly 在它是 a 的情况下str,所以根据定义elif总是失败。但我认为如果你真正的代码缩进是这样的,你会得到一个SyntaxError,所以我认为你只是在发布时犯了错误,你的真正问题仍然隐藏。

回答by Dan Lorenc

If you are running this on Windows, you can't have the Arduino IDE open with a serial connection at the same time that you run your Python script. This will throw the same error.

如果您在 Windows 上运行它,则不能在运行 Python 脚本的同时通过串行连接打开 Arduino IDE。这将引发相同的错误。

回答by Peter Schaeffer

Let me try to offer a few comments that might be helpful to you and other folks with similar problems. First, try to run your Arduino sketch with the Serial Monitor a few times. You can find the Serial Monitor under Tools in the IDE menu. You can also type Ctrl-Shift-M to invoke the Serial Monitor.

让我尝试提供一些评论,这些评论可能对您和其他有类似问题的人有所帮助。首先,尝试使用串行监视器运行您的 Arduino 草图几次。您可以在 IDE 菜单的工具下找到串行监视器。您还可以键入 Ctrl-Shift-M 来调用串行监视器。

The Serial Monitor displays what the Arduino sketch sends back to you. However, it also lets you type in data that gets sent to the Arduino sketch. In other words you test and debug both sides of the serial data flow, just using the Serial Monitor.

串行监视器显示 Arduino 草图发送回给您的内容。但是,它还允许您输入发送到 Arduino 草图的数据。换句话说,您只需使用串行监视器即可测试和调试串行数据流的两端。

Look at what shows up. It will frequently be quite helpful assuming your sketch tries to send data back via Serial.print(). A few notes. Make absolutely sure that the baud rate set inside the Serial Monitor exactly matches the baud rate in your sketch (9600 is a good choice in almost all cases).

看看出现了什么。假设您的草图尝试通过 Serial.print() 发回数据,这通常会很有帮助。一些笔记。确保串行监视器中设置的波特率与草图中的波特率完全匹配(几乎在所有情况下 9600 都是不错的选择)。

The second note is critical. Bringing up the Serial Monitor forces a reset on the Arduino board. Your sketch starts over (always). This is a good thing because it gives you a fresh run each time. Note that you can force a reset, just by setting the baud rate to 9600 (even if it is already 9600). This lets you run many tests inside the Serial Monitor without having to restart the Serial Monitor each time.

第二个注释很关键。启动串行监视器会强制重置 Arduino 板。你的草图(总是)重新开始。这是一件好事,因为它每次都能让您焕然一新。请注意,您可以强制重置,只需将波特率设置为 9600(即使它已经是 9600)。这使您可以在串行监视器内运行许多测试,而无需每次都重新启动串行监视器。