Issue sending multiple commands to serial port only when using PsychoPy

Hi there,

I am using the module pyserial () to send commands to a serial port in psychopy, using ser.write().

The majority of my script works fine, but I have a particular condition where I want to send two commands sequentially to my serial port. Here is the following code:

elif catch_trial == 0 and body_part == 'nose': 
     comm_1=bytes(comm_1,'utf-8')
     ser.write(comm_1)
     comm_2=bytes(comm_2,'utf-8')
     ser.write(comm_2)

When opening my serial port at the beginning of the trial, I define:

ser=serial.Serial('<name of serial port>',write_timeout=0)

By defining write_timeout=0, it should cause the serial port to be in non-blocking mode.

When I tried the above commands in the Python Shell of the Coder view, the above code worked absolutely fine and my two commands were sent. However, when I execute the commands in the script it does not work and I have no idea why!

Any help would be much appreciated.
Vicky

Do you do anything to ser at any other point in your script? It could be that something (even something PsychoPy has added in, if you’re compiling from Builder) is changing the properties of your Serial object before reaching the write commands

Hi Tim,

I do send a previous message 0.8 seconds beforehand using ser.write. However, I do this for my other conditions (e.g. defined in my conditions file termed body_part) and it is not an issue. The only difference that I have here is sending two communications to ser.write in close proximity.

Since posting I have come across a new error:

Fatal Python error: (pygame parachute) Segmentation Fault

Do you know why this occurs? I have had a google on the forum and it looks like it is associated with multiple different reasons (e.g. sound, text or movie stimuli) each time. I am only using the module serial in my script - could it be ‘overloading’ PsychoPy and causing it to crash?

Thanks again for all your help - I’m pretty stuck!
Vicky

Hi @vroot,

Did you ever manage to fix the issue? I’m trying to write to the serial port and it currently only works in the shell. I tried using the basic code on pyserial to start with, but this doesn’t work when I run it from the Editor.

Thank you!

The serial driver has changed a lot since the last time I used it. I had to modify my writes to (e.g.)
ser.write((‘MES\r\n’).encode())
to talk to a chromameter. ‘MES’ calls for a measurement and I needed \r return and \n newline, at least that is my interpretation and maybe is specific to the chromameter. The .encode() I got from a search … it fixes some kind of unicode thing. BTW I am on linux.
One other strange thing (again, I am just reporting what helped me), was that the serial port showed up on /dev/ttyS3 not sure why … spent 30 mins finding that out.