views:

840

answers:

3

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. Could you think of a good way to test what is happening in the program?

+1  A: 

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 byte is an int only in the case in which it's a str, so the elif always failed by definition. But I think if you real code 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.

Alex Martelli
you are right about the intendation :)
Janusz
+1  A: 

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.

Dan Lorenc
I thought about an error too that two programs or scripts write to the arduino. We run this program on a linux machine and the script is the only thing writing. There are no threads in the program. And before the error appears some packets are send succesfully. So it seems unlikely that a second program writes to the connection.
Janusz
+1  A: 

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.

Janusz