0
votes

I am trying to send temperature sensor data from arduino to matlab using serial communication. The temperature data are displayed on the Arduino serial monitor but I couldn't get the data in Matlab. The following error occurs. I've tried every possible ways to get the data by putting a pause(3) before fscanf but it still doesn't work.

Warning: Unsuccessful read: A timeout occurred before the Terminator was reached. 'serial' unable to read any data. For more information on possible reasons, see Serial Read Warnings. NaN

My matlab code as follows

if ~isempty(instrfind)
    fclose(instrfind);
    delete (instrfind);
end

waitTime = 10;

s = serial('COM4','BAUD',9600);

waitTime = duration(0,waitTime,0);
t = datetime('now') - startTime;

while t < waitTime
    fopen(s);
    pause(3);
    idn = fscanf(s);
    fclose(s);

    serialData = str2double(idn);
    corrData = serialData;
    disp(corrData);
end


clear a;
clear s;
1
The fopen and close within the loop is probably wrong. Usually you open the connection and leave it open, until everything is read. What kind of data is the arduino sending? Text data? - Daniel
I'm not sure what kind of data is arduino sending as I'm absolute beginner in arduino but I'm sending temperature sensor data to matlab. The data is showing in arduino using Serial.print(bme.readTemperature());. - www

1 Answers

0
votes

fscanf will return when the first terminator is found. This is usually carriage return (CR). Since you are using Serial.print(bme.readTemperature()); there is never a CR sent by the Arduino. Try Serial.println(bme.readTemperature());