function VC300K
    %
    comPort = 'COM14';
    VC300Kser = [];
    changeUnit = 0;
    savePath = '--';
    isRecording = 0;
    unitJustChanged = 0;
    txtFileID = [];
    rotTxt = '|/—\';
    rotTxtInd = 1;
    %
    graphVals = nan(500, 1);
    saveVals = [];
    %
    fh = figure('position', [100 100 760 540], ...
        'NumberTitle','Off', ...
        'DoubleBuffer','on', ...  
        'Color', [0.9412 0.9412 0.9412], ...
        'NumberTitle','Off', ...
        'ToolBar', 'none', ...
        'MenuBar', 'none', ...
        'CloseRequestFcn', @cf, ...
        'Name', 'VC 300K Panel');
    % 
    ah1 = axes('Units','pixels',...
        'Position',[70 50 620 300],...
        'XLim', [0 1000], ...
        'XTickLabel', [], ...
        'YTickLabel', [], ...
        'Box','on', ...
        'Xgrid', 'on', ...
        'Ygrid', 'on');
    %
    h1 = plot(graphVals, 'LineWidth', 2);
    set(h1, 'YDataSource', 'graphVals')
    %
    ph = uipanel('Title', 'Temperature', 'FontSize', 10, 'Units', 'Pixel', ...
        'Position', [70 380 300 118]);
    t1h = uicontrol('parent', ph, 'Style', 'text', 'position', [15 28 200 65], ...
        'string', '--.-', 'FontSize', 45, ...
        'HorizontalAlignment', 'left');
    %
    b1h = uicontrol('Style', 'Pushbutton', 'position', [380 440 150 50], ...
        'string', 'Connect to VK300K', 'FontSize', 10, 'Callback', @cnctVK300Cb);
    b2h = uicontrol('Style', 'Pushbutton', 'position', [380 380 150 50], ...
        'string', '°C/°F', 'FontSize', 10, 'Callback', @chngUnitCb);
    b3h = uicontrol('Style', 'Pushbutton', 'position', [540 380 150 50], ...
        'string', 'Record to File', 'FontSize', 10, 'Callback', @pbSelRecCb);
    b4h = uicontrol('Style', 'Pushbutton', 'position', [540 440 150 50], ...
        'string', '<HTML>Transfer Data<br>to workspace', 'FontSize', 10, 'Callback', @pbData2WsCb);
    %
    tmr = timer('ExecutionMode', 'fixedRate', 'Period', 0.4, 'TimerFcn', @tmrCb);    
    %
    function tmrCb(~, ~)
        if changeUnit == 1
            fwrite(VC300Kser, 'C')
            unitJustChanged = 2;
            changeUnit = 0;
            graphVals = nan(500, 1);    
        else 
            fwrite(VC300Kser, 'A')            
        end    
        %
        if isRecording == 1
            set(b3h, 'String', ['<HTML><center>Stop recording<br>' rotTxt(rotTxtInd)])
            if rotTxtInd < 4
                rotTxtInd = rotTxtInd + 1;
            else
                rotTxtInd = 1;
            end
        end  
    end
    %
    function chngUnitCb(~, ~)
        % °C/°F
        changeUnit = 1;
    end
    %
    function cnctVK300Cb(~, ~)
        try
            if isempty(VC300Kser) 
                VC300Kser = serial(comPort, 'BaudRate', 9600, 'DataBits', 8, ...
                    'Parity', 'none', 'StopBits', 1, 'ReadAsyncMode', 'continuous', ...
                    'InputBufferSize', 1024, 'OutputBufferSize', 1024, ...
                    'BytesAvailableFcnMode', 'byte', 'BytesAvailableFcnCount', 8, ...
                    'BytesAvailableFcn', @bav);
                
                fopen(VC300Kser);
                start(tmr)
                set(b1h, 'String', '<HTML><center>Disconnect from<br>VK 300K')    
            else
                graphVals = nan(500, 1);
                saveVals = [];  
                stop(tmr)
                fclose(VC300Kser);
                delete(VC300Kser)
                VC300Kser = [];
                set(b1h, 'String', 'Connect to VK 300K')
            end
        catch
            disp(lasterr)
        end
    end
    %
    function pbSelRecCb(~, ~)
        if ~isRecording
            [filename, pathname] = uiputfile({'*.txt'}, 'select file to save to');
            if ~isequal(filename, 0)
                savePath = [pathname filename];
                txtFileID = fopen(savePath, 'a');
                isRecording = 1;
            end
        else
            isRecording = 0;
            fclose(txtFileID);
            set(b3h, 'String', 'Record to File')
        end
    end
    %
    function pbData2WsCb(~, ~)
        assignin('base', 'temperature', saveVals)
    end
    %
    function bav(obj, ~)
        l = fread(obj, VC300Kser.bytesavailable);
         if length(l) == 8
            if unitJustChanged > 0
                % discard two samples after unit change
                unitJustChanged = unitJustChanged - 1;
            else
                if l(1) == 2 && l(8) == 3
                    bcd1 = double(bitand(uint8(l(4)), uint8(240))/16);
                    if bcd1 == 11 
                        bcd1 = 0;
                    end
                    bcd2 = double(bitand(uint8(l(4)), uint8(15)));
                    if bcd2 == 11
                        bcd2 = 0;
                    end
                    bcd3 = double(bitand(uint8(l(5)), uint8(240))/16);
                    bcd4 = double(bitand(uint8(l(5)), uint8(15)));
                    %
                    t = bcd1 * 100 + bcd2 * 10 + bcd3 + bcd4/10;
                    % sign
                    if bitand(l(3), 2) == 2
                        t = -t;
                    end
                    % unit
                    if bitand(l(2), 128) == 128
                        set(t1h, 'String', num2str(t, '%.1f°C'))
                        ylabel(ah1, 'Temperature [°C]')
                        unit = 1;
                    else
                        set(t1h, 'String', num2str(t, '%.1f°F'))
                        ylabel(ah1, 'Temperature [°F]')
                        unit = 0;
                    end
                    %
                    graphVals(2:500, 1) = graphVals(1:499, 1);
                    graphVals(1, 1) = t;
                    refreshdata(h1, 'caller')
                    %
                    dataOut = [datevec(now) t unit];
                    saveVals = [saveVals; dataOut];
                    %                    
                    if isRecording == 1
                        fprintf(txtFileID, [num2str(dataOut) '\n']);
                    end
                end
            end
        end
    end
    %
    function cf(~, ~)
        if ~isempty(VC300Kser)
            stop(tmr)
            delete(tmr)
            fclose(VC300Kser);
            delete(VC300Kser)
            clear('VC300Kser')
        end   
        fclose('all');
        pause(1)
        delete(fh)       
    end
end