Changes

Jump to navigation Jump to search
760 bytes removed ,  15:51, 11 March 2016
no edit summary
Line 339: Line 339:  
%
 
%
 
% to run: (receiving incoming data, check code for own purposes)
 
% to run: (receiving incoming data, check code for own purposes)
% buttonbox('run',handle);
+
% buttonbox('run');
 
%
 
%
 
% or
 
% or
Line 353: Line 353:  
%
 
%
 
% to close the connection:
 
% to close the connection:
% buttonbox('close',handle);
+
% buttonbox('close');  
 
  −
persistent old_hdl  % keep handle to COM object persistent
      +
persistent old_hdl  % keep handle to COM object persistent
 
% set defaults
 
% set defaults
 
bb.Device    = 'COM2';
 
bb.Device    = 'COM2';
Line 362: Line 361:  
bb.DataBits  = 8;
 
bb.DataBits  = 8;
 
bb.StopBits  = 1;
 
bb.StopBits  = 1;
bb.Parity    = 'none';
+
bb.Parity    = 'none';  
 
   
if nargin < 1
 
if nargin < 1
 
   cmd = 'open';
 
   cmd = 'open';
Line 379: Line 377:  
end
 
end
    +
if ~any(strcmp(cmd,{'open','close'}))
 +
  if isempty(old_hdl)
 +
      help serial_buttonbox_common
 +
      error('Buttonbox not yet initialized');
 +
  end
 +
  handle = old_hdl;
 +
end
    
switch cmd
 
switch cmd
 
   case 'marker'
 
   case 'marker'
       if isempty(old_hdl)
+
       fwrite(handle, uint8(marker));%IOPort('Write', handle, uint8(marker), 1); % last argument: blocking
        help serial_buttonbox_common
+
       WaitSecs(0.002);
        error('Buttonbox not yet initialized');
+
       fwrite(handle, uint8(0));%IOPort('Write', handle, uint8(0), 0); % last argument: blocking
      end
  −
      handle = old_hdl;
  −
      ret = IOPort('Write', handle, uint8(marker), 1); % last argument: blocking
  −
       java.lang.Thread.sleep(10);
  −
       IOPort('Write', handle, uint8(0), 0); % last argument: blocking
  −
      if ret < 1
  −
        disp('Marker might not have been written to button box, please verify setup....?');
  −
      end     
   
       return
 
       return
 
   case 'clear'
 
   case 'clear'
       if isempty(old_hdl)
+
       while(handle.BytesAvailable)
         help serial_buttonbox_common
+
         fread(handle, 1);
        error('Buttonbox not yet initialized');
+
       end    
       end
  −
      handle = old_hdl;
  −
      IOPort('purge', handle);
   
       ret = []; % meaningless
 
       ret = []; % meaningless
 
       return
 
       return
Line 409: Line 403:  
       return
 
       return
 
   case 'close'
 
   case 'close'
       if nargin > 0
+
       if nargin > 1
 
         handle = varargin{1};
 
         handle = varargin{1};
 
       else
 
       else
Line 415: Line 409:  
       end
 
       end
 
       fclose(handle);
 
       fclose(handle);
       ret = handle;
+
      delete(handle);
 +
       ret = [];
 
       return
 
       return
 
   case 'run'
 
   case 'run'
 
       % read incoming data
 
       % read incoming data
      if isempty(old_hdl)
  −
        help serial_buttonbox_common
  −
        error('Buttonbox not yet initialized');
  −
      end
  −
      handle = old_hdl;
   
       % code proceeds below ....
 
       % code proceeds below ....
 
   case 'wait_keypress'
 
   case 'wait_keypress'
       % read incoming data
+
       % start polling for characters (indicating start of scan)
      if isempty(old_hdl)
+
      while(1)
        help serial_buttonbox_common
+
         data = [];
        error('Buttonbox not yet initialized');
+
        while handle.BytesAvailable
      end
+
            navailable = handle.BytesAvailable;
      handle = old_hdl;
+
             % read incoming data
      while 1
+
            [newdata, cnt] = fread(handle, navailable);
        % start polling for characters (indicating start of scan)
+
            % concatenate possible new data
         navailable = handle.BytesAvailable;
+
            if cnt
        if navailable
+
              data = [data newdata(:)];
            data = [];
  −
             while navailable
  −
              % read incoming data
  −
              [newdata, cnt] = fread(handle, navailable);
  −
              % concatenate possible new data
  −
              if cnt
  −
                  data = [data newdata(:)];
  −
              end
  −
              % check if any more data left
  −
              navailable = handle.BytesAvailable;
   
             end
 
             end
 +
        end
 +
        if ~isempty(data)
 
             ret = data;
 
             ret = data;
 
             return
 
             return
 
         end
 
         end
       end %while 1
+
       end
 
   otherwise
 
   otherwise
 
       fprintf('Unknown option %s\n',cmd);
 
       fprintf('Unknown option %s\n',cmd);
 
       ret = [];
 
       ret = [];
       return
+
       return          
     
+
end  
     
  −
end
      
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% only gets here when cmd = 'run' %
 
% only gets here when cmd = 'run' %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 
   
% Initialize output figure
 
% Initialize output figure
 
win = list_output(' ',[]);
 
win = list_output(' ',[]);
Line 491: Line 470:  
   end
 
   end
 
   pause(0.01);
 
   pause(0.01);
end %while 1
+
end %while 1  
 +
 
 +
 
    
   function hdl = open_buttonbox(device)
 
   function hdl = open_buttonbox(device)
       % open handle to serial device (mini buttonbox)
+
       % open handle to serial device (mini buttonbox)     WaitSecs(0.002); % just to load mex-file into memory
 
       try
 
       try
 
         hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity);
 
         hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity);
Line 506: Line 487:  
         fopen(hdl);
 
         fopen(hdl);
 
       end
 
       end
       old_hdl = hdl;
+
       old_hdl = hdl;    
     
   
       fprintf('Wait for device buttonbox....\n');
 
       fprintf('Wait for device buttonbox....\n');
 
       tic
 
       tic
Line 515: Line 495:  
         fread(hdl, navailable);
 
         fread(hdl, navailable);
 
       end
 
       end
       pause(0.5);
+
       pause(0.5);    
     
+
   end  
      % while ~IOPort('BytesAvailable', hdl) && toc<10
  −
      % % wait for welcome message device
  −
      % end
  −
      % pause(0.5);
  −
     
  −
      % clear buffer
  −
      %IOPort('flush', hdl);
  −
      IOPort('purge', hdl);
  −
     
  −
   end
      
   function win = list_output(line,win)
 
   function win = list_output(line,win)
Line 532: Line 502:  
       persistent lines
 
       persistent lines
 
       persistent edt
 
       persistent edt
       Maxlines = 40;
+
       Maxlines = 40;    
     
   
       if isempty(win)
 
       if isempty(win)
 
         % initialize listbox output figure
 
         % initialize listbox output figure
Line 540: Line 509:  
         ptr=Maxlines;
 
         ptr=Maxlines;
 
         lines(ptr) = {'Buttonbox output:'};
 
         lines(ptr) = {'Buttonbox output:'};
         idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;
+
         idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;        
       
   
         win = figure();
 
         win = figure();
 
         % initialize figure to hold output text
 
         % initialize figure to hold output text
Line 555: Line 523:  
       set(edt,'String',lines(idxs),'Value',Maxlines);
 
       set(edt,'String',lines(idxs),'Value',Maxlines);
 
       drawnow;
 
       drawnow;
   end
+
   end  
 
+
end
 
end</syntaxhighlight>
 
end</syntaxhighlight>

Navigation menu