%minolta.m - Send/receive data to Minolta CS-100A Colorimeter
%            Lines must be terminated with <CR><LF> characters
%
%            See MinoltaDemo2.m for same function but using
%            setupCS100A.m and readCS100A.m scripts

clear all;

%640x480 windowed display mode
config_display(0);
%setup COM1 for access to Minolta CS-100A Chroma meter
port=1;     %use COM1: port
baud=4800;  %4800 bits/sec transfer speed
parity=2;   %2=even parity
nstop=2;    %2 stop bits (0=1 stop bit)
ndata=7;    %7 data bits
config_serial(port,baud,parity,nstop,ndata);
start_cogent;

disp(' ');
disp('Start Minolta Demo');
disp(' ');
wait(100);

%send initialization sequence to CS-100A
%start with fresh biffer
clearserialbytes(port);
%construct STD Initialization message with <CR><LF> termination
msg=['MDS,00',char(13),char(10)];
%convert text to numeric array
nmsg=abs(msg);
%send message via COM1:
sendserialbytes(port, nmsg);

%retrieve <LF> terminated feedback from CS-100A
line_buffer = []; bytes = []; matches = [];
found = 0;
loop = 1;
ftime=time+2000;  %set 2 sec timeout
while( loop > 0 )
    %drain low-level serial input data buffer
    readserialbytes(port);
    %fetch buffered characters (if any)
    [bytes,times,n]=getserialbytes(port);
    %update line array if any data found
    if( n > 0 )
        %concatenate new chars to old
        line_buffer = [line_buffer; bytes];
        %search for <LF> terminator
        matches = find(bytes == 10);
        if(~isempty(matches))
            found = 1;
            loop = 0;  %exit if <LF> received
        end
    end
    %test for time-out condition
    if( time > ftime )
        loop = 0;  %exit if timed-out
    end
end

disp(['length of line_buffer = ',num2str(length(line_buffer))]);
if( length(line_buffer) > 0 )
    %convert numeric column-vector to string
    myString = char(line_buffer');
    %display line
    disp(['Line: ',myString]);
end

%request data from CS-100A
clearserialbytes(port);
msg = []; nmsg = [];
msg=['MES',char(13),char(10)];
nmsg=abs(msg);
sendserialbytes(port, nmsg);

%retrieve <LF> terminated feedback from CS-100A
line_buffer = []; bytes = []; matches = [];
found = 0;
loop = 1;
ftime=time+4000;  %set 4 sec timeout
while( loop > 0 )
    %drain low-level serial input data buffer
    readserialbytes(port);
    %fetch buffered characters (if any)
    [bytes,times,n]=getserialbytes(port);
    %update line array if any data found
    if( n > 0 )
        %concatenate new chars to old
        line_buffer = [line_buffer; bytes];
        %search for <LF> terminator
        matches = find(bytes == 10);
        if(~isempty(matches))
            found = 1;
            loop = 0;  %exit if <LF> received
        end
    end
    %test for time-out condition
    if( time > ftime )
        loop = 0;  %exit if timed-out
    end
end

disp(['length of line_buffer = ',num2str(length(line_buffer))]);
disp(['FOUND FLAG = ',num2str(found)]);
if( length(line_buffer) > 0 )
    %convert numeric column-vector to string
    myString = [];
    myString = char(line_buffer');
    %display line
    disp(['Line: ',myString]);
end
disp(' ');

stop_cogent;

