Pergunta

Eu tenho um script python que grava pacotes de dados em uma placa Arduino Pyserial. Às vezes, ao escrever o código para a placa, o Pyserial aumenta um erro de entrada/saída com Errno 5.

Algumas pesquisas dizem que isso indica um erro ao escrever no arquivo que representa a conexão com o conselho do Arduino.

O código que envia, envia apenas pacotes de bytes únicos:

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__())

O erro impresso por este código é:

Erro de erro do sistema operacional Erro de entrada/saída Errno 5

Há algo errado no meu código enquanto envia? Preciso verificar se a conexão serial está pronta para enviar algo ou deve haver um atraso após o envio? Ou poderia haver um problema com o hardware ou a conexão com o hardware?

Editar: Olhei para a implementação do Linux da Pyserial e a implementação está passando apenas o erro para o meu código. Portanto, não há novas idéias reais a partir daí. Existe uma boa maneira de testar o que está acontecendo no programa?

Foi útil?

Solução

Lamento ter incomodado você, mas tenho certeza de que o erro é causado pela redefinição do Arduino e, portanto, fechando a conexão com o computador.

Outras dicas

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.

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.

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.

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.

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).

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top