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

11 visualizaciones (últimos 30 días)
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

Respuestas (1)

Prateek
Prateek el 22 de Nov. de 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

Categorías

Más información sobre Arduino Hardware en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by