How to run Psychotoolbox function simultaneously with serial port activity?

3 views (last 30 days)
I need to run a function from PTB that displays a visual stimulus while Matlab simultaneously talks to a behavioral system via the serial port. I can run both of them, but only in a way that the PTB function runs in its entirety before the script continues and the serial ports interaction takes place. How can I start the PTB function, but then have the script continue to run through the code while the function runs? I have attempted to use parallel computing using
parfor i =1:2
if i ==1
*Runs PTB function*
else
*Runs the rest of the code*
but the variables ('Positions' and 'Timestamps' below) that I am reading in from the serial port is not a classification that can work with parfor. Below is the main part of code that I'm trying to integrate together.
fwrite(ChoiceBallSystem.SerialPort, char(61)); % Send the programming Op-Code
fwrite(ChoiceBallSystem.SerialPort, ChoiceThreshold, 'uint16'); % Send choice threshold (2 bytes)
fwrite(ChoiceBallSystem.SerialPort, Timeout, 'uint16'); % Send timeout (2 bytes)
fwrite(ChoiceBallSystem.SerialPort, IdleTimer, 'uint16'); % Send Ball idle timer (2 bytes)
fwrite(ChoiceBallSystem.SerialPort, Command, 'uint16'); % Send command direction (2 bytes)
Ack = fread(ChoiceBallSystem.SerialPort,1); % Receive an acknowledgement from the microcontroller
if isempty(Ack)
error('Failed to send parameters to the choice ball');
end
tic
%%Run trial
Graphics(1) %This is the function that needs to run simultaneously to the code below
fwrite(ChoiceBallSystem.SerialPort, char(62)); % Send the trial start Op-Code
TrialFinished = 0; % Logic byte to determine whether a threshold has been crossed or a timeout has occurred
while TrialFinished == 0 % While the ball is still collecting data
if ChoiceBallSystem.SerialPort.BytesAvailable > 0 % If a new message has arrived on the serial port
MessageHeader = fread(ChoiceBallSystem.SerialPort,1); % Read the message header.
if MessageHeader == 5 % If the header = 5 (To distinguish legitimate data transmissions - otherwise buffer is dumped)
nSamples = fread(ChoiceBallSystem.SerialPort,1)+1; % Receive the number of position/time samples to expect in this transmission
Positions = NaN(1,nSamples);
Timestamps = NaN(1,nSamples);
for x = 1:nSamples % For each sample
Positions(x) = str2double(fscanf(ChoiceBallSystem.SerialPort)); % Get the ball position during this sample
end
for x = 1:nSamples % For each sample
Timestamps(x) = str2double(fscanf(ChoiceBallSystem.SerialPort))/1000000; % Get the timestamp during this sample (and convert from us to seconds)
end
if abs(Positions(length(Positions))) > ChoiceThreshold % If a choice threshold was crossed
if Positions(length(Positions)) > 0 % If the last position in the position vector is positive
Choice = 1; % The ball crossed the right threhsold. Choice 1.
else
Choice = 2; % The ball crossed the left threhsold. Choice 2.
end
else
Choice = 3; % The ball didn't cross a threshold. Choice 3.
end
TrialFinished = 1; % Set the logic byte to 0 to exit the loop and return data
else
junk = fread(ChoiceBallSystem.SerialPort, ChoiceBallSystem.SerialPort.BytesAvailable); % Not valid data, dump serial buffer
end
end
end

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!