MATLAB: Problems with fscanf using serial

scanfserial

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

Best Answer

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