フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to pass User input COM port into serial function?

1 回表示 (過去 30 日間)
Martin Velasquez
Martin Velasquez 2019 年 4 月 16 日
閉鎖済み: Martin Velasquez 2019 年 4 月 16 日
Hello
I'm trying to create a inputdlg() where it ask the user to input the comport, which will then pass it to a serial function so it may communicate with an Arduino.
My problem is that when I run the code, it does gives me an "Invalid PORT specified" error. I type the comport in the inputdlg in paranthesis, e.g 'COM5', that doesn't work. I type just COM5, still doesn't work.
Reason why I do this is because the code is to be shared with different computers so they will hae to input different comports and what not.
Thanks to anyone who read and helps
if(~exist('serialFlag','var'))
prompt = {'Enter COM port number, if windows user use COM6 for example. If MAC user use /dev/tty. Use device manager to find Comport.'};
dlgtitle = 'COM port';
definput = {'COM5'};
answer = inputdlg(prompt,dlgtitle,[1 40],definput);
comPort = answer(1);
disp(comPort)
[gyroConnection.s,serialFlag]=setupSerial(comPort);
end
function [s,flag] = setupSerial2(comPort)
flag=1;
s=serial(comPort);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
a='b';
while (a ~='a')
a=fread(s,1,'uchar');
end
if (a=='a')
disp('serial read');
end
fprintf(s,'%c','a');
mbox = msgbox('Serial Communication Setup.');
uiwait(mbox);
fscanf(s,'%u');
end
  1 件のコメント
Martin Velasquez
Martin Velasquez 2019 年 4 月 16 日
Never mind haha. Turns out it outputs an array.
Since it's a 1x1 array I found out you can use string() function and now it works well.

回答 (0 件)

この質問は閉じられています。

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by