Serial port object unable to read data in MATLAB

  arduino, arduino-due, matlab, serial-port, windows

I’ve created a MATLAB app that acquires data from digital signals created by a flex sensor. When I try to run the app, it prompts the warning:

Warning: The specified amount of data was not returned within the
Timeout period for ‘readline’.
‘serialport’ unable to read any data. For more information on possible
reasons, see serialport Read Warnings.

I’ve checked which serial port the hardware is connected to, I’ve checked the configureTerminator, and whether the serial port object was created correctly. Would anyone happen to know what exactly is causing the issue???

Below is the code I have for the MATLAB app that is supposed to take data upon clicking a ‘start’ button and have the option to save the data to a .csv file.

app.SerialPortDropDown.Value is linked to serialportlist("available").

    % Button pushed function: StartButton
    function StartButtonPushed(app, event)
        app.stopButtonPressed = false;

        try 
        app.arduinoObj = serialport(app.SerialPortDropDown.Value, str2double(app.BaudRateDropDown.Value));
        catch 
            warning('Problem with Serial Port');
            delete(app.arduinoObj);
            return;
        end
        
        configureTerminator(app.arduinoObj,"CR/LF");
        app.arduinoObj.DataBits = 8;
        app.arduinoObj.StopBits = 1;
        flush(app.arduinoObj);
        app.arduinoObj.UserData = struct("Data",[],"Count",1);

        t0 = clock;
        app.startTime = datetime;
        i = 1;
        
        while (etime(clock, t0) < app.RecordingDurationsSpinner.Value) && (app.stopButtonPressed == false)
            app.data(i) = str2double(readline(app.arduinoObj));
            app.times(i) = etime(clock, t0);
            
            if not(isnan(app.data(i)))
                app.DegreeofFlexGauge.Value = app.data(i);
                
                percent = 100.*app.times(i)./app.RecordingDurationsSpinner.Value;
                
                if round(percent) == 100
                    percent = 100;
                end
                
                app.ElapsedTimeLabel.Text = ['Elapsed time: ' num2str(percent) '%'];
                i = i+1;
            end
        end
        
        app.SavetoFileButton.Enable = true;

Source: Windows Questions

LEAVE A COMMENT