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

11 ビュー (過去 30 日間)
Jake Nowicki
Jake Nowicki 2022 年 10 月 31 日
回答済み: Prateek 2022 年 11 月 22 日
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 Center および File ExchangeArduino Hardware についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by