Thought I'd led other people know how I fixed this. When I told MATLAB to start the serial connection, there was a delay before the Arduino started up and was able to receive data. That delay took longer than I had pauses in for. I put a 3 second pause before I wrote the start command and everything worked great. Thanks again Walter for trying to help with this.
MATLAB/Arduino fprintf not working
3 views (last 30 days)
Show older comments
I am writing a program in Arduino that waits for MATLAB to ask it to send data. Once asked, it sends sensor data, until MATLAB asks it to stop. When I use Arduino's built-in serial emulator, it works fine. When I use my MATLAB script, it breaks down.
I've managed to troubleshoot my code by having Arduino light up different LED at different points in the code, so I know that I got to that line. In Arduino, the section of interest looks like this:
void loop()
{
int x=0; //Initialize serial communication marker
digitalWrite(13, HIGH); //Check if I got to this line before hang
if (Serial.available()>0) //Check if there is serial data available
{
digitalWrite(4, HIGH); //Check if I got to this line before hang
delay(1000); //pause long enough (1 s) so I can see the light
x=Serial.read(); //read data
and then the rest of the code operates. So, the LED attached to pin 13 lights up, meaning its entering the loop, but the LED on pin 4 never lights up, meaning it never receives serial data. Now for the MATLAB side, here's how I'm trying to send a start command:
fclose(instrfindall); %close all serial comms
s=serial('COM4','BaudRate',9600); %start serial port to arduino
fopen(s); %open port
try
start='/A01'; %start command
fprintf(s,start) %print start command to arduino
tline = fgetl(s); %read data from arduino, which waited until start command
count = 0; %initialize count
while ischar(tline) && count<1000
disp(tline) %display arduino data
tline=fgetl(s); %get next line of data
count=count+1;
end
fprintf('%s','.A01'); %stop command
fclose(instrfindall); %close all serial comms
catch
fclose(instrfindall);
end
The code breaks down at tline=fgetl(s) with the warning:
Warning: A timeout occurred before the Terminator was reached.
I'm pretty sure whats happening is fprintf(s,start) isn't working for some reason. That means Arduino never sends data back, meaning fgetl(s) will never read anything, causing the code to hang. When I modify my code slightly to use a text file instead of a serial object it works fine. Has anybody had this problem? I've put a lot of effort into trying to solve it on my own and ran out of ideas, so any help solving this would be greatly appreciated.
0 Comments
Answers (3)
Walter Roberson
on 30 Jul 2011
Dang system ate my answer. Excuse me for being concise.
fprintf(s,'/A01') is defined as fprintf(s,'%s\n','/A01') which transmits your defined terminator after your string. You might not have matched terminators with the other side.
Later you have fprintf('%s','.A01') which is
5 Comments
Walter Roberson
on 30 Jul 2011
You could _try_ pause(1) after the fprintf(); you could also try a brief pause after sending each character, such as
fwrite(s,'/'); pause(0.02); fwrite(s,'A'); pause(0.02)
and so on.
I wonder, though, what the default properties of the serial port are? I wonder if it is defaulting to a buffered mode? I don't think I have a serial port handy to check that out on.
adel moussidene
on 21 Apr 2016
a have a probleme with a port when a simulink a cant use a interface graphique you have a solution a wanna controle my onduleur inyone can help me plz
0 Comments
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!