假定串口设备文件为 /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;
}