/*******************************************************************************************
* 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 = ;
int serialNumber = ;
int baudRate = ;
int workMode = ; // 1 send; 2 recv; 3 send and recv
char serialString[] = {}; char sendString[] = {};
char recvString[] = {}; 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 ) != )
return -; getSerial ( argv[] );
baudRate = getBaudRate ( argv[] );
workMode = getWorkMode ( argv[] ); initSerial (); if ( workMode == RECVWORKMODE || workMode == SENDANDRECVWORKMODE ) {
pthread_create ( &recv_thread, NULL, recvDataThread, NULL );
} pthread_create ( &quitOut_thread, NULL, quitOutThead, NULL ); int i = ;
while ( ) { if ( workMode == SENDWORKMODE || workMode == SENDANDRECVWORKMODE ) {
sprintf ( sendString, "%03d: %s\r\n", i++, argv[] );
uart_send ( serial_fd, sendString, strlen ( sendString ) );
}
usleep ( );
} close ( serial_fd );
return ;
} int getWorkMode ( char *workModeString ) { int ret = atoi( workModeString ); switch ( ret ) {
case :
printf ( "workMode: send.\n" );
break;
case :
printf ( "workMode: recv.\n" );
break;
case :
printf ( "workMode: send and recv.\n" );
break;
default:
printf ( "none of this workMode.\n" );
exit ( );
} return ret;
} void *quitOutThead(void *arg) { //system("stty raw -echo"); can't work well in Android linux
char ch = '\0'; while ( ) {
scanf ( "%c", &ch );
if ( ch == 'q' || ch == 'Q' ) {
exit ( );
}
}
} void *recvDataThread(void *arg) { int ret = ;
int i = ; while ( ) {
ret = uart_recv ( serial_fd, recvString, sizeof(recvString) );
printf ( "%03d %s\n", i++, recvString );
bzero ( recvString, sizeof(recvString) );
usleep ( );
}
} int help( int argc ) { if ( argc != ) {
printf ( "USAGE:\n" );
printf ( " command <serial absolute path> <baudRate> <workMode> <send String>\n" );
printf ( " example:\n" );
printf ( " ./uartRS /dev/ttymxc1 115200 3 \"1234567890ABCDEFG\"\n" );
return -;
} return ;
} int uart_recv(int fd, char *data, int datalen) { int ret = ; ret = read ( fd, data, datalen ); return ret;
} int uart_send(int fd, char *data, int datalen) { int len = ; len = write ( fd, data, datalen ); //实际写入的长度
if(len == datalen) {
return len;
} else {
tcflush(fd, TCOFLUSH); //TCOFLUSH刷新写入的数据但不传送
return -;
} return ;
} int initSerial( void ) { //serial_fd = open( serialString, O_RDWR | O_NOCTTY | O_NDELAY );
serial_fd = open ( serialString, O_RDWR );
if ( serial_fd < ) {
perror ( "open" );
return -;
} // 串口主要设置结构体termios <termios.h>
struct termios options; /**
* tcgetattr函数用于获取与终端相关的参数,参数fd为终端的文件描述符,
* 返回的结果保存在termios结构体中
*/
tcgetattr ( serial_fd, &options );
/**2. 修改所获得的参数*/
options.c_cflag |= (CLOCAL | CREAD); //设置控制模式状态,本地连接,接收使能
options.c_cflag &= ~CSIZE; //字符长度,设置数据位之前一定要屏掉这个位
options.c_cflag &= ~CRTSCTS; //无硬件流控
options.c_cflag |= CS8; //8位数据长度
options.c_cflag &= ~CSTOPB; //1位停止位
options.c_iflag |= IGNPAR; //无奇偶检验位
options.c_oflag = ; //输出模式
options.c_lflag = ; //不激活终端模式
cfsetospeed ( &options, baudRate ); //设置波特率
//cfsetospeed(&options, B2000000); //设置波特率 /* 设置新属性,TCSANOW:所有改变立即生效*/
tcflush ( serial_fd, TCIFLUSH ); //溢出数据可以接收,但不读
tcsetattr ( serial_fd, TCSANOW, &options ); return ;
} void getSerial ( char* commandLineserialString ) { sprintf ( serialString, "%s", commandLineserialString );
printf ( "serialString : %s.\n", serialString );
} /**
* 该函数之所以采用这种方式,主要是为了波特率出错时方便调试
*/
int getBaudRate ( char* baudRateString ) {
int ret = atoi ( baudRateString );
switch ( ret ) {
case :
printf ( "baudRate %s.\n", "" );
ret = B0;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B50;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B75;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B110;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B134;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B150;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B200;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B300;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B600;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B1200;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B1800;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B2400;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B4800;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B9600;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B19200;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B38400;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B57600;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B115200;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B230400;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B460800;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B500000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B576000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B921600;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B1000000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B1152000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B1500000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B2000000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B2500000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B3000000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B3500000;
break;
case :
printf ( "baudRate %s.\n", "" );
ret = B4000000;
break;
default:
printf ( "baudRate is not exist %s.\n", "" );
ret = B0;
}
//printf ("baudRate %s.\n", baudRateString);
return ret;
} 二、cat Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS)
LOCAL_MODULE := uartRs
#LOCAL_STATIC_LIBRARIES :=libpthread
#LOCAL_SHARED_LIBRARIES :=libpthread
LOCAL_STATIC_LIBRARIES :=libpthread
LOCAL_SRC_FILES := uartRS.c
#LOCAL_LDLIBS += -lpthread include $(BUILD_EXECUTABLE) 三、cat autorun.sh
# ./uartRS <serial absolute path> <baudRate> <work mode> <send String>
# .serial absolute path
# the absolute path for serial port, example:
# /dev/ttymxc1
# . referrence baudrate:
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# . work mode
# . send;
# . recv;
# . send and recv
# . send string
# the string what you want to send chmod ./uartRS # give excute-permission
# ./uartRS <serial absolute path> <baudRate> <work mode> <send String>
./uartRS /dev/ttymxc1 "1234567890ABCDEF"