How can I get the available serial com port automatically?

29 ビュー (過去 30 日間)
MD RAQUIBUZZAMAN
MD RAQUIBUZZAMAN 2018 年 9 月 6 日
コメント済み: Walter Roberson 2019 年 5 月 28 日
After using this command instrhwinfo ('serial'), I get the available serial port name. Then I have to manually input it like- s = serial('COM5','BaudRate',9600). But how Can I automatically detect and use it without any manual edit inside the code?

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 9 月 6 日
info = instrhwinfo('serial');
if isempty(info.AvailableSerialPorts)
error('No ports free!');
end
s = serial(info.AvailableSerialPorts{1}, 'BaudRate', 9600);
Unfortunately it would be common that this will not do what you want. Typically you want to find the serial port that a specific kind of device is on, not just any available serial port. Like if you have an older system that has a PS2 mouse connector no longer in use, that might show up as a serial port like COM2, but you would probably be more interested in finding an arduino or serial over USB port.
  2 件のコメント
Jack Nguyen
Jack Nguyen 2019 年 5 月 28 日
so if I would like to use the output answer to input data and graph, what will be the way to do it in my code?
I was trying to write:
info = instrhwinfo('serial');
if isempty(info.AvailableSerialPorts)
error('No ports free!');
end
s = serial(info.AvailableSerialPorts{1}, 'BaudRate', 9600);
open_data = fopen(s)
line = fgetl (open_data)
scan_line = sscanf (line, '%f,%f,%f) (depending on the output)
% it returns as too many argument open_data = fopen(s)
Walter Roberson
Walter Roberson 2019 年 5 月 28 日
fopen(s)
line = fgetl(s);

サインインしてコメントする。


MD RAQUIBUZZAMAN
MD RAQUIBUZZAMAN 2018 年 9 月 6 日
I totally agree with you about it. Thank you for the reply. I will look for the link.

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

タグ

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by