/******************************************************************************************* * I.MX6 Android Linux UART send receive with multi-thread and multi-mode demo * 声明: * 1. 本软件是为了测试Android底层的Linux驱动以及硬件是否存在缺陷而编写的测试软件; * 2. 本软件可以设置为发送、接收、发送并接收模式,并且提供q+enter键退出程序; * 3. 本人是采用NDK的方式进行编译的,并采用autorun.sh脚本进行调用,主要是不想敲太多键盘; * 4. 本程序并不打算给出太多的注释,没有理由; * 5. 如果想知道波特率更多细节,可以参考本人的《I.MX6 Linux Serial Baud Rate hacking》。 * * 2015-8-22 晴 深圳 南山平山村 曾剑锋 ******************************************************************************************/ \\\\-*- 目录 -*-//////// | 一、cat uartRS.c | | 二、cat Android.mk | | 三、cat autorun.sh | \\\\\\\////////////// 一、cat uartRS.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <errno.h> #include <pthread.h> #define SENDWORKMODE 1 #define RECVWORKMODE 2 #define SENDANDRECVWORKMODE 3 int serial_fd = 0; int serialNumber = 0; int baudRate = 0; int workMode = 0; // 1 send; 2 recv; 3 send and recv char serialString[20] = {0}; char sendString[512] = {0}; char recvString[512] = {0}; int help ( int argc ); int initSerial ( void ); int getBaudRate ( char *baudRateString ); void getSerial ( char* commandLineserialString ); int getWorkMode ( char *workMode ); int uart_send ( int fd, char *data, int datalen ); int uart_recv ( int fd, char *data, int datalen ); pthread_t recv_thread; void *recvDataThread ( void *arg ); pthread_t quitOut_thread; void *quitOutThead ( void *arg ); int main(int argc, char **argv) { if ( help( argc ) != 0) return -1; getSerial ( argv[1] ); baudRate = getBaudRate ( argv[2] ); workMode = getWorkMode ( argv[3] ); initSerial (); if ( workMode == RECVWORKMODE || workMode == SENDANDRECVWORKMODE ) { pthread_create ( &recv_thread, NULL, recvDataThread, NULL ); } pthread_create ( &quitOut_thread, NULL, quitOutThead, NULL ); int i = 0; while ( 1 ) { if ( workMode == SENDWORKMODE || workMode == SENDANDRECVWORKMODE ) { sprintf ( sendString, "%03d: %s ", i++, argv[4] ); uart_send ( serial_fd, sendString, strlen ( sendString ) ); } usleep ( 100000 ); } close ( serial_fd ); return 0; } int getWorkMode ( char *workModeString ) { int ret = atoi( workModeString ); switch ( ret ) { case 1 : printf ( "workMode: send. " ); break; case 2 : printf ( "workMode: recv. " ); break; case 3 : printf ( "workMode: send and recv. " ); break; default: printf ( "none of this workMode. " ); exit ( 0 ); } return ret; } void *quitOutThead(void *arg) { //system("stty raw -echo"); can't work well in Android linux char ch = '