matlab - Reading data only when present -


i'm trying read data com3 port.
i'm using code:

in = fscanf(s);     if(in == 'a')         fclose(s);         break;     end 

the problem when no data sent com3 port, fscanf() wait time interval , give timeout.

is there way read data when present?

read when data present

you can read out bytesavailable-property of serial object s know how many bytes in buffer ready read:

bytes = get(s,'bytesavailable');    % using getter-function bytes = s.bytesavailable;           % using object-oriented-addressing 

then can check value of bytes match criteria. assuming char 1 byte, can check before reading buffer.

if (bytes >= 1)      in = fscanf(s);      % handling of 'in' here end 

minimize time wait

you can manually set timeout-property of serial object s lower value continue execution earlier default timeout.

set(s,'timeout',1);       % sets timeout 1 second (default 10 seconds) 

most following warning:

unsuccessful read: timeout occurred before terminator reached..

it can suppressed executing following command before fscanf.

warning('off','matlab:serial:fscanf:unsuccessfulread'); 

here example:

s = serial('com3');  set(s,'timeout',1);       % sets timeout 1 second (default 10 seconds)  fopen(s);  warning('off','matlab:serial:fscanf:unsuccessfulread'); in = fscanf(s); warning('on','matlab:serial:fscanf:unsuccessfulread');  if(in == 'a')     fclose(s);     break; end 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -