直接读写设备文件
2015-08-27 02:58
423 查看
假定串口设备文件为 /dev/ttyAMA0,
那么,有两种读写 ttyAMA0的方法。
1) 在shell 命令行:
e#echo "hi" > /dev/ttyAMA0
hi
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的方法。
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;
}
相关文章推荐
- Linux下的串口编程
- Leetcode: Majority Element
- hdoj4539郑厂长系列故事——排兵布阵【状压dp】
- Centos7安装及使用webbench进行并发测试
- Linux用户和组管理
- C++ Primer : 第十二章 : 动态内存之allocator类
- LeetCode Reverse Nodes in k-Group
- LeetCode Reverse Nodes in k-Group
- Python初学(6)——Python的函数
- 笔试题整理
- 根据user stack 数据分析函数调用栈
- 进程管理
- Leetcode: Intersection of Two Linked Lists
- TCPServer
- 字节型TCP协议解析
- 华为上机题7(四则运算)
- 单例模式在android中的应用
- 扩展Tomcat支持OSGi应用服务
- jmeter 树结构返回乱码解决
- 单例模式在android中的应用