1.关于

这份代码来自: stackoverflow

2. 测试

  • 2.1 测试环境: vmware + ubuntu, vmware添加串口(虚拟机关机后再添加)

  • 2.2 测试输出

3. 源码

#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <linux/serial.h> #include <iostream>
#include <list> using namespace std; static string get_driver(const string& tty) {
struct stat st;
string devicedir = tty; // Append '/device' to the tty-path
devicedir += "/device"; // Stat the devicedir and handle it if it is a symlink
if (lstat(devicedir.c_str(), &st)==0 && S_ISLNK(st.st_mode)) {
char buffer[1024];
memset(buffer, 0, sizeof(buffer)); // Append '/driver' and return basename of the target
devicedir += "/driver"; if (readlink(devicedir.c_str(), buffer, sizeof(buffer)) > 0)
return basename(buffer);
}
return "";
} static void register_comport( list<string>& comList, list<string>& comList8250, const string& dir) {
// Get the driver the device is using
string driver = get_driver(dir); // Skip devices without a driver
if (driver.size() > 0) {
string devfile = string("/dev/") + basename(dir.c_str()); // Put serial8250-devices in a seperate list
if (driver == "serial8250") {
comList8250.push_back(devfile);
} else
comList.push_back(devfile);
}
} static void probe_serial8250_comports(list<string>& comList, list<string> comList8250) {
struct serial_struct serinfo;
list<string>::iterator it = comList8250.begin(); // Iterate over all serial8250-devices
while (it != comList8250.end()) { // Try to open the device
int fd = open((*it).c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY); if (fd >= 0) {
// Get serial_info
if (ioctl(fd, TIOCGSERIAL, &serinfo)==0) {
// If device type is no PORT_UNKNOWN we accept the port
if (serinfo.type != PORT_UNKNOWN)
comList.push_back(*it);
}
close(fd);
}
it ++;
}
} list<string> getComList() {
int n;
struct dirent **namelist;
list<string> comList;
list<string> comList8250;
const char* sysdir = "/sys/class/tty/"; // Scan through /sys/class/tty - it contains all tty-devices in the system
n = scandir(sysdir, &namelist, NULL, NULL);
if (n < 0)
perror("scandir");
else {
while (n--) {
if (strcmp(namelist[n]->d_name,"..") && strcmp(namelist[n]->d_name,".")) { // Construct full absolute file path
string devicedir = sysdir;
devicedir += namelist[n]->d_name; // Register the device
register_comport(comList, comList8250, devicedir);
}
free(namelist[n]);
}
free(namelist);
} // Only non-serial8250 has been added to comList without any further testing
// serial8250-devices must be probe to check for validity
probe_serial8250_comports(comList, comList8250); // Return the lsit of detected comports
return comList;
} int main() {
list<string> l = getComList(); list<string>::iterator it = l.begin();
while (it != l.end()) {
cout << *it << endl;
it++;
} return 0;
}

