O_RDWR 终端未申报

O_RDWR undeclared for terminal

我正在使用 ubuntu 14.0.4LTS,它已更新和升级。

我写了串口通信的代码。

 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <time.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <termios.h>

以上文件已包含在代码中。

当我编译代码时,return出现错误"O_RDWR undeclared" 如果我包含 fcntl.h,那么编译器 return 错误 "incompatible type of argument 1",这意味着打开函数需要 const char* 类型的参数。

struct termios TtyN;
open( TtyN, O_RDWR );

类型转换不合适。什么是正确的解决方案?

open的第一个参数是char*pathname,不是struct termios类型。 不要这样打开它。

open 采用路径字符串 (char *),而不是随机的 structstruct termios 用于 specific terminal control APIs,而不是任意文件操作函数。

也许您想 open 类似 "/dev/tty#" 的内容(其中 #tty 数字替换)?

man 2 open 将显示 open() 所需的头文件。即,

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

接下来我建议看man 3 termios