Linux串口UART测试程序,收到什么,打印什么。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
-
- #define FALSE -1
- #define TRUE 0
- #define flag 1
- #define noflag 0
-
- int wait_flag = noflag;
- int STOP = 0;
- int res;
-
- int speed_arr[] =
- { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600,
- B4800, B2400, B1200, B300,B115200 };
- int name_arr[] =
- { 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400,
- 1200, 300, 115200,};
- void
- set_speed (int fd, int speed)
- {
- int i;
- int status;
- struct termios Opt;
- tcgetattr (fd, &Opt);
- for (i = 0; i < sizeof (speed_arr) / sizeof (int); i++)
- {
- if (speed == name_arr[i])
- if (speed == name_arr[i])
- {
- tcflush (fd, TCIOFLUSH);
- cfsetispeed (&Opt, speed_arr[i]);
- cfsetospeed (&Opt, speed_arr[i]);
- status = tcsetattr (fd, TCSANOW, &Opt);
- if (status != 0)
- {
- perror ("tcsetattr fd1");
- return;
- }
- tcflush (fd, TCIOFLUSH);
- }
- }
- }
-
- int
- set_Parity (int fd, int databits, int stopbits, int parity)
- {
- struct termios options;
- if (tcgetattr (fd, &options) != 0)
- {
- perror ("SetupSerial 1");
- return (FALSE);
- }
- options.c_cflag &= ~CSIZE;
- options.c_lflag &= ~ECHO; //这个是重点!!!!!!!! 关闭回显功能
- switch (databits)
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- fprintf (stderr, "Unsupported data size\n");
- return (FALSE);
- }
- switch (parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; /* Clear parity enable */
- options.c_iflag &= ~INPCK; /* Enable parity checking */
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB);
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; /* Enable parity */
- options.c_cflag &= ~PARODD;
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'S':
- case 's': /*as no parity */
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf (stderr, "Unsupported parity\n");
- return (FALSE);
- }
-
- switch (stopbits)
- {
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf (stderr, "Unsupported stop bits\n");
- return (FALSE);
- }
- /* Set input parity option */
- if (parity != 'n')
- options.c_iflag |= INPCK;
- tcflush (fd, TCIFLUSH);
- options.c_cc[VTIME] = 150;
- options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
- if (tcsetattr (fd, TCSANOW, &options) != 0)
- {
- perror ("SetupSerial 3");
- return (FALSE);
- }
- return (TRUE);
- }
-
- void
- signal_handler_IO (int status)
- {
- printf ("received SIGIO signale.\n");
- wait_flag = noflag;
- }
-
-
- void StrToHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
- {
- char h1,h2;
- unsigned char s1,s2;
- int i;
-
- for (i=0; i
- {
- h1 = pbSrc[2*i];
- h2 = pbSrc[2*i+1];
-
- s1 = toupper(h1) - 0x30;
- if (s1 > 9)
- s1 -= 7;
-
- s2 = toupper(h2) - 0x30;
- if (s2 > 9)
- s2 -= 7;
-
- pbDest[i] = s1*16 + s2;
- }
- }
-
-
-
- void print_use()
- {
- printf("*************Send hex data to uart and waitting timeout recv data************************ \n");
- printf("uart-test -d [/dev/ttyS0] -b [Baudrate 115200] -s [hex data eg:AA550102] \n");
- printf("uart-test -d [/dev/ttyS0] -b [Baudrate 115200] -s [hex data eg:AA550102] \n");
- printf("uart-test -d [/dev/ttyS0] -b [Baudrate 115200] -s [hex data eg:AA550102] \n");
- printf("******************************************************************* \n");
-
- }
-
-
- int main (int argc, char* argv[])
- {
- int i=0;
- int j=0;
- printf ("This program updates last time at %s %s\n", __TIME__, __DATE__);
- printf ("TEST FOR ZTJY IOT HW \n");
- print_use();
- if(argc != 7)
- {
- print_use();
- return -1;
- }
-
- int fd;
- // fd = open (argv[2], O_RDWR|);
- fd = open (argv[2], O_RDWR|O_NOCTTY|O_NONBLOCK); //注意这里的标志位 是重点
- if (fd == -1)
- {
- perror ("serialport open error\n");
- return -1;
- }
- else
- {
- printf ("open ");
- printf ("%s", argv[2]);
- printf (" succesfully\n");
- }
-
- set_speed (fd, atoi(argv[4]));
- if (set_Parity (fd, 8, 1, 'N') == FALSE)
- {
- printf ("Set Parity Error\n");
- exit (0);
- }
-
-
- unsigned char sendData[1024]={0};
- StrToHex(sendData,argv[6],strlen(argv[6])/2);
- write(fd,sendData,strlen(sendData));
- //fflush();
- printf("Send Data: \n");
- for(i=0;i<strlen(sendData);i++)
- {
- printf("-0x%2x-",sendData[i]);
- }
- printf("\n");
-
- char buf[2048];
- fd_set rd;
- int nread = 0;
- while(1)
- {
- FD_ZERO(&rd);
- FD_SET(fd, &rd);
- while(FD_ISSET(fd, &rd))
- {
- if(select(fd+1, &rd, NULL,NULL,NULL) < 0)
- {
- perror("select error\n");
- }
- else
- {
- while((nread = read(fd, buf, sizeof(buf))) > 0)
- {
- printf("nread = %d,%s\n",nread, buf);
- for(j=0;j
- {
- printf("-0x%2x-",buf[j]);
- }
- printf("\n");
- printf("\n");
- printf("\n");
- memset(buf, 0 , sizeof(buf));
- printf("Exit uart-teset App\r\n");
- break;
- }
- }
- }
- }
- close (fd);
- printf("Exit uart-teset App");
- return 0;
- }
-
相关阅读:
Python21天学习挑战赛Day(9)·操作MongoDB数据库
【学习】如何高效地进行集成测试
Python自学之路--002:Python 如何生成exe可执行文件
JMeter压测如何分配业务比例?
常用的 Spring Boot 注解及其作用
LeetCoded贪心算法系列——455.分发饼干
Elasticsearch 安装踩坑!
智能安防系统-视频监控系统
二.maven常用功能点
分割常用损失函数
-
原文地址:https://blog.csdn.net/hjj651471519/article/details/133425679