Delay between trigger and polygon apparition

Dear Psychopy community,

I am setting up and experiment in which I have to send a trigger at the start of the apparition of a figure. The trigger is sent via a code block of the routine using the serial python toolbox. The problem is that I get delays between ~20-50ms. The delay occurs between the apparition of the polygon and sending of the trigger pulse. To measure this delay we used the logic8 device. This allowed us to use a diode to detect the apparition of the stimulus in the screen and the time when the trigger (serial port) is sent.

Since I have to synchronize the EEG signal to polygon apparition using the trigger pulse, the delay between the the polygon apparition and the trigger pulse being sent is a huge problem. Is there a better way to send the delay?
I attach the script used to send the trigger and the csv with the pulse and diode signal.

cabina_fijo_digital.csv (4.1 KB)
test_jitter.psyexp (22.1 KB)