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
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Arduino Hardware についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!