Code covered by the BSD License  

Highlights from
MATLAB Support Package for Parallax BASIC Stamp

image thumbnail

MATLAB Support Package for Parallax BASIC Stamp

by

 

25 Jul 2011 (Updated )

MATLAB library for communicating with a BASIC Stamp 2 and Board of Education (BoE) kit

stamp.DummySerial
% stamp.DummySerial
%   Test serial COM port used in stamp.BasicStamp in test mode. Duplicates
%   the Stamp's echo of serial input, otherwise returns zero for all
%   values.

%   MATLAB Support Package for BASIC Stamp
%   Version 1.0
%   Copyright 2011 The MathWorks, Inc.

classdef DummySerial < handle
    
    properties(Constant,Hidden = true)
        CommandsRemembered = 5
    end
    
    properties(SetAccess=private)
        Commands = cell(stamp.DummySerial.CommandsRemembered,1)
        buffer = uint8(zeros(20,1))
        BytesAvailable = 0;
        Status = 'closed';
    end
    
    properties
        Port = 'Virtual'
    end
    
    methods
        
        function fopen(obj)
            obj.addCommandCall('fopen');
            obj.Status = 'open';
        end
        
        function fclose(obj)
            obj.addCommandCall('fclose');
            obj.Status = 'closed';
        end
            
        function fwrite(obj,msg,varargin) 
            obj.addCommandCall('fwrite');
            
            for i = 1:size(msg)
                for j = 1:size(msg,2)
                    obj.BytesAvailable = obj.BytesAvailable + 1;
                    obj.buffer(obj.BytesAvailable) = msg(i,j);
                end
            end
        end
               
        
        function out = fread(obj,size,varargin)
            obj.addCommandCall('fread');
            
            if ~exist('size','var')
                size = 20;
            end
            
            out = zeros(size,1);    
            
            for i = 1:size
                out(i) = obj.readBuffer;
            end
        end
        
        function out = fscanf(obj,notused,size)
            obj.addCommandCall('fscanf');
            
            if exist('size','var')
                out = obj.fread(size);
            else
                out = [];
                for i = 1:obj.BytesAvailable()
                    temp = obj.readBuffer;
                    if obj.readBuffer
                        break;
                    end
                    out = [out temp]; %#ok<AGROW>
                end
            end
        end
    end
    
    methods(Access = private)
        function addCommandCall(obj,cmd)
            for i = obj.CommandsRemembered-1:-1:1
                obj.Commands{i+1} = obj.Commands{i};
            end
            
            obj.Commands{1} = cmd;
        end
        
        function out = readBuffer(obj)
            if obj.BytesAvailable ~= 0
                out = obj.buffer(1);
                for i = 1:obj.BytesAvailable
                    if i > size(obj.buffer)
                        obj.buffer(i) = 0;
                    else
                        obj.buffer(i) = obj.buffer(i+1);
                    end
                end
                obj.BytesAvailable = obj.BytesAvailable - 1;
            else
                out = 0;
            end
        end
    end
                
end

Contact us