'writing, then reading serial port in python changes when printing?

I am communicating with different devices on a RS485 network with a single RS485MAX device running through the UART pins on a Raspberry Pi.

It is using serial port /dev/serial0.

The UART flipper is used to flip the RS485MAX device from send to receive and back.

It works fine when communicating with other devices, but for one in particular, it only reads the response from the serial port if there is a print statement:

with Serial(port=rs485_manager.port,
                            baudrate=rs485_device.baud_rate,
                            bytesize=rs485_device.byte_size,
                            parity=rs485_device.parity,
                            stopbits=rs485_device.stop_bits,
                            timeout=rs485_device.read_timeout) as s:
                payload = send_packet.get_payload()
                rs485_manager.uart_flipper.setup_for_write()
                #
                # Note that this sleep breaks it again, even if the print statement is there
                #
                time.sleep(1)

                print(flush=True)  # tried adding this - makes no difference
                s.reset_output_buffer()  # tried adding this - makes no difference
                s.reset_input_buffer()  # tried adding this - makes no difference
                s.flush()  # tried adding this - makes no difference

                s.write(payload)
                #
                # If I add this print statement, it receives the packet correctly from the serial port:
                #
                print('this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test')
                rs485_manager.uart_flipper.setup_for_read()

                while s.in_waiting > 0:
                    received_bytes += s.read(s.in_waiting)

                print('read bytes: {}'.format(as_hex(received_bytes)))

When the print statement is present, it very consistently reads the data from the comm port (unless I have a sleep statement before it - see comments above) I have tried substituting the print statement with a sleep (even up to a few seconds!) and it does not fix the problem. Its almost as if the stdout is affecting the serial port



Solution 1:[1]

OK... After much thinking, testing, scoping, hair-pulling, I think that the problem is with the RS485MAX. It takes too long to flip the transmit/receive switch and for some reason the random amount of time it took to write to the file did something to occasionally allow it to work.

I replaced it with a conventional USB-RS485 dongle and it is working fine with no bizarre timing issues.

My take-away: if you have PLENTY of time to flip between transmit and receive, the RS485MAX may work for you... Otherwise, buyer beware ;-)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jordanthompson