Contents

Aaron Klapheck

% Transfer information back and forth between the BS2 and MATLAB 26-Aug-08
clear, clc, home
fprintf('The date and time: %s \n \n', datestr(now))
The date and time: 29-Aug-2008 14:06:10 
 

Program Information

% Purpose
% The purpose of this program is to set up direct communication to the
% Basic Stamp. Two way communication is to be achieved.

% Execution
% How this works is that a serial comunication port is opened and data is
% read from it using the fscanf command. This data is then transfered into
% numerical format where mathematical opporations are performed on it.
% Following these opporations this manipulated data is then converted back
% into text and transmitted back to the BS2 using the fprintf command. This
% is repeated several times. Just following the fprintf command is another
% fscanf, this is becuase the fprintf command is echoed and unless the
% fscanf command is used directly after it then the following fscanf will
% simply read the data that was just sent out using the fprintf command.
% This is very confusing and I have no idea why this happons.

% Users
% For those who wish to use this code to send messages of their own the
% following information may prove useful. The parts of this code that would
% change depending on individuals software and hardware setup are
% followed by a marking; marking: **(may need to change)**.

Two-way Communication

port_number = 4; % **(may need to change)**

port = ['COM', num2str(port_number)];

s = serial(port); % Select COM port

fopen(s) % Open COM port for reading

[message, leangth] = fscanf(s); %Recived lone LF from Boe-Bot


% Repeate the while loop below untill information is being sent. When there is
% information being sent its length will always be two or greater. x is
% used because sometimes a stable communication is not established (due to
% an inproper connection, falty coding, etc.) so x will keep the loop from
% repeating infinitly in that case.
while leangth < 2;
    x = 0;
    x = x + 1;
    if x == 30;
        leangth = 30;
    end
    [message, leangth] = fscanf(s); %Recived text from Boe-Bot
end


text1_char = message;
text = cellstr(text1_char)


rows = 20; % The number of communications.

% Receive data, perform calculations, then send modified data. Repeate
% these steps 3 times. It performs the first step perfectly but instead of
% reading the next data line sent by the BS2 it reads its own fprintf data.
% when an fprintf command is made the following fscanf simply echos the
% fprintf. If fprintf is used three times in a row followd by fscanf three
% times in a row then the fscanfs show all three fprintf information. I am
% not sure why that is.
for x = 1:rows;

  char{x,1} = fscanf(s);

  % Select individual characters
  row = char(x,1);

  % Convert characters into numbers
  row_str = cell2mat(row);
  number(x,1) = str2double(row_str);

  BS2_number_sent = number(x,1)

  % Perform mathematical opporation, multiply by three.
  Multiply_in_MATLAB = number(x,1)*3

  result(x,1) = Multiply_in_MATLAB;

  % Convert number into string
  opporation_str = num2str(Multiply_in_MATLAB);

  % Transmit string to the BS2
  fprintf(s, opporation_str);
  echo{x,1} = fscanf(s); % get rid of the command that is echoed.

end

fclose(s)
delete(s)
clear s

disp('End Program')
text = 

    'This is your Boe-Bot'


BS2_number_sent =

     3


Multiply_in_MATLAB =

     9


BS2_number_sent =

     4


Multiply_in_MATLAB =

    12


BS2_number_sent =

     5


Multiply_in_MATLAB =

    15


BS2_number_sent =

     6


Multiply_in_MATLAB =

    18


BS2_number_sent =

     7


Multiply_in_MATLAB =

    21


BS2_number_sent =

     8


Multiply_in_MATLAB =

    24


BS2_number_sent =

     9


Multiply_in_MATLAB =

    27


BS2_number_sent =

    10


Multiply_in_MATLAB =

    30


BS2_number_sent =

    11


Multiply_in_MATLAB =

    33


BS2_number_sent =

    12


Multiply_in_MATLAB =

    36


BS2_number_sent =

    13


Multiply_in_MATLAB =

    39


BS2_number_sent =

    14


Multiply_in_MATLAB =

    42


BS2_number_sent =

    15


Multiply_in_MATLAB =

    45


BS2_number_sent =

    16


Multiply_in_MATLAB =

    48


BS2_number_sent =

    17


Multiply_in_MATLAB =

    51


BS2_number_sent =

    18


Multiply_in_MATLAB =

    54


BS2_number_sent =

    19


Multiply_in_MATLAB =

    57


BS2_number_sent =

    20


Multiply_in_MATLAB =

    60


BS2_number_sent =

    21


Multiply_in_MATLAB =

    63


BS2_number_sent =

    22


Multiply_in_MATLAB =

    66

End Program