I'm trying to create an .m-file in MATLAB that can both send transmit and receive data from an Arduino Uno. The Arduino is set to echo any incoming signal.
When I'm using the mttool(built in tool in matlab) I get the correct values, in eg if I send 1 the received data is 1. But the things is when I try to do this with an m-file I get:
Warning: A timeout occurred before the Terminator was reached.
This is the code I'm using:
% Find a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', '/dev/tty.usbmodemfd131', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('/dev/tty.usbmodemfd131','BaudRate', 9600, 'StopBits', 1);
else
fclose(obj1);
obj1 = obj1(1)
end
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
data1 = query(obj1, '1');
fclose(obj1);
I'm using an macbook btw..
I did mine a bit different and got the same results you did. Try looking at the code I provided from Matlab.
% Communications MatLab <--> Arduino
% Matlab file 1 for use with Arduino file 1
clc;
clear all;
numSec=1;
t=[];
v=[];
s1 = serial('/dev/ttyUSB1'); % define serial port
s1.BaudRate=9600; % define baud rate
set(s1, 'terminator', 'LF'); % define the terminator for println
fopen(s1);
try % use try catch to ensure fclose
% signal the arduino to start collection
w=fscanf(s1,'%s'); % must define the input % d or %s, etc.
if (w=='A')
display(['Collecting data']);
fprintf(s1,'%s\n','A'); % establishContact just wants
% something in the buffer
end
i=0;
t0=tic;
while (toc(t0)<=numSec)
i=i+1;
t(i)=toc(t0);
t(i)=t(i)-t(1);
v(i)=fscanf(s1,'%d'); % must define the input % d or %s, etc.
end
fclose(s1);
plot(t,v,'*r') % a very interesting graph
% if you need precise timing
% you better get it from the
% arduino (see file 2)
catch me
fclose(s1); % always, always want to close s1
end
imagiro1:
I did mine a bit different and got the same results you did. Try looking at the code I provided from Matlab.
% Communications MatLab <--> Arduino
% Matlab file 1 for use with Arduino file 1
clc;
clear all;
numSec=1;
t=[];
v=[];
s1 = serial('/dev/ttyUSB1'); % define serial port
s1.BaudRate=9600; % define baud rate
set(s1, 'terminator', 'LF'); % define the terminator for println
fopen(s1);
try % use try catch to ensure fclose
% signal the arduino to start collection
w=fscanf(s1,'%s'); % must define the input % d or %s, etc.
if (w=='A')
display(['Collecting data']);
fprintf(s1,'%s\n','A'); % establishContact just wants
% something in the buffer
end
i=0;
t0=tic;
while (toc(t0)<=numSec)
i=i+1;
t(i)=toc(t0);
t(i)=t(i)-t(1);
v(i)=fscanf(s1,'%d'); % must define the input % d or %s, etc.
end
fclose(s1);
plot(t,v,'*r') % a very interesting graph
% if you need precise timing
% you better get it from the
% arduino (see file 2)
catch me
fclose(s1); % always, always want to close s1
end
Link: http://www.mathworks.com/matlabcentral/fileexchange/26711-interacting-with-an-arduino-using-the-matlab-serial-commands/content/arduino2matlab/serialTest1.m
I think we're missing defining the terminator. Hope that helps.
Oh, good. Now we know that it can be done. Just not how...
Sorry Paul.
Here is how I solved it.
Matlab
% Communications MatLab <--> Arduino
% Matlab file 1 for use with Arduino file 1
clc;
clear all;
s1 = serial('/dev/tty.usbmodemfa141'); % define serial port
s1.BaudRate=9600; % define baud rate
set(s1, 'terminator', 'LF'); % define the terminator for println
fopen(s1);
% signal the arduino to start collection
w=fscanf(s1,'%s'); % must define the input % d or %s, etc.
if (w=='A')
display(['Connection established']);
end
fprintf(s1,'%s\n','1')
fscanf(s1)%
fclose(s1);
Arduino
/*
Communications Matlab <--> Arduino
Arduino file 1 for use with Matlab file 1
L. Toms
establishContact() routine by:
by Tom Igoe and Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/SerialCallResponse
other ideas from
robot grrrl
http://robotgrrl.com/blog/2010/01/15/arduino-to-matlab-read-in-sensor-data/
*/
int ledPin=13;
int i = 0;
unsigned char input_PIN = 0;
void setup() {
pinMode(input_PIN,INPUT);
// start serial port at 9600 bps:
Serial.begin(9600);
//digitalWrite(ledPin,HIGH);
establishContact(); // send a byte to establish contact until receiver responds
//digitalWrite(ledPin,LOW);
}
void loop()
{
if(Serial.read() == '1')
{
unsigned int val = analogRead(input_PIN);
Serial.println(val);
delay(500);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println('A'); // send a capital A
delay(300);
}
Serial.flush();
}