您的位置:首页 > 其它

直接读写设备文件

2015-08-27 02:58 423 查看
假定串口设备文件为  /dev/ttyAMA0,

那么,有两种读写 ttyAMA0的方法。

1)  在shell 命令行:

e#echo "hi" > /dev/ttyAMA0
hi
# read x < /dev/ttyAMA0
hello
#echo $x
hello


2): 通过C代码:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
char byte;
int fd = open("/dev/ttyAMA0", O_RDWR);
write(fd, "X", 1);
ssize_t size = read(fd, &byte, 1);
printf("Read byte %c\n", byte);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ttyAMA0