Getting an input/output error from Python with pySerial


Question

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.

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

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

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?

1
4
10/6/2012 9:00:47 PM

Accepted Answer

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.

2
6/15/2009 9:45:22 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon