1 RS232驱动测试
1.1 编写应用测试程序
本实验例程路径:i.MX6UL终结者光盘资料/06_Linux驱动例程/19_uart
创建uart_test.c应用测试程序,具体内容如下:
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <termios.h>
8 #include <errno.h>
9 #include <poll.h>
10
11 int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
12 {
13 struct termios newtio,oldtio;
14 if ( tcgetattr( fd,&oldtio) != 0) {
15 perror("SetupSerial 1");
16 return -1;
17 }
18 bzero( &newtio, sizeof( newtio ) );
19 newtio.c_cflag |= CLOCAL | CREAD;
20 newtio.c_cflag &= ~CSIZE;
21
22 switch( nBits )
23 {
24 case 7:
25 newtio.c_cflag |= CS7;
26 break;
27 case 8:
28 newtio.c_cflag |= CS8;
29 break;
30 }
31
32 switch( nEvent )
33 {
34 case 'O':
35 newtio.c_cflag |= PARENB;
36 newtio.c_cflag |= PARODD;
37 newtio.c_iflag |= (INPCK | ISTRIP);
38 break;
39 case 'E':
40 newtio.c_iflag |= (INPCK | ISTRIP);
41 newtio.c_cflag |= PARENB;
42 newtio.c_cflag &= ~PARODD;
43 break;
44 case 'N':
45 newtio.c_cflag &= ~PARENB;
46 break;
47 }
48
49 switch( nSpeed )
50 {
51 case 2400:
52 cfsetispeed(&newtio, B2400);
53 cfsetospeed(&newtio, B2400);
54 break;
55 case 4800:
56 cfsetispeed(&newtio, B4800);
57 cfsetospeed(&newtio, B4800);
58 break;
59 case 9600:
60 cfsetispeed(&newtio, B9600);
61 cfsetospeed(&newtio, B9600);
62 break;
63 case 115200:
64 cfsetispeed(&newtio, B115200);
65 cfsetospeed(&newtio, B115200);
66 break;
67 case 460800:
68 cfsetispeed(&newtio, B460800);
69 cfsetospeed(&newtio, B460800);
70 break;
71 default:
72 cfsetispeed(&newtio, B9600);
73 cfsetospeed(&newtio, B9600);
74 break;
75 }
76 if( nStop == 1 )
77 newtio.c_cflag &= ~CSTOPB;
78 else if ( nStop == 2 )
79 newtio.c_cflag |= CSTOPB;
80 newtio.c_cc[VTIME] = 0;
81 newtio.c_cc[VMIN] = 0;
82 tcflush(fd,TCIFLUSH);
83 if((tcsetattr(fd,TCSANOW,&newtio))!=0)
84 {
85 perror("com set error");
86 return -1;
87 }
88
89 // printf("set done!\n\r");
90 return 0;
91 }
92 int main(int argc , char **argv)
93 {
94 int fd,wr_static,ret,nread,count=0;
95 char *buffer = "hello world!\r\n";
96 char buff[8];
97 int i;
98
99 if(argc < 3)
100 printf("Usage ...");
101
102 printf("\r\n uart__test start\r\n");
103
104 char *uart = argv[1];
105
106 if((fd = open(uart, O_RDWR|O_NOCTTY|O_NDELAY))<0){
107 printf("open %s is failed",uart);
108 }
109 else {
110 printf("open %s is success\n",uart);
111 set_opt(fd, 115200, 8, 'N', 1);
112 }
113
114 if(atoi(argv[2]) == 0)
115 {
116 while(1){
117 if (ret == 0)
118 printf("write time out\n");
119 else{
120 ret = write(fd,buffer, strlen(buffer));
121 sleep(1);
122 }
123 }
124 }
125
126 else if(atoi(argv[2]) == 1)
127 {
128 memset(buff,0,8);
129 while(1){
130 while((nread = read(fd,buff,8))>0){
131 //count+=nread;
132 //printf("count = %d\r\n",count);
133 printf("read: ");
134 for(i = 0; buff[i] != 0; i++)
135 printf("%c",buff[i]);
136 printf("\r\n");
137 memset(buff,0,8);
138 }
139 }
140 }
141
142 close(fd);
143 return 0;
144 }
第11~91行,用于设置串口的波特率。
第92行,main函数需要两个参数,第一个参数是串口的设备节点文件,比如/dev/ttymxc2,第二个参数选择读写数据,0:写数据,1:读数据。
第111行,当打开串口设备文件成功时,设置串口参数为:115200/8/N/1。用户可以根据实际情况修改。
第114~124行,当指令为写数据时执行。使用sleep函数延时,1s发一次数据。
第126~140行,当指令为读数据时执行。
1.2 运行测试
首先使用下面的命令编译应用测试程序:
arm-linux-gnueabihf-gcc -o uart_test uart_test.c
编译成功,得到uart_test应用程序。
然后开始测试RS232功能,因为RS232是TTL电平所以需要USB转TTL电平设备,然后连接开发板上的uart3引脚,在电脑打开USB转TTL电平设备的终端,如图 1.2.1所示:
选择正确的port端口,应用程序中默认波特率为115200,所以设置波特率为115200,然后连接设备。
拷贝编译好的uart_test应用测试程序到开发板中,执行下面命令进行写数据实现:
./uart_test /dev/ttymxc2 0 &
运行结果如图 1.2.2所示:
使用下面的命令进行读数据:
./uart_test /dev/ttymxc2 1 &
运行结果如图 1.2.3所示:
应用程序每次接受8个字节的数据,可以看出运行正常。
2 RS485测试
其实RS485测试和RS232测试流程一样,只不过接口不一样,需要使用USB转485设备。应用测试程序也是一样的。在这里就不重复测试了,用户可以自己试验一下。