注意事项:friendlycore系统下 UART3 对应的设备文件名是 /dev/ttyAMA3
实验目的:实现串口回环测试,即:自己给自己发数据,然后自己接收到自己发送的内容。
实验内容:
1. 使用杜邦线短接管脚11和12, 对应的是UART3_TX和UART3_RX。
然后呢,我们连接NanoPC T2,写一个程序循环读取串口数据,并打印到屏幕上。
read.c代码如下:
#include
#include
#include
#include
int main()
{
int fd = -1;
fd = open("/dev/ttyAMA3", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("Open Serial Port Error!\n");
return -1;
}
struct termios options;
tcgetattr(fd, &options);
//115200, 8N1
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_cc[VTIME]=0;
options.c_cc[VMIN]=1;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
unsigned char rx_buffer[256];
while(1){
int rx_length = read(fd, (void*)rx_buffer, 255);
if (rx_length > 0)
{
//Bytes received
rx_buffer[rx_length] = '\0';
printf("%i bytes read : %s\n", rx_length, rx_buffer);
}
}
close(fd);
return 0;
}
2. 在NanoPC T2 的Debug端口座接上USB转TTL调试小板,打开putty调试软件,依次设置serial, 115200, 8-N-1
T2开机运行friendlycore系统,
新建read.c源文件: vi read.c
然后将代码敲进去,或者复制进去,保存退出。
编译:
pi@NanoPC-T2:~$ gcc read.c -o read
运行:
pi@NanoPC-T2:~$ ./read
Open Serial Port Error!
: Permission denied
提示权限不够
pi@NanoPC-T2:~$ sudo ./read
[sudo] password for pi: 输入pi用户的密码,输入pi回车
,则read程序开始运行,等待接收UART3传输过来的数据。
3. 由于串口是独占设备,所以我们再重新运行一次putty,这次协议为SSH, 输入T2的IP地址(可以提前从上面的串口终端查看IP地址:ifconfig eth0)
在SSH端口输入:
root@NanoPC-T2:~# echo hello > /dev/ttyAMA3
也就是将字符串"hello"发送给UART3
这个时候,另外一个putty窗口将立即收到这个字符串:
pi@NanoPC-T2:~$ sudo ./read
6 bytes read : hello
NanoPC T2应用范围广泛,下面列出目前最热门的几大领域:
举报/反馈