c++之Linux获取可用串口的更多相关文章

  1. QT 自动获取可用串口

    本来想直接用Settings来获取的,但是串口信息类似 "\Device\Serial0",死活获取不了,用了转义.反斜杠还是获取不到,所以就放弃了,网上好像也没有获取成功的. 所 ...

  2. Qt5获取可用串口

    概述 本文将介绍Qt5使用类QSerialPortInfo获取可以用串口号 效果 机器上配置的虚拟串口 程序获取结果 源码开始 .pro文件中添加下面的代码 QT += serialport 然后,执 ...

  3. 详解linux下的串口通讯开发

    串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用.常用的串口是RS-232-C接口(又称EIA RS-232-C)它是在1970年由美国电子工业协会(EIA)联合贝尔系统.调制解调 ...

  4. 【转载】详解linux下的串口通讯开发

    来源:https://www.cnblogs.com/sunyubo/archive/2010/09/26/2282116.html 串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使 ...

  5. Linux 获取设备树源文件(DTS)里描述的资源

    Linux 获取设备树源文件(DTS)里的资源 韩大卫@吉林师范大学 在linux使用platform_driver_register() 注册 platform_driver 时, 需要在 plat ...

  6. linux下查看串口信息

    rs232串口通信接口:当通信距离较近时(<12m),可以使用电缆线直接连接,若距离较远,需附加调制解调器. 9个脚针的定义: CDC数据载波检测,RXD接收数据,TXD发送数据,DTR数据中断 ...

  7. Linux与Windows串口通信

    串口是常用的计算机与外部串行设备之间的数据传输通道,由于串行通信方便易行,所以应用广泛.现在国际上不断有串口新技术及新规格推出,结合社会各方面需要,串口通信发展的空间庞大.串口通讯技术因其自身的优势和 ...

  8. 获取可用的处理器(CPU)核数【转】

    linux下获取cpu核数,sysconf(_SC_NPROCESSORS_CONF),,, from:红黑联盟,https://www.2cto.com/kf/201210/164480.html ...

  9. 具体解释linux下的串口通讯开发

    串行口是计算机一种经常使用的接口,具有连接线少.通讯简单,得到广泛的使用.经常使用的串口是RS-232-C接口(又称EIA RS-232-C)它是在1970年由美国电子工业协会(EIA)联合贝尔系统. ...

随机推荐

  1. doxygen相关命令

    主要配置修改 整个程序配置分几个部分 Project related configuration options 项目相关,包括: 项目名 输出目录 输出语言 是否显示继承属性 是否对C.Java.F ...

  2. 使用mamba加快conda安装软件速度?

    conda是个安装软件的神器,但镜像不稳定,下载安装软件的速度有时很慢.对于几十Mb甚至上百Mb的软件往往下不动,下了半天可能失败. 找了一个叫mamba的加速神器,可以用来并行下载和安装,大大加快速 ...

  3. 多组学分析及可视化R包

    最近打算开始写一个多组学(包括宏基因组/16S/转录组/蛋白组/代谢组)关联分析的R包,避免重复造轮子,在开始之前随便在网上调研了下目前已有的R包工具,部分罗列如下: 1. mixOmics 应该是在 ...

  4. cpu的性能测试

    #!/bin/bash #user%加上sys%是性能的评判标准 User_sys_a=`sar -u 1 3 |tail -1 |awk '{print $3"+"$5}'|bc ...

  5. Java 读取txt文件生成Word文档

    本文将以Java程序代码为例介绍如何读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: IntelliJ IDEA Free Spire.Doc for Java T ...

  6. 02 Windows安装C语言开发工具CodeBlocks

    CodeBlocks安装 使用微信扫码关注微信公众号,并回复:"C语言环境",免费获取下载链接! 1.卸载CodeBlocks(电脑未装此软件,跳过)    进入目录:C:\Pro ...

  7. 日常Java 2021/10/10

    多态就是同一个行为具有多个不同表现形式的能力 多态就是同一个接口,使用不同的实例而执行不同操作 多态的优点 1.消除类型之间的耦合关系 2.可替换性 3.可扩充性 4.接口性 5.灵活性 6.简化性 ...

  8. 学习java 6.29

    今天是学习Java的第一天. 学习内容:了解了JDK的下载和安装: 学会了如何配置Path环境变量及安装eclipse: 执行了HelloWorld案例: 在Java中关键字需要小写,Java中最基本 ...

  9. 为构建大型复杂系统而生的微服务框架 Erda Infra

    作者|宋瑞国(尘醉) 来源|尔达 Erda 公众号 ​ 导读:Erda Infra 微服务框架是从 Erda 项目演进而来,并且完全开源.Erda 基于 Erda Infra 框架完成了大型复杂项目的 ...

  10. Spark基础:(二)Spark RDD编程

    1.RDD基础 Spark中的RDD就是一个不可变的分布式对象集合.每个RDD都被分为多个分区,这些分区运行在分区的不同节点上. 用户可以通过两种方式创建RDD: (1)读取外部数据集====> ...