#include #include #include #include #include #include #include #include #include #include #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; //检查发送的字符 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; //检测串口接收缓存区中的字符 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; //在此处打开串口设备 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; } } //关闭文件 close(fd); return 0; }