在Ubuntu上无法通过fcntl设置O_SYNC

如题,看如下测试代码:

 

 

看看man手册的说明:

F_SETFL (long)

              Set the file status flags to the value specified by arg.  File access mode (O_RDONLY, O_WRONLY, O_RDWR) and

              file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored.  On Linux  this  command

              can only change the O_APPEND, O_ASYNC, O_DIRECT, O_NOATIME, and O_NONBLOCK flags.

 

### 如何在 Ubuntu 系统中使用 C 语言进行串口编程 #### 所需头文件和函数 为了实现串口通信,需要引入特定的头文件并调用相应的API来配置端口参数、打开设备节点、读写数据等操作。通常会涉及到`termios.h`这个用于终端控制的标准库[^1]。 #### 配置环境 确保已经安装了编译器和其他必要的开发工具链,可以通过包管理器apt-get来进行安装: ```bash sudo apt update && sudo apt install build-essential ``` 对于具体的串口编程来说,一般不需要额外安装其他软件包,因为POSIX兼容的操作系统(如Linux)自带了处理TTY设备所需的一切资源。 #### 编程步骤概述 创建一个新的C源文件,并按照如下方式编写代码逻辑: - 初始化结构体`struct termios`以设置波特率、字符大小以及其他选项; - 使用`open()`函数打开指定名称的串行端口; - 调整属性通过`tcssetattr()`; - 进行I/O操作——发送或接收字节流; - 关闭连接前恢复默认状态; #### 示例代码展示 下面给出一段简单的例子用来说明上述过程: ```c #include <stdio.h> #include <string.h> /* String function definitions */ #include <unistd.h> /* UNIX standard function defs */ #include <fcntl.h> /* File control definitions */ #include <errno.h> /* Error number definitions */ #include <termios.h> /* POSIX terminal control definitions */ int set_interface_attribs (int fd, int speed, int parity) { struct termios tty; memset (&tty, 0, sizeof tty); if (tcgetattr (fd, &tty) != 0) { perror("error %d from tcgetattr"); return -1; } cfsetospeed (&tty, speed); cfsetispeed (&tty, speed); tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars // disable IGNBRK for mismatched speed tests; otherwise receive break // as \000 chars tty.c_iflag &= ~IGNBRK; // disable break processing tty.c_lflag = 0; // no signaling chars, no echo, // no canonical processing tty.c_oflag = 0; // no remapping, no delays tty.c_cc[VMIN] = 0; // read doesn't block tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, // enable reading tty.c_cflag &= ~(PARENB | PARODD); // shut off parity tty.c_cflag |= parity; tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CRTSCTS; if (tcsetattr (fd, TCSANOW, &tty) != 0) { perror("tcsetattr"); return -1; } return 0; } void set_blocking (int fd, int should_block) { struct termios tty; memset (&tty, 0, sizeof tty); if (tcgetattr (fd, &tty) != 0) { printf("error %d from tggetattr", errno); return; } if(should_block){ tty.c_cc[VMIN]=1; tty.c_cc[VTIME]=0; }else{ tty.c_cc[VMIN]=0; tty.c_cc[VTIME]=1; } if (tcsetattr (fd, TCSANOW, &tty) != 0) printf("error %d setting term attributes", errno); } int main(int argc,char **argv){ char *portname = "/dev/ttyUSB0"; int fd = open(portname, O_RDWR|O_NOCTTY|O_SYNC); if(fd<0){ fprintf(stderr,"Error opening serial port %s\n",portname); exit(-1); } set_interface_attribs (fd, B9600, 0); // 设置为9600bps无校验位 set_blocking (fd, 0); // 设定非阻塞模式 write(fd,"hello",6); // 向串口发送字符串 "hello" unsigned char buf [255]; int n = read(fd,buf,sizeof(buf)); // 尝试从串口中读取最多255个字节的数据 if(n>0){ // 如果成功读到了一些东西... printf("%.*s",n,buf); // ...那么就将其打印出来. } close(fd); // 记得关闭文件描述符! return 0; } ``` 这段程序展示了怎样建立与串行接口之间的联系,调整它的传输速率及其他特性,并尝试向它发送消息以及监听回应的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值