Go Down

Topic: Serial Monitor (programming in Matlab) (Read 3581 times) previous topic - next topic

Tyrion

Oct 05, 2012, 02:51 pm Last Edit: Oct 05, 2012, 02:53 pm by Tyrion Reason: 1
Hey there

I hope some of you guys have some Matlab experience or maybe can still help me.
I want to write an Interface for Matlab that works just like the Serial Monitor in Arduino.
But unfortunately, I do not have much experience with electronics, so I hope my questions do not sound too stupid. ^^'

So it is an Arduino Diecimila Board board I am using and there are six pins on it which I should make visible in Matlab. They are the Analog Input pins.

http://arduino.cc/en/uploads/Main/ArduinoDiecimilaComponents.jpg

So I have a little function, with which I can get some output for one port.
But as I said, I understand some programming but little electronics and not much about ports. So I don't really know, how to open the additional ports and what port corresponds to what pin on the board (which is my biggest question, e.g. to which pin  does "COM3" correspond?).

Does anybody know, what I have to enter, in order to get numbers like in the Serial Monitor for those six analog inputs?

The Matlab code looks like this:

Code: [Select]

function serialMonitor


%run('clean');
clear all;
close all;

s = serial('COM3'); %assigns the object s to serial port

set(s, 'InputBufferSize', 256); %number of bytes in inout buffer
set(s, 'FlowControl', 'hardware');
set(s, 'BaudRate', 9600);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',10);
%clc;

disp(get(s,'Name'));
prop(1)=(get(s,'BaudRate'));
prop(2)=(get(s,'DataBits'));
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));

disp(['Port Setup Done!!',num2str(prop)]);

fopen(s);           %opens the serial port
t=1;
disp('Running');
x=0;
while(t <= 200)  %Runs for 200 cycles - if you cant see the symbol, it is "less than" sign. so while (t less than 200)

  a =fread(s); %reads the data from the serial port and stores it to the matrix a
  a=max(a);  % in this particular example, I'm plotting the maximum value of the 256B input buffer

  x =[x a];  % Merging the value to an array, this is not very computationaly effective, as the array size is dynamic.
                %Consider pre allocation the size of the array to avoid this. But beware, You might loose some important
                 %data at the end!

  plot(x);
  axis auto;
  grid on;

  disp([num2str(t),'th iteration max= ',num2str(a)]);
  hold on;
  t=t+1;
  a=0;  %Clear the buffer
  drawnow;
end

fclose(s); %close the serial port


end

PaulS

Quote
I want to write an Interface for Matlab that works just like the Serial Monitor in Arduino.

The Arduino doesn't know, or care, what is on the other end of the serial port. So, whatever code was writing to the serial port that the Serial Monitor application was reading will continue to write to the serial port that matlab is reading from.


arkadiraf

Hi,
It is pretty easy interfacing with matlab via the serial, i have tinker with it and its great but remember that matlab isn`t that fast so if you send data to fast your matlab won`t keep up with the streaming and you will see a delay in the data.
the serial interface is basically 2 pins, Rx and Tx ( receive and transmit) with which you send data using bits. all you need to set is the bound rate on both sides and the format of the transmitting data. the arduino will send the data via the serial each time you perform serial.print
i add the files i have used.

Go Up