Problems with fscanf using serial

1 vue (au cours des 30 derniers jours)
Klaus Schürch
Klaus Schürch le 15 Fév 2016
Commenté : Walter Roberson le 17 Fév 2016
Dear Community
I have a microcontroller which is connected to my PC via a serial port. Thereby I have some problems by reading the incoming data. Using HTerm it is possible to get the correct data in Ascii format from the microcontroller (see figure below). Only the last line ([006C221C249C24C42A]), which contaisn the sensor data, is of interrest.
Here you can see my code:
%Set Parameter
s = serial('COM4');
set(s, 'BaudRate', 115200, 'DataBits', 8, 'StopBits',1, 'Terminator', ...
'CR/LF', 'ByteOrder', 'bigEndian', 'Parity', 'none');
%Communication begin
fopen(s);
% record file for debugging
s.RecordName = 'myRecord.txt';
record(s);
%%set up MCU
% start MCU
fprintf(s, startMCU);
% set High Data Rate
fprintf(s, setHighDataRate);
fprintf(s, agcTtoggle);
fprintf(s, ampmToggle);
% convert to 8 byte mode for sampling sensors
fprintf(s, convertTo8ByteMode);
%%end set up MCU
% set up and sample
fprintf(s, setUpAllSensors);
fprintf(s, sampleAllSensors);
fprintf(s, readingBlock0x00);
% delay to sample
pause(0.5);
fprintf(s, readingBlock0x09);
condition = true;
count = 0;
while condition
received = upper(fscanf(s));
if strcmp(received, 'REQUEST MODE.');
count = count+1;
end
if count == 3
condition = false;
end
end % while condition
data = fscanf(s);
fclose(s);
delete(s);
%Communication end
% delete serial port for further communication
delete(instrfindall)
Matlab gives out a warning after a couple of seconds: "Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.. "
Then when I break the communication manually it displays the error: "Error using serial/fscanf (line 154)
Unsuccessful read: Error using serial/fscanf (line 154)"
Has somebody an idea how I can fix that? Thank for your help
Best regards
Klaus

Réponse acceptée

Klaus Schürch
Klaus Schürch le 17 Fév 2016
It works now. I did not know that the function "fscanf()" adds a terminator to my transmissions. Now I am using "fwrite()" which does not send an additional terminator.
  1 commentaire
Walter Roberson
Walter Roberson le 17 Fév 2016
fscanf() does not add a terminator, fscanf() by default reads up to and including the terminator that is already there in the buffer.
fprintf() does add a terminator to output if you do not specify a format string, but leaves out the terminator if you specify a format string.
fprintf(s, 'hello')
would be the same as
fprintf(s, '%s\n', 'hello')
which would add the line terminator. But
fprintf(s, '%s', 'hello')
would not add the line terminator.
fwrite(s, 'hello')
is not going to add the line terminator.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 15 Fév 2016
fscanf() is probably including the CR/LF as part of the returned text. You then compare for exactly 'REQUEST MODE.' not for the later being a leading prefix of the line.
I suggest you switch to fgetl(s) from fscanf(s) as fgetl() is defined to remove line terminators.
  1 commentaire
Klaus Schürch
Klaus Schürch le 16 Fév 2016
Thank's for your help. I can read the first three lines with fgetl(s), up to " ISO15693: [5D0E000000A207E0,5B]TRF7970A EVM \r\n". When I send the next commant to the microcontroller MATLAB displays: "Warning: Unexpected Warning: A timeout occurred before the Terminator was reached." Thereby the received variable is empty, which I suggest comes from line 4 (\r\n\r\n). Then I do not get any further messages. Is it possible that the timeout is too small? My controller sends a message every 0.5 seconds.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by