Serial port reading in parallel with experiment program

1,028 views
Skip to first unread message

Aaron Lee

unread,
Jul 14, 2014, 11:28:07 AM7/14/14
to psychop...@googlegroups.com
I'm currently developing a program for an experiment that requires constant reading from a serial port while the experiment is presenting some stimuli and audio cues in order to track activity from an fMRI. Does anyone have any experience with this. The only way I can think of doing this would be to have 2 separate python script running in parallel using threading or multiprocessing but I'm not sure if this will cause any significant timing issues.

Gaelen Hadlett

unread,
Jul 14, 2014, 4:42:20 PM7/14/14
to psychop...@googlegroups.com
Ideally you would want to separate the system presenting the stimuli from the system receiving data from the fMRI.  Are you trying to do something with the fMRI data in real time?

If not, you are probably best off running a separate python script to simply read the serial port and write the data to file.  If yes—that you are trying to make changes to your stimuli presentation in response to data from the serial port—then you would want to use multiprocessing, gevent, or threading to parallelize your experiment.  They wouldn't be two separate scripts.  You would have one script with at least two functions in them (one function for the serial port work, another for presenting the stimuli).

You could probably get away with using one of the above libraries to kick off a serial port reader/file writer inside whatever is presenting the stimuli.  That way you wouldn't accidentally forget to run the serial port side of things.

Michael MacAskill

unread,
Jul 14, 2014, 6:45:57 PM7/14/14
to psychop...@googlegroups.com
Hi,

Threading or multiprocessing may not be necessary at all. You could just check the serial port once every screen refresh, which might be rapid enough for you. i.e. just incorporate port reading into your standard drawing loop.

I imagine you are just checking the scanner's serial output in the same way one checks for key presses, and the drawing loop frequency is generally fine for that.

Would this work for you?

Regards,

Mike


On 15/07/2014, at 3:28 a.m., Aaron Lee <ach....@gmail.com> wrote:

> I'm currently developing a program for an experiment that requires constant reading from a serial port while the experiment is presenting some stimuli and audio cues in order to track activity from an fMRI. Does anyone have any experience with this. The only way I can think of doing this would be to have 2 separate python script running in parallel using threading or multiprocessing but I'm not sure if this will cause any significant timing issues.
>
>

--
Michael R. MacAskill, PhD 66 Stewart St
Research Director, Christchurch 8011
New Zealand Brain Research Institute NEW ZEALAND

Research Fellow, michael....@nzbri.org
Te Whare Wānanga o Otāgo, Otautahi Ph: +64 3 3786 072
University of Otago, Christchurch http://www.nzbri.org/macaskill



Alistair Walsh

unread,
Jul 14, 2014, 8:38:39 PM7/14/14
to psychop...@googlegroups.com
Hi Aaron,

When you say 'constant reading' what do you mean? Do the two processes need to happen concurrently or is it okay to switch back and forth very quickly (every frame as Michael has suggested)?

I'm facing a similar situation and am using Pyserial in Psychopy to gather data from the serial port. My data is an Arduino measuring GSR and ECG so a stream of two readings at 500hz.  Pyserial is included in the standalone version of Psychopy and I have had some initial success with this approach.
Michael's suggestion to poll the serial port at each screen refresh makes sense and is the approach I'm currently persueing. One thing to bear in mind when doing this is if the required data is there at the serial port when the Psychopy goes to get it. The serial port has a buffer that will hold a small amount of data between calls to it. I'm not sure what amount of data you are trying to get, is it large or just a few readings?

Happy to share if you think this might work in your situation.

Michael MacAskill

unread,
Jul 14, 2014, 8:52:32 PM7/14/14
to psychop...@googlegroups.com
Yes, as Alistair says, there are two quite different ways in one wants to "constantly" monitor a port. I assumed it was the MRI scanner itself that you are monitoring. It will generally just be sending out discrete event signals, which for all intents and purposes can be treated in the same way as checking the keyboard for a response.

But then there is the more demanding situation as described by Alistair, where data is being generated and streamed at a high rate. As he notes, it may still be sufficient to just be checking this stream at say 60 Hz, if it is being buffered sufficiently. If not, a more sophisticated approach may be necessary.

Perhaps you could describe your own situation more fully.

Cheers,

Michael
Message has been deleted

Aaron Lee

unread,
Jul 15, 2014, 5:55:01 PM7/15/14
to psychop...@googlegroups.com
Hi Alistair,

the serial port is not generating a large amount of data. Specifically, it gets some data that comes from the fMRI whenever it's scanning and some inputs from a trigger box in the lab. 

Currently, I have 2 separate files; the experiment, and a serial port script that just outputs all the data that goes through the serial port via a serial port to usb converter.

As for what I meant as "constant reading", the experiment we're running is very time sensitive so we would like to have time stamps for when the data was received in the log files generated to be as accurate as possible.

Do you know if checking once every screen refresh cause any of these timing issues?

Thanks in advance!

Aaron
Reply all
Reply to author
Forward
0 new messages