102 lines
1.8 KiB
C
102 lines
1.8 KiB
C
|
|
#include <stdint.h>
|
|||
|
|
#include <unistd.h>
|
|||
|
|
#include <stdio.h>
|
|||
|
|
#include <stdlib.h>
|
|||
|
|
#include <getopt.h>
|
|||
|
|
#include <fcntl.h>
|
|||
|
|
#include <sys/ioctl.h>
|
|||
|
|
#include <linux/types.h>
|
|||
|
|
#include <string.h>
|
|||
|
|
#include <termios.h>
|
|||
|
|
|
|||
|
|
#define FY_TRUE 1
|
|||
|
|
#define FY_FALSE 0
|
|||
|
|
static char device[128] = "/dev/ttyS2";
|
|||
|
|
|
|||
|
|
void SAMPLE_UART_Usage(void)
|
|||
|
|
{
|
|||
|
|
printf("please choose the case which you want to run:\n");
|
|||
|
|
printf("\t1: UART Send Test\n");
|
|||
|
|
printf("\t2: UART Recv Test\n");
|
|||
|
|
|
|||
|
|
printf("\n");
|
|||
|
|
printf("================================================================\n");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int send_test(int fd,char *s)
|
|||
|
|
{
|
|||
|
|
int len = 0;
|
|||
|
|
//<2F><><EFBFBD>鷢<EFBFBD>͵<EFBFBD><CDB5>ַ<EFBFBD>
|
|||
|
|
printf("send data:%s\n", s);
|
|||
|
|
len = strlen(s);
|
|||
|
|
write(fd, s, len);
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void recv_test(int fd,char *s)
|
|||
|
|
{
|
|||
|
|
int len = 0;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ַ<EFBFBD>
|
|||
|
|
while((len = read(fd, s, 12)) != 0){
|
|||
|
|
s[12] = '\0';
|
|||
|
|
printf("%s", s);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int main(int argc, char *argv[])
|
|||
|
|
{
|
|||
|
|
char s[] = "hello world\n";
|
|||
|
|
char s1[13] = {0};
|
|||
|
|
char ch;
|
|||
|
|
int bExit = FY_FALSE;
|
|||
|
|
|
|||
|
|
//<2F>ڴ˴<DAB4><CBB4><EFBFBD><F2BFAAB4><EFBFBD><EFBFBD>豸
|
|||
|
|
if (argc > 1)
|
|||
|
|
memcpy(device, argv[1], strlen(argv[1]));
|
|||
|
|
|
|||
|
|
printf("\n");
|
|||
|
|
printf("================================================================\n");
|
|||
|
|
printf("Test Device is %s\n", device);
|
|||
|
|
printf("================================================================\n");
|
|||
|
|
|
|||
|
|
int fd = open(device, O_RDWR | O_NOCTTY);
|
|||
|
|
if (fd < 0)
|
|||
|
|
printf("can't open device\n");
|
|||
|
|
|
|||
|
|
|
|||
|
|
while(1) {
|
|||
|
|
SAMPLE_UART_Usage();
|
|||
|
|
ch = getchar();
|
|||
|
|
if(10 == ch)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
getchar();
|
|||
|
|
switch (ch)
|
|||
|
|
{
|
|||
|
|
case '1':
|
|||
|
|
send_test(fd,s);
|
|||
|
|
break;
|
|||
|
|
case '2':
|
|||
|
|
recv_test(fd,s1);
|
|||
|
|
break;
|
|||
|
|
case 'q':
|
|||
|
|
case 'Q':
|
|||
|
|
{
|
|||
|
|
bExit = FY_TRUE;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
default :
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
if (bExit)
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//<2F>ر<EFBFBD><D8B1>ļ<EFBFBD>
|
|||
|
|
close(fd);
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|