MATLAB/Arduino fprintf not working

12 views (last 30 days)
Adam
Adam on 29 Jul 2011
Answered: adel moussidene on 21 Apr 2016
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.

Answers (3)

Adam
Adam on 15 Aug 2011
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.

Walter Roberson
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
Adam
Adam on 30 Jul 2011
I had tried pauses already, unfortunately they didn't help. The default properties on the serial port are:
Bps:9600
Data bits:8
Parity:None
Stop bits:1
Flow Control:None
Use FIFO Buffer
Receive Buffer:14
Transmit Buffer:16

Sign in to comment.


adel moussidene
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

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!