Serial port no longer working after force quitting psychopy

Hi all,

New to psychopy and coding generally! We are adapting an experimental task coded in psychopy3 and using pyserial to event mark. Our setup is PsychoPy3 → BlackBox USBTTL module → STP100D → MP150 → Aquisition Graph template on MacBook Pro. I attached an image at the bottom that contains the digital acquisition channels that can be recorded in AcqKnowledge/BIOPAC. The serial port was working and we were able to event mark using several softwares including the USB TTL Configuration Utility, Eprime, python IDLE, and psychopy3. However, after force quitting Psychopy we are no longer able to send event markers via any of the softwares. We believe this issue was caused because the serial port was not closed cleanly in PsychoPy, but we cannot figure out how to fix it. We have tried a number of things including restarting the computer, reinstalling the serial port and drivers, changing the COM port number, and running lines of code in Psychopy and python IDLE. We have read through existing threads on event marking via psychopy and the BlackBox TTL module: Event markers to BIOPAC via serial port

We have not been getting error messages with our code, and even though it says it is sending bytes, we do not see any event markers in the acquisition graph.

When running the following code snippet, psychopy did report that the serial port was opening and closing as expected:

import serial
port_name = 'COM4'
 
try:
    # Attempt to create a serial port object
    port = serial.Serial(port_name)
 
    # Check if the port is open
    if port.is_open:
        print(f"The serial port {port_name} is currently open. Closing it now.")
        port.close()
    else:
        print(f"The serial port {port_name} is already closed.")
 
except serial.SerialException as e:
    print(f"Error: Could not access the serial port {port_name}. Details: {e}")
 
print("Serial port check and closure complete.")
 

We have also tried codes to flush the serial port:

ser.reset_input_buffer()
ser.reset_output_buffer()

And code to adjust the flow control settings:

try:
    port = serial.Serial(
        'COM4',
        baudrate=115200,
        timeout=1,
        xonxoff=True,
        rtscts=True,
        dsrdtr=True
    )
    port.write("RR".encode())  # Reset USB TTL Module
    port.close()
except serial.SerialException as e:
    print(f"Serial port error: {e}")

Here is an example snippets of code that were working (i.e., event marking in the appropriate digital channels) prior to this issue.

Import serial
port = serial.Serial(‘COM4’, baudrate = 115200, timeout = 0)
port.write(“RR”.encode()) #Turns all digital channels off (i.e., 0), resetting them
port.write(“FF”.encode()) #Turns all digital channels on
port.write(“RR”.encode()) 
Port.write(“01”.encode()) #Turns on channel 28
port.write(“RR”.encode())
Port.write(“02”.encode()) #Turns on channel 29

On a different setup (Biosemi) one day PsycoPy was not seeing out COM4. After hours of troubleshooting I realised, it was Windows (10) that was not seeing the port and indeed it was not appearing in the device manager.
After a few machine restarts, the port reappeared, PsychoPy was seeing it and it was working as normal.
Long shot but check if your port appears in the device manager.

Yiannis

Thanks for your response! Our serial port (COM4) does show up in the device manager and PsychoPy is recognizing it. It almost seems like the serial port connection has been corrupted/kept busy after PsychoPy did not cleanly close it.