Event markers to BIOPAC via serial port

Hi there,
I am trying to setup a task in Builder (using code snippets) to send event markers in a BIOPAC physiology recording. I am sending bytes via serial port from PsychoPy using pyserial and I’m not quite sure what I’m doing. I attached an image at the bottom that contains the digital acquisition channels that can be recorded in AcqKnowledge/BIOPAC. (here is a similar thread)

I have successfully opened the serial port, which sets one digital acquisition channel (upper pink, D9/D29) from 5 to 0 volts (and that channel remains at 0 until the port is closed)

import serial
ser = serial.Serial('COM4', baudrate = 115200, timeout=1)

Then the following code can set one or two different digital acquisition channels from 5 to 0 volts one time (green, D33/D13; lower pink, D34/D14). (I’m not entirely sure what this line directly below does - it seems to change based on what else is happening. I can provide more detail but I feel like I’m just missing something.)

ser.write("RR".encode())

Then, when the port is closed, the upper pink channel is set back to 5, as described in parentheses above. Also, if the ser.write(“RR”.encode()) was used, the blue channel (D32/D12) is sometimes set to 0 one time upon close.

ser.close()

So, I think I’m starting to understand some of what is happening above but if anyone could provide a better explanation I would be very appreciative. However, the biggest question that I’m having trouble with is how to trigger specific channels. It seems as if whenever I send a byte when the port is open, it only ever triggers the green channel (D33/D13). For example,

ser.write(b'36')
ser.flush()

seems to be the same channel and information as

ser.write(b'10')
ser.flush()

I know that one way to code events in AcqKnowledge would be to send triggers to specific BIOPAC channels, and then those channels could represent unique codes or they could be combined into a calculated channel. Would anyone be able to point me in the direction of how to accomplish that? Or, if there are any other ways that we could use to mark events in an AcqKnowledge recording via serial port, that would also be helpful. Thanks!

Hi There!

I’m afraid I wouldn’t consider myself an expert in biopac or the python serial library enough to answer this question thoroughly - and we might need some more info on the type of BIOPAC system you are using - but I will try to be as helpful as possible!

My first guidance would always be towards to serial documentation, which I think you have probably already explored. For the command ser.write("RR".encode) which you ask about, the encode() part of this is encoding a string to bytes. the write part is to write those bytes to the serial port. I think without this some serial users had issues writing a string to the port without it first being converted to a byte array.

In terms of guidance how to move forward, I notice that there are some related posts on the forum. I also wonder if this blog might be helpful - you could try adding the repository folder to your experiment .psyexp file location and importing it using a code component. The code used in the blog should in theory also work from PsychoPy I believe (though it might depend on what BIOPAC system you are using). You wouldn’t need to do any of this bits relating to creating a window though, as PsychoPy will do that for you. I would be interested to hear if this works so if you do give that approach a try, it would be great if you could report back!)

Sorry I can’t be of more specific help!
Becca

Hi Becca,

Thanks for following up! You are 100% correct about the first part - and I have experienced the error when the string is not converted to a byte array first so that all makes sense.

We use a MP150, but what I’ve learned would also apply to the MP160, as far as I can tell. The other systems might be different but likely not very different.

For the record (and especially for others coming across this thread), in the comments in the blog post linked above, the author clarified that it’s not set up to send event markers (i.e., triggers, or information to specific channels) to BIOPAC. Unfortunately, as far as I can tell, AcqKnowledge (the software to accompany the MP150/MP160) cannot accept traditionally serially encoded bytes. This means that to the best of my knowledge, there must be a piece of hardware in between the serial information leaving PsychoPy and the information recorded from BIOPAC. Blackbox Toolkit manufactures a USB-TTL module (also available on BIOPAC’s website) which has been tested by the BIOPAC team and should work as the in-between. Someone with some amount of electrical engineering knowledge could probably produce a similar piece of hardware, although it might have a greater latency, since the USB-TTL was designed to send very precisely timed triggers. It is possible that a custom cable, or a custom arrangement of available cables, would also be able to replicate the connections (again with questionable latency), but I can’t vouch for cables being a realistic possibility. If anyone is interested in testing out the cables idea I can send more info (the cables would be a little over USD$200, whereas the USB-TTL module would be in the USD$350-460 range).

Will definitely keep you updated, and I’ll make code available once there’s a functional, tested task.