linux查看串口端口,规范模式Linux串行端口
小编典典
是否将0xD0xA(CRLF)字节放在传输线的开头以告知read()函数数据已准备好被读取?
在 “串行端口” 或 “硬件” 没有一个概念 “开始” 或 “结束” 的的 “传输线” 。只是到U [S] ART的有效载荷数据。
仅当在标准模式下使用termios读取串行终端缓冲区时,线路终端才具有上下文。
请参阅Linux串行驱动程序,以了解如何从硬件中删除用户空间代码。
Linux使用换行符或具有ASCII代码0x0A的换行符作为行终止符,如 手册 页(已引述)中明确指出的那样。
Termios允许定义其他行尾字符,即串行终端的VEOL和VEOL2。
每次出现行定界符都会并且将导致(待定)规范 read() 返回。
行定界符将是缓冲区中返回的最后一个字符,除非用户缓冲区太小而无法包含整行。
为EOF定义的字符,即 VEOF ,默认为EOT的ASCII代码0x04,由termios处理略有不同。
EOF字符的接收导致(待定的)规范 read() 像行定界符一样返回,但是EOF字符未存储在返回的缓冲区中。
因此,当EOF前面有行定界符时, read() 的返回码为零,即实际的空行!
如果您是一位怀疑的托马斯,那么您应该将一对USB-RS232适配器交叉连接在一起,并测试使用termios从串行终端读取数据时会发生什么。在第一个串行终端上
使用终端仿真器程序(例如 minicom) 输入数据,并使用以下C程序查看另一个串行终端上的规范读取。
#define SERIALTERMINAL "/dev/ttyUSB1"
#include
#include
#include
#include
#include
#include
#include
int set_interface_attribs(int fd, int speed)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag &= ~PARENB; /* no parity bit */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
tty.c_lflag |= ICANON | ISIG; /* canonical input */
tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN);
tty.c_iflag &= ~IGNCR; /* preserve carriage return */
tty.c_iflag &= ~INPCK;
tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL);
tty.c_iflag &= ~(IXON | IXOFF | IXANY); /* no SW flowcontrol */
tty.c_oflag &= ~OPOST;
tty.c_cc[VEOL] = 0;
tty.c_cc[VEOL2] = 0;
tty.c_cc[VEOF] = 0x04;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
int main()
{
char *portname = SERIALTERMINAL;
int fd;
int wlen;
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}
/*baudrate 115200, 8 bits, no parity, 1 stop bit */
set_interface_attribs(fd, B115200);
/* simple output */
wlen = write(fd, "Hello!\n", 7);
if (wlen != 7) {
printf("Error from write: %d, %d\n", wlen, errno);
}
tcdrain(fd); /* delay for output */
/* simple canonical input */
do {
unsigned char buf[81];
unsigned char *p;
int rdlen;
rdlen = read(fd, buf, sizeof(buf) - 1);
if (rdlen > 0) {
buf[rdlen] = 0;
printf("Read %d:", rdlen);
/* first display as hex numbers then ASCII */
for (p = buf; rdlen-- > 0; p++) {
printf(" 0x%x", *p);
if (*p < ' ')
*p = '.'; /* replace any control chars */
}
printf("\n \"%s\"\n\n", buf);
} else if (rdlen < 0) {
printf("Error from read: %d: %s\n", rdlen, strerror(errno));
} else { /* rdlen == 0 */
printf("Nothing read. EOF?\n");
}
/* repeat read */
} while (1);
}
请注意,该程序不会去除’\ r’字符(即,属性IGNCR被清除),但是回车符也未定义为行定界符。
因此,在此termios配置中,回车符没有特殊含义,并且像任何可打印字符一样通过。
因此,键入(等效于)ABCDEFG^M^J的内容为:
Read 9: 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0xd 0xa
"ABCDEFG.."
123^Mabc^J 读取为:
Read 8: 0x31 0x32 0x33 0xd 0x61 0x62 0x63 0xa
"123.abc."
可选的termios配置可以去除回车符或将回车符视为行定界符。
2020-06-02
linux查看串口端口,规范模式Linux串行端口相关推荐
- linux查看80端口连接ip,Linux通过netstat命令查看80端口连接数的方法
服务器上的一些统计数据: 1)统计80端口连接数 netstat -nat|grep -i "80"|wc -l 2)统计httpd协议连接数 ps -ef|grep httpd| ...
- linux查看某端口进程占用,Linux下查看某端口占用进程
文章分类:操作系统 在Linux操作系统中 查看占用某一端口的进程是什么:#lsof -i:端口号 或者 netstat -apn | grep redis [root@192_168_56_28 ~ ...
- linux查看双网卡绑定模式,Linux中双网卡绑定实现的各种模式
一.Linux中双网卡绑定实现的原理 Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡;linux设置bond网卡绑定---有些用. Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡, ...
- linux查看7799端口使用情况,Linux /proc/net/ 下文件用途
转载: http://jlspyaozhongkai.blog.163.com/blog/static/116057464200962724623344/ 系统版本 cat /proc/version ...
- linux查询80端口,Linux 查看 80 端口的占用情况
lsof -i:端口号 eg: lsof -i:80 lsof -i:21 [root@localhost ~]# lsof -i: COMMAND PID USER FD TYPE DEVICE S ...
- Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
文章目录: 1 linux查看那端口监听情况 2 Linux查看某个端口对应的进程号和程序 1 linux查看那端口监听情况 linux中可以使用netstat命令查看端口监听情况,首先来看一下该命令 ...
- linux查看主机端口进程命令
linux查看主机端口进程命令 1.查看主机信息 #more /etc/hosts #Do not remove the following line, or various programs #th ...
- linux查看3306端口号是否打开
linux查看3306端口号是否打开 在Linux使用过程中,需要了解当前系统开放了哪些端口,并且要查看开放这些端口的具体进程和用户,可以通过netstat命令进行简单查询. netstat命令各个参 ...
- Linux 查看占用端口进程的绝对路径
Linux 查看占用端口进程的绝对路径 目 录 *Linux 查看占用端口进程的绝对路径* @[toc](目 录) 1. 本机环境 2. 查询占用的端口号 3. 查看进程文件夹 4. 进程结束时, 该 ...
最新文章
- spark变量使用broadcast、accumulator
- ros创建功能包和编译过程问题处理
- java数数字及while和do while 的使用,以及程序的调试与验证
- XE Delphi 判断字符为中文的方法
- (算法)宝石升级问题
- srt编辑的文件在linux显示乱码,解决看本地视频srt字幕乱码问题教程-srt文件
- java 中free,javac(freejava)
- android录音相关
- python访问oracle_用Python操作Oracle
- Codeforces 86C Genetic engineering (AC自己主动机+dp)
- 【论文笔记】A Convolutional Neural Network Cascade for Face Detection
- foremost windows_windows上安装foremost - kalibb
- 中国工业企业数据库stata处理
- linux find查找时间段文件,查找某个时间段的文件和find命令
- 亚马逊云计算机配置,亚马逊云教程6:创建、启动AMI,设置Cloud Watch
- 在Web网页里面实现像Excel/WPS条件格式那样根据数据大小自动改变背景颜色(网页数据热力图实现方法)
- 数据库开发技术的课程记录
- 云上架构和传统IT架构有什么区别及优势?
- 电脑alt+tap切换屏幕卡顿解决
- 贝叶斯(朴素贝叶斯,正太贝叶斯)及OpenCV源码分析