I am trying to connect an Arduino to Matlab and "fopen" or "BytesAvailable" give me errors.

11 vues (au cours des 30 derniers jours)
What needs to be changed in order for this code to run correctly? The fopen error says it needs a valid file, and the "BytesAvailable" error gives: "Unrecognized method, property, or field 'BytesAvailable' for class 'internal.Serialport'."
Matlab Code:
function OpenSerialButtonPushed(app, event)
app.ArduinoUno= serialport(strcat('COM',num2str(app.COMPortDropDown.Value)), 9600);
fopen(app.ArduinoUno);
pause(0.01);
while app.ArduinoUno.BytesAvailable > 0 || counter < 5
inByte = fscanf(app.ArduinoUno, '%c', 1);
counter = counter + 1;
if counter == 5
errordlg('Connect arduino to the computer or type in correct COM port')
set(app.ConnectionStatusLabel,'String', 'Connection Failed');
break
end
if inByte == 'c'
set(app.ConnectionStatusLabel.Text, 'String', 'Connection Successful');
end
end
end

Réponses (1)

Prateek
Prateek le 22 Nov 2022
Hi Jake,
A valid COM port is required by “fopen. Please debug the code to ensure that an available “COM” port is being passed to “fopen”.
Also, in MATLAB R2022a, the “serialport” objects have a property “NumBytesAvailable” to ascertain amount of incoming data available to be read. Thus, changing the “BytesAvailable” to NumBytesAvailable may resolve the error.
Hope this helps. For better assistance, I request you to post the app designer file and complete code.
Regards,
Prateek

Catégories

En savoir plus sur Arduino Hardware dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by