function bbwaitforready(robot);
% bbwaitforready(robot) - Waits until the robot is ready
%		robot 	- robot control structure

try
    
portflush(robot);
portwriteline(robot,'R:');
t=robot.timeout;
robot=portsettimeout(robot,200); % original value is 60, units???
s=portreadline(robot);
robot=portsettimeout(robot,t);
if isequal(s,['R!',13,10])
   return
end
if isequal(s,['FAIL!',13,10])
   bbisready(robot);
   error('bbwaitforready: command ''R:'' returned ''FAIL!''');
end
error(['bbwaitforready: unknown answer: ',s]);

catch
    disp('bbwaitforready failed, using bbisready in while-loop');
    while ~bbisready(robot), pause(1/10); end
end