%baud rate: 28800 or 57600 %com port: function [serCommand] = initializeComMod(ComPortNumber) %This function initializes the serial port properly % for use with the iRobot create commandModule %ComPortNumber is the number of the serial port: ex use 3 for 'COM3' % port number can be checked in % Control Panel/System/Hardware/DeviceManager/Ports % serCommand output is a matlab serial port object % Esposito 6/25/2008 with code from regneier, bishop, et al; modified by % Davidson college aritifial intelligence class 2012 port = strcat('COM',num2str(ComPortNumber) ); out = instrfind('Port', port); % Check to see if THAT serial port is already defined in MATLAB if (~isempty(out)) % It is disp('WARNING: port in use. Closing.') if (~strcmp(get(out(1), 'Status'),'open')) % Is it open? delete(out(1)); % If not, delete else % is open fclose(out(1)); delete(out(1)); end end serCommand = serial(port); % Define serial port set(serCommand, 'BaudRate', 28800); % Default Baud rate of 19200 set(serCommand, 'Tag', 'CommandMod'); % give it a name set(serCommand, 'TimeOut', .1); % want to make the buffer big enough that new message can always fit %(example is 389 characters long but messsage length is variable) % but not so big as to hold 2 messages set(serCommand, 'OutputBufferSize', floor(389*1.5) ); set(serCommand, 'InputBufferSize', floor(389*1.5) ); fopen(serCommand); % Open it; pause(1) % give it a second to start getting data disp('Command Module Initialized')