Thread Subject: Serial input using GUI

Subject: Serial input using GUI

From: Camron Call

Date: 10 Nov, 2009 00:19:03

Message: 1 of 3

It seems that the first time I run my GUI after starting Matlab, I can't get the fscanf to read from the serial object. The timeout happens. But if I simply run the script again and send my data again, the data gets received just fine. It works from then on. Even if I close my GUI and restart it.

My initialization script is:
%----------------------------------------------------
if instrfind ~= NaN; %#ok<FNAN>
  fclose(instrfind);delete(instrfind);
end

s = serial('com1');
s.Terminator = 33;
s.InputBufferSize = 10000;
timeout = 10;
s.Timeout = timeout;
fopen(s);

header = fscanf(s);

header(header==10)=''; % remove the line feeds
nl = find(header==13); % find the carriage returns

if isempty(nl); %if no carriage returns then no more data to be read
    fclose(s); delete(s); clear s;
    
else
% bunch of stuff here to assign received data to workspace.
end
%---------------------------------------------------

I would like to know if anyone can tell me why it runs fine except for the first time I run it after starting Matlab. Also, since this is the first time I have used serial objects, any pointers would be helpful.

Thanks,

Camron

Subject: Serial input using GUI

From: jrenfree

Date: 10 Nov, 2009 00:36:42

Message: 2 of 3

On Nov 9, 4:19 pm, "Camron Call" <camronc...@gmail.cam> wrote:
> It seems that the first time I run my GUI after starting Matlab, I can't get the fscanf to read from the serial object.  The timeout happens.  But if I simply run the script again and send my data again, the data gets received just fine.  It works from then on.  Even if I close my GUI and restart it.
>
> My initialization script is:
> %----------------------------------------------------
> if instrfind ~= NaN;  %#ok<FNAN>
>   fclose(instrfind);delete(instrfind);
> end
>
> s = serial('com1');
> s.Terminator = 33;
> s.InputBufferSize = 10000;
> timeout = 10;
> s.Timeout = timeout;
> fopen(s);
>
> header = fscanf(s);
>
> header(header==10)=''; % remove the line feeds
> nl = find(header==13); % find the carriage returns
>
> if isempty(nl); %if no carriage returns then no more data to be read
>     fclose(s); delete(s); clear s;
>
> else
> % bunch of stuff here to assign received data to workspace.
> end
> %---------------------------------------------------
>
> I would like to know if anyone can tell me why it runs fine except for the first time I run it after starting Matlab.  Also, since this is the first time I have used serial objects, any pointers would be helpful.
>
> Thanks,
>
> Camron

Not sure why it would be doing that.

Does the device you're communicating with have a sleep mode? Perhaps
it's asleep, and the first time you make a call to it, it simply wakes
the device up but doesn't actually sent any recognized commands. Then
once the device is awake, commands are recongnized.

Just a wild guess, but I have a device that does something like that.

Subject: Serial input using GUI

From: Camron Call

Date: 10 Nov, 2009 08:41:01

Message: 3 of 3

The code I included above is inside the callback of a pushbutton in my GUI. Basically it's the download button. Once it's pushed I must actively start the download on the device. Data only flows when I start the upload. I am pretty sure that it's not a problem with the device since it's not really two way communication. All the device does is send. It doesn't even know if something is connected to it.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
serial crc 9 Nov, 2009 19:24:03
fscanf crc 9 Nov, 2009 19:24:03
timeout crc 9 Nov, 2009 19:24:03
rssFeed for this Thread

Contact us at files@mathworks.com