Getting input / output error from Python using pySerial

I have a Python script that writes data packets to an Arduino board via pySerial . Sometimes when writing code on the board, pySerial causes an I / O error with error 5.

Some studies indicate that this indicates an error while writing to a file representing the connection to the Arduino board.

The code that sends sends only single-byte packets:

try: # Check if it already a single byte if isinstance(byte, str): if len(byte) == 1: # It is. Send it. self.serial.write(byte) else: # It not raise PacketException # Check if it 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__()) 

Error printed by this code:

Errno 5 OS I / O Error Error

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

Change I reviewed the Linux implementation from pyserial, and the implementation only passes the error to my code. So there are no new real ideas from there. Is there a good way to check what is happening in the program?

+4
source share
4 answers

Sorry to bother you, but I’m very sure that the error was caused by arduino self-healing and, therefore, closing the connection to the computer.

+2
source

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

 try: # Check if it already a single byte if isinstance(byte, str): if len(byte) == 1: # It is. Send it. self.serial.write(byte) else: # It not raise PacketException # else, check if it 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 mistake comes from this, but try it this way and let us know! You checked if byte int only if it is str , therefore, by definition, elif always . But I think that if you inserted the real code, you would get a SyntaxError , so I think that you simply were mistaken in the publication and your real problem remains hidden.

+1
source

If you run this on Windows, you cannot open the Arduino IDE with a serial connection while running the Python script. This will cause the same error.

+1
source

Let me try to suggest a few comments that may be useful to you and other people with similar problems. First, try running the Arduino sketch several times using the Serial Monitor. The serial monitor can be found in the Tools menu of the IDE menu. You can also enter Ctrl-Shift-M to call up the Serial Monitor.

The serial monitor displays what the Arduino sketch sends you. However, it also allows you to enter data that is sent to an Arduino sketch. In other words, you test and debug both sides of the serial data stream just by using a serial monitor.

Look what appears. This will often be useful if your sketch is trying to send data back through Serial.print (). A few notes. Make sure that the baud rate set in Serial Monitor exactly matches the baud rate in your sketch (9600 is a good choice in almost all cases).

The second note is crucial. Bringing Serial Monitor forces a reset on the Arduino board. Your sketch begins (always). This is good because it gives you a fresh turn every time. Please note that you can force reset by setting the baud rate to 9600 (even if it is already 9600). This allows you to run many tests inside the serial monitor without having to restart the serial monitor every time.

0
source

Source: https://habr.com/ru/post/1286212/


All Articles