Info

This question is closed. Reopen it to edit or answer.

How to pass User input COM port into serial function?

1 view (last 30 days)
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 Comment
Martin Velasquez
Martin Velasquez on 16 Apr 2019
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.

Answers (0)

This question is closed.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!