我用的是usb串口,也可以自己改成自己需要获取的串口
std::vector<std::string> list;
for(int i = 0; i < 256; ++i)
{
port.clear();
port.append("/dev/ttyUSB");
port.append(std::to_string(i));
fd = open(port.c_str(), O_RDWR | O_NOCTTY);
if(fd != -1)
{
list.push_back(port);
}
}
for (std::vector<std::string> ::iterator start = list.begin(); start != list.end(); start++)
{
std::cout << *start << std::endl;
}