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

11 次查看(过去 30 天)
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

回答(1 个)

Prateek
Prateek 2022-11-22
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

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by