% readSerial2.m is a script file to read out a serial port

%% Reset
clear all;  % clear workspace
close all;  % closes all open windows
clc;        % clear command window

% reset serial port
instrreset
delete(instrfindall)

%% Constants
callTheBoard =          '0108000304FF0000';
convertTo4ByteMode =    '010F000304180221FF95FE00000000';   %the first four bytes of are block 0, the next are block 1. 
convertTo8ByteMode =    '010F000304180221FF95FF00000000';    %convert back to 8 Byte mode
readSingleBlock =       '010B000304180220020000';   % 02 stands for block number, in this case it is block number 2
writeSingleBlock =      '01130003041802210201020304050607080000'; % 0102030405060708 = 8 bytes of data in big-endian format
setUpAllSensors =       '0113000304180221022D2D2C2C000000000000';  % initializes all sensors for sampling
sampleAllSensors =      '01130003041802210001000F00010100400000'; % samples all sensors
readingBlock0x00 =      '010B000304180220000000';    % poll for status (because of long conversion times, wait until status byte turns to 02)
readingBlock0x09 =      '010B000304180220090000';    % Sensors
readingBlock0x0A =      '010B0003041802200A0000';    % ADC0 sensor
resetMCU =              '0108000304170000';
setHighDataRate =       '010C00030410002101020000';
agcTtoggle =            '0109000304F0000000';   % command usede with high data rate
ampmToggle =            '0109000304F1FF0000';   % command used with high data rate
startMCU =              '0108000304FF0000';


% configure EMV to accept ISO15693 commands
setUpISO15693_1outOf4_fullPower =   '010C00030410002101000000';
setUpISO15693_1outOf4_halfPower =   '010C00030410003101000000';
setUpISO15693_1outOf256_fullPower = '010C00030410003101000000';
setUpISO15693_1outOf256_halfPower = '010C00030410003101010000';
% end configure ISO15693


%% Code

%Set Parameter
s = serial('COM4');
set(s, 'BaudRate', 115200, 'DataBits', 8, 'StopBits',1, 'Terminator', ...
    'CR/LF', 'ByteOrder', 'bigEndian', 'Parity', 'none');

%Communication begin
fopen(s);

% Remove data from input buffer
flushinput(s);

% record file for debugging
s.RecordName = 'myRecord.txt';
record(s);

% start MCU
fprintf(s, startMCU);

% set High Data Rate
fprintf(s, setHighDataRate);

fprintf(s, agcTtoggle);

fprintf(s, ampmToggle);

% convert to 8 byte mode for sampling sensors
fprintf(s, convertTo8ByteMode);

% set up and sample
fprintf(s, setUpAllSensors);
fprintf(s, sampleAllSensors);
fprintf(s, readingBlock0x00);

% delay to sample
pause(0.5);
fprintf(s, readingBlock0x09);


condition = true;
count = 0;
while condition
    received = upper(fscanf(s));
    if strcmp(received, 'REQUEST MODE.');
        count = count+1;
    end
    if count == 3
        condition = false;
    end   
end % while condition

data = fscanf(s);

fclose(s);
delete(s);
%Communication end

% delete serial port for further communication
delete(instrfindall)

% % calculations
% refValue = referece resistor;
% thermValue = thermistor result;
% tempConv = ((((((thermValue * 0.9) / 16384.0) / 2.0) / 0.0000024) * 8738.13) / refValue);
% tempConv = (1.0 / ((((log10(tempConv / 100000.0) / Log(2.718))) / 4250.0) + (1.0 / 298.15))) - 273.15;



% figure()
% plot(1:length(wert), wert);
