Info

此问题已关闭。 请重新打开它进行编辑或回答。

How to pass User input COM port into serial function?

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