102 lines
2.3 KiB
C
Raw Permalink Normal View History

2025-05-10 21:58:58 +08:00
#include <stdio.h> /*<2A><>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
#include <stdlib.h> /*<2A><>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2B6A8>*/
#include <unistd.h> /*Unix<69><78>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /*<2A>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>*/
#include <termios.h> /*PPSIX<49>ն˿<D5B6><CBBF>ƶ<EFBFBD><C6B6><EFBFBD>*/
#include <errno.h> /*<2A><><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD>*/
#include <sys/time.h>
#include <string.h>
#include <getopt.h>
#define TRUE 1
#define FALSE -1
int main(int argc, char **argv)
{
int fd;
int nread;
char buffer[512];
int n=0,i=0;
char* dev = NULL;
struct termios oldtio,newtio;
speed_t speed = B115200;
int next_option,havearg = 0,flow = 0;
const char *const short_opt = "fd:";
const struct option long_opt[] = {
{"devices",1,NULL,'d'},
{"hard_flow",0,NULL,'f'},
{NULL,0,NULL,0},
};
do{
next_option = getopt_long(argc,argv,short_opt,long_opt,NULL);
switch (next_option) {
case 'd':
dev = optarg;
havearg = 1;
break;
case '?':
printf("usage: fltest_uarttest -d <uart_dev>\n");
break;
case -1:
if(havearg)
break;
default:
printf("usage: fltest_uarttest -d /dev/ttyS5 \n");
exit(1);
}
}while(next_option != -1);
if(dev == NULL)
{
printf("Please input seria device name ,for exmaple /dev/ttySAC0.\nNote:This is loop test application. Make sure that your serial is loop\n");
exit(1);
}
/* <20>򿪴<EFBFBD><F2BFAAB4><EFBFBD> */
fd = open(dev, O_RDWR | O_NONBLOCK| O_NOCTTY | O_NDELAY);
if (fd < 0) {
printf("Can't Open Serial Port!\n");
exit(0);
}
printf("Welcome to uart test\n");
//save to oldtio
tcgetattr(fd,&oldtio);
bzero(&newtio,sizeof(newtio));
newtio.c_cflag = speed|CS8|CLOCAL|CREAD;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~PARENB;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
tcgetattr(fd,&oldtio);
memset(buffer,0,sizeof(buffer));
char test[100]="forlinx_uart_test.1234567890...";
printf("Send test data:\n%s\n",test);
write(fd, test, strlen(test) + 1);
fd_set rd;
while(1)
{
int ret;
nread = read(fd, &buffer[n], 1);
if (strlen(test) == strlen(buffer))
{
printf("Read Test Data finished,Read:\n%s\n",buffer);
memset(buffer,0,sizeof(buffer));
tcflush(fd, TCIOFLUSH);
break;
}
n += nread;
}
close(fd);
}