xref: /OK3568_Linux_fs/app/forlinx/forlinx_cmd/fltest_uarttest/uarttest.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include     <stdio.h>      /*��׼�����������*/
2 #include     <stdlib.h>     /*��׼�����ⶨ��*/
3 #include     <unistd.h>     /*Unix��׼��������*/
4 #include     <sys/types.h>  /**/
5 #include     <sys/stat.h>   /**/
6 #include     <fcntl.h>      /*�ļ����ƶ���*/
7 #include     <termios.h>    /*PPSIX�ն˿��ƶ���*/
8 #include     <errno.h>      /*����Ŷ���*/
9 #include	 <sys/time.h>
10 #include     <string.h>
11 #include     <getopt.h>
12 
13 #define TRUE 1
14 #define FALSE -1
15 
main(int argc,char ** argv)16 int main(int argc, char **argv)
17 {
18 	int fd;
19 	int nread;
20 	char buffer[512];
21 	int n=0,i=0;
22 	char* dev  = NULL;
23 	struct termios oldtio,newtio;
24 	speed_t speed = B115200;
25 	int next_option,havearg = 0,flow = 0;
26 	const char *const short_opt = "fd:";
27 	const struct option long_opt[] = {
28 		{"devices",1,NULL,'d'},
29 		{"hard_flow",0,NULL,'f'},
30 		{NULL,0,NULL,0},
31 	};
32 	do{
33 		next_option = getopt_long(argc,argv,short_opt,long_opt,NULL);
34 		switch (next_option) {
35 			case 'd':
36 				dev = optarg;
37 				havearg = 1;
38 				break;
39 			case '?':
40 				printf("usage: fltest_uarttest -d <uart_dev>\n");
41 				break;
42 			case -1:
43 				if(havearg)
44 					break;
45 			default:
46 				printf("usage: fltest_uarttest -d  /dev/ttyS5 \n");
47 				exit(1);
48 
49 		}
50 	}while(next_option != -1);
51 
52 	if(dev == NULL)
53 	{
54 		printf("Please input seria device name ,for exmaple /dev/ttySAC0.\nNote:This is loop test application. Make sure that your serial is loop\n");
55 
56 			exit(1);
57 	}
58 	/*  ������ */
59 	fd = open(dev, O_RDWR | O_NONBLOCK| O_NOCTTY | O_NDELAY);
60 	if (fd < 0)	{
61 		printf("Can't Open Serial Port!\n");
62 		exit(0);
63 	}
64 
65 	printf("Welcome to uart test\n");
66 
67 	//save to oldtio
68 	tcgetattr(fd,&oldtio);
69 	bzero(&newtio,sizeof(newtio));
70 	newtio.c_cflag = speed|CS8|CLOCAL|CREAD;
71 	newtio.c_cflag &= ~CSTOPB;
72 	newtio.c_cflag &= ~PARENB;
73 	newtio.c_iflag = IGNPAR;
74 	newtio.c_oflag = 0;
75 	tcflush(fd,TCIFLUSH);
76 	tcsetattr(fd,TCSANOW,&newtio);
77 	tcgetattr(fd,&oldtio);
78 
79 	memset(buffer,0,sizeof(buffer));
80 
81 	char test[100]="forlinx_uart_test.1234567890...";
82 
83 	printf("Send test data:\n%s\n",test);
84 	write(fd, test, strlen(test) + 1);
85 	fd_set rd;
86 	while(1)
87 	{
88 		int ret;
89 		nread = read(fd, &buffer[n], 1);
90 		if (strlen(test) == strlen(buffer))
91 		{
92 			printf("Read Test Data finished,Read:\n%s\n",buffer);
93 			memset(buffer,0,sizeof(buffer));
94 			tcflush(fd, TCIOFLUSH);
95 			break;
96 		}
97 		n += nread;
98 	}
99 	close(fd);
100 }
101 
102 
103