sccn / lsl_archived

Multi-modal time-synched data transmission over local network

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pull_chunk starts and KEEPS returning empty after a short delay in stream AND stream is back

mayucel opened this issue · comments

Hi all,

I have a question related to lsl pull_chunk function.
We have a closed loop setup that we communicate three pcs using lsl. Everything works except: The processing pc sends processed data to lsl and we stream this data from the display laptop via again lsl. It works fine until, when there is a slight delay in sending from the processing pc and so receiving it by the display laptop. (In that case, the code assigns the previous data from the stream buffer for display) But for later time points, when the stream becomes active again, the pull_chunk still turns empty. (We know there is data in the stream as we checked the stream on display laptop via vis_stream function on a different matlab session.) I am not sure if there could be any issue with using serial port or psychtoolbox at the same time, so I put the code below for your review. Btw the stream is sent every 200 ms. Thanks a lot in advance for your input and time!!

clear all;
tic;
 
%% Open serial port and set the port to 0
flag_trigger = 1;
 
if flag_trigger
    SerialPortObj=serial('COM4', 'TimeOut', 1); %
    SerialPortObj.BytesAvailableFcnMode='byte';
    SerialPortObj.BytesAvailableFcnCount=1;
    SerialPortObj.BytesAvailableFcn=@ReadCallback;
    fopen(SerialPortObj);
    fwrite(SerialPortObj, 0,'sync');
end
 
 
 
%% ask subject ID
SubjID = input('Please provide subject ID:...');
 
 
 
%% LSL
%load LSL lib and settings
pathBehavioralLSL = 'x';
pathImages = 'x';
lsl.LslDir = 'x';
addpath(genpath(lsl.LslDir))
lsl.LSLlib = lsl_loadlib();
lsl.StreamName = 'Feedback';
lsl.bufferrange = 1; % sec
% Check whether names of streams on the lab network are identical to those in the config file
snames = unique(cellfun(@(s)s.name(), lsl_resolve_all(lsl.LSLlib,0.3) ,'UniformOutput',false));
if isempty(snames)
    error('There is no stream visible on the network.');
else
    if isempty(find(strcmp(snames,lsl.StreamName),1))
        warning(['No ' lsl.StreamName ' stream found.']);
    end
end
% resolve stream...
disp(['Resolving stream ' lsl.StreamName '...']);
resS = {};
while isempty(resS)
    resS = lsl_resolve_byprop(lsl.LSLlib,'name',lsl.StreamName); end
% create and save new inlet
disp(['Opening inlet for stream ' lsl.StreamName '...']);
lsl.inletS = lsl_inlet(resS{1});
% get the stream info objects
lsl.Streaminfo = lsl.inletS.info();
 
opt.lsl = lsl;
feedbackX = [0 0 0 0 1];
 
 

%% load
cond_name = {'Action left', 'Action right','Imagery left', 'Imagery right','Catch Action left', 'Catch Action right','Catch Imagery left', 'Catch Imagery right','Control','Baseline Recovery','Red Button','End of Run'};
load x
 
 
%% Psychtoolbox setup
PsychDefaultSetup(2);
PsychWatchDog; % watch for Control C to escape
% Select the display screen
screenid = 1;% max(Screen('Screens')); %1
 
% Open a window on display screen 'screenid'.
[win, wRect] = PsychImaging('Openwindow', screenid, [0 0 0]);
 
% Set the text font:
Screen('TextSize', win, 30);
Screen('TextColor', win, [1 1 1]);
 
 
 
%% Start Experiment
% Initial Baseline put fixation dot at the center of the screen
[xCenter, yCenter] = RectCenter(wRect);
rect_fix=[0 0 10 10]; % fixation point
fixation_dot = OffsetRect(rect_fix, xCenter, yCenter);
Screen('FillRect', win, [1, 1, 1], fixation_dot);
Screen('Flip',win);
WaitSecs(2);
 
 
cond_lst = cond_lst_all(1:2:end);
stim_duration = 15;
rest_duration = 9;
 
for i = 1:size(cond_lst,2)
    
    %% set port to "1" in the beginning of each trial
    if flag_trigger
        fwrite(SerialPortObj, 1,'sync');
        cond_lst_all = [cond_lst_all cond_lst(i)];
    end
    
    foo = GetSecs;
    onset = foo + stim_duration;
    
    %% start feedback bar after cue
    while (onset-foo) >= 0
        
        pause(0.1);
        %% pull from stream
        [buf, timestamps] = lsl.inletS.pull_chunk();
        if ~isempty(buf)
            feedbackX = buf(:,end);
        end
        
        [nx,ny,textbounds] = DrawFormattedText(win, cond_name{cond_lst(i)},xCenter,yCenter); % current condition
        [nx,ny,textbounds] = DrawFormattedText(win, cond_name{feedbackX(5)},xCenter,yCenter+100); % streamed condition
        
        
        %% put everything on screen now (real conditions)
        Screen('Flip',win);
        
        foo = GetSecs;
    end
    

    % set port to "0" after real conditions
    if flag_trigger
        fwrite(SerialPortObj, 0,'sync');
    end
    
    %% rest
    
    Screen('FillRect', win, [1, 1, 1], fixation_dot);
    Screen('Flip',win);
    pause(rest_duration)
end
 
 
 
%% close everything
% return
sca;
toc;
 
% close port
if flag_trigger
    fclose(SerialPortObj);
    clear SerialPortObj;
end

I am going to close this because this repository is an archive and not under development. If you want to post an issue, please post it at sccn/labstreaminglayer.

There is nothing wrong with pull chunk. It looks like something is odd about your while loop, however. I don't know what GetSecs is, but this could be the reason. It could also be that you need to supply the inlet with a max_chunklen. I don't know how you are sending the data, but on the inlet side there is no control over chunk size and this could also be the culprit.