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.
Problems with fscanf using serial
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Klaus Schürch
il 15 Feb 2016
Commentato: Walter Roberson
il 17 Feb 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
0 Commenti
Risposta accettata
Klaus Schürch
il 17 Feb 2016
1 Commento
Walter Roberson
il 17 Feb 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.
Più risposte (1)
Walter Roberson
il 15 Feb 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.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!