|
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.
|