I have a Python script to communicate with a measuring instrument over a RS232 serial port. Everything works fine, but every time I turn on the PC (Windows 10) the communication doesn't work in the beginning. I have to open a serial terminal (for example hterm) press the "connect" and "disconnect" button. After that the Python script works as expected, reading and writing to and from the instrument is no problem. Here is a short example of the code:
import serial, time
ser = serial.Serial(port='COM6', baudrate=19200, bytesize=8, parity=serial.PARITY_NONE, stopbits=1, timeout=0, xonxoff=False, rtscts=False, dsrdtr=False)
time.sleep(1)
print(ser.isOpen()) #output: true
ser.write(b'READ:CH1\r\n')
time.sleep(1)
print("read:" + ser.read(18).decode('utf-8'))
ser.close()
print(ser.isOpen()) #output: false
The instrument doesn't receive the data "READ:CH1" or any other command. Because of this there isn't any transmitted data to the PC via ser.read(). I tried every possibility with hardware handshakes and very long sleep times. I guess there's a problem between Windows and Pyserial. In Python the port is open, but Windows doesn't send the data. Do you have any ideas what I could do? Thanks for your help.
Best regards
Edit with solution:
Instead of or additional to "Serial.flushInput()" and "Serial.flushOutput()" you need "Serial.reset_input_buffer()" and "Serial.reset_output_buffer()".