% Written by Daniel Shima, Vanderbilt Vision Research Center, 9 August 2002.
% Minor update to indicate use of Psychtoolbox Version 2.52 on 13 July 2004.

% In conjunction with the function ViewPointchar2double,
% this ViewPointreaddemo program demonstrates how to read serial data
% from ViewPoint.

% If variable choice is set to 0, no serial functions are called and
% the program will instead load the Matlabdatacapture MAT-file
% which contains previously captured ViewPoint serial data in character form.

% Psychtoolbox Version 2.52 (12 August 2002) or later shoud be used
% so that PsychSerial can read binary and includes the expirationSecs parameter.

clear all;

choice = 0; % Change from 0 to 1 if you have an active ViewPoint serial connection in send and/or receive mode.

if (choice)
	% See ViewPoint write demo for an explanation of PsychSerial('Open') and PsychSerial('Params').
	serialportname = PsychSerial('Open','.Bin','.Bout',57600);
	serialportparams = PsychSerial('Params',serialportname);
	serialportparams.hardwareReset = 1;
	serialportparams.baudRate = 57600;
	serialportparams.dataBits = 8;
	serialportparams.stopBits = 1;
	serialportparams.parity = 'none';
	serialportparams.outCTSEnable = 0;
	PsychSerial('Params',serialportname,serialportparams);
	disp('Serial port opened and parameters set.');
	serialportread = PsychSerial('Read',serialportname,1,0.5); % A timeoutSecs value greater that expirationSecs means that only expirationSecs is used.
	disp('Half a second of new ViewPoint serial data captured.');
else
	load Matlabdatacapture;
	disp('Half a second of old ViewPoint serial data loaded.');
end

ViewPointxydt = ViewPointchar2double(serialportread);
disp('ViewPoint values for xpos, ypos, and dt written to three columns of matrix ViewPointxydt.');
disp('Compare to values in ViewPointdatacapture file which can be viewed in a spreadsheet.');
disp('Also look at original character data in serialportread array.');

if (choice)
	PsychSerial('Close',serialportname)
	disp('Serial port closed.');
end
