Problems with python serial communication

Asked

Viewed 65 times

2

So, I’m developing a project for the university, it’s basically the development of a supervisory. I have to read some I/O modules that communicate over an RS485 network. Communication with the main module is through serial communication, but I’m having trouble reading it. Using the serial package, in python, I read it as follows:

response = bytearray()
print("Sending " + cmd)

ser.open()

if ser.inWaiting() > 0:
        ser.reset_input_buffer()

# Envia os dados e recebe a resposta
if ser.isOpen():
    ser.write(cmd.encode('ascii'))
    time.sleep(SLEEP_VALUE)
    while ser.inWaiting() == 0:
        ser.write(cmd.encode('ascii'))
        time.sleep(SLEEP_VALUE)

    response.extend(ser.read((BIGGEST_N_OUTPUT * 8) + 2))  # Leitura de até 8 sinais + '>'
else:
    print("ERRO DE CONEXAO COM PORTA SERIAL")

Where "cmd" is the variable that contains the codes to be sent to the module. At the end of it there is a line break (as requested by the module manufacturer’s manual):

# Comando a ser escrito
cmd = '#' + str(ID) + str(channel) + '\r\n'

This piece of code is in an eventual loop to perform several sequential readings. It turns out that in some random scripts, when using the be.Write command, I get no response from the module, resulting in an input buffer (be.inWaiting) equal to zero. For this reason I wrote the while loop so that the program tries to spin writing repeatedly until it gets a response.

Is there any better way to perform the readings? I have tried to change the timeout time, or even add a higher break value in the team.Leep(), but even with a very big break the result is the same. I also reset the buffer before each reading, to ensure the absence of other errors.

  • 1

    If you’ve managed to exchange data with the serial, you’re halfway there. You are probably sending data without a finish line, and the data gets "stuck" waiting for a "newline" or close the connection to be sent. If so, after each writing, and before the "Sleep", call the port "flush" method: ser.flush() .

  • 1

    Another tip before moving forward on your project: I hope you have understood how functions work and make good use of them Leaving communication code spread by the module as you are in this example is bad practice and will be a blocking factor for you to develop the project. If you haven’t already, take the opportunity to study this part by creating several examples.

  • @jsbueno The command ser.flush() decreased the amount of read/write errors, but they keep happening. I do not know if the end of the line commented would be a de facto line break. The question was updated. But anyway it helped a lot.

  • I don’t think you can answer more than that - even if you improve the code of the question by putting a complete and verifiable example, without being experimenting together.

  • Try to improve the code on the question: put a complete list of the program, at least by writing and reading a message - so that if someone was with your device here, they could copy and paste your code and run it. May have to do with cross-use of the interface, among other things.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.