Pages: [1]   Go Down
Author Topic: Serial Monitor (programming in Matlab)  (Read 3341 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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:

function serialMonitor

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);
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));
disp(['Port Setup Done!!',num2str(prop)]);
fopen(s);           %opens the serial port
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!
   axis auto;
   grid on;
   disp([num2str(t),'th iteration max= ',num2str(a)]);
   hold on;
   a=0;  %Clear the buffer
fclose(s); %close the serial port

« Last Edit: October 05, 2012, 07:53:20 am by Tyrion » Logged

Seattle, WA USA
Online Online
Brattain Member
Karma: 640
Posts: 50345
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Offline Offline
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

* arduino_data_aquire.m (0.52 KB - downloaded 138 times.)
* AnalogReadSerial.ino (0.66 KB - downloaded 84 times.)

Pages: [1]   Go Up
Jump to: