oTree Forum >

Write on serial port

#1 by sepidehkhoneiveh

Hello everyone
I want to record some neural activity (eeg)while my participants are playing my task in otree so i need to synchronise my task with my data recording. 
I need to write some numbers in the serial port of the computer that the task is running in. 
I really dont know how to do that. 
Does anyone has the experience or suggestion? 
Bests

#2 by BonnEconLab (edited )

I have not written anything to the serial port yet but recorded signals from the serial port. My coauthors and I did so in order to tie the display of pages in oTree to triggers received from an MRI machine.

We used pySerial (pip install pyserial) for this purpose.

In the oTree code, we included, for instance,

import serial
import time

and then, in the page definition:

class Fixation_run_start(Page):

    @staticmethod
    def before_next_page(player, timeout_happened):
        print("fMRI Mode")
        serial_std = {
            "port": 'COM3',  #'/dev/cu.URT2',  #"/dev/cu.Bluetooth-Incoming-Port",
            "baudrate": 115200,
            "bytesize": 8,
            "parity": "N",
            "stopbits": 1,
            "xonxoff": 0,
            "rtscts": 0,
            "dsrdtr": 0,
            "timeout": 0.00
        }
        ser = serial.Serial(**serial_std)
        ser.close()
        ser.open()
        allser = []
        while ser.is_open:
            res = ''
            res = ser.read(4096)
            if res == b'\xfe\x86':
                allser.append(res)
                if len(allser) == 6:  # At run start, wait for 6th TR before presenting first trial
                    player.start_MRI_trigger_of_run = time.time()
                    ser.close()
                else:
                    print('Trigger No. ' + str(len(allser)))

According to https://pyserial.readthedocs.io/en/latest/shortintro.html, there is also a .write() method.

#3 by sepidehkhoneiveh

Thanks alot for your help i will take a look at this. So i want each page in my task that appears, i write a number on my serial port. You think its gonna work like this? Shouldnt i code anything on the .html file i have?

#4 by BonnEconLab

I should have added: My code assumes that oTree is run locally, that is, on the same PC that runs the browser through which the participant interacts with oTree.

In that case you do not need to include anything in the HTML template.

If you run oTree on a different machine than the browser and the EEG equipment is connected to the computer running the browser, you will probably have to use JavaScript to write data to the serial port (which seems possible, judging by a quick Google search).

If you run oTree on the PC connected to the EEG equipment, then all you need is to include your Python code in vars_for_template (executed before a page is displayed to the participant) or before_next_page (executed after a page has been displayed to the participant) of the respective page.

#5 by sepidehkhoneiveh

Many Thanks for your help. I tried and its working. It was very helpul 
wish you the bests

Write a reply

Set forum username