I.MX6 Android Linux UART send receive with multi-thread and multi-mode demo
/*******************************************************************************************
* 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"
I.MX6 Android Linux UART send receive with multi-thread and multi-mode demo的更多相关文章
- I.MX6 Android Linux shell MMPF0100 i2c 设置数据
#!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 设置数据 # 说明: # 本文主要记录通过shell脚本来设置 ...
- I.MX6 Android Linux shell MMPF0100 i2c 获取数据
#!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 获取数据 # 说明: # 本文主要记录通过shell脚本来获取 ...
- I.MX6 Android U-blox miniPCI 4G porting
/************************************************************************** * I.MX6 Android U-blox m ...
- I.MX6 Android 5.1 纯Linux、U-Boot编译
/***************************************************************************** * I.MX6 Android 5.1 纯 ...
- I.MX6 Android iperf3 porting failed
/***************************************************************************** * I.MX6 Android iperf ...
- I.MX6 Android netperf
/***************************************************************************** * I.MX6 Android netpe ...
- I.MX6 android 获取framebuffer信息
/******************************************************************************** * I.MX6 android 获取 ...
- I.MX6 Android i2c-tools porting
/************************************************************************** * I.MX6 Android i2c-tool ...
- I.MX6 android BatteryService jni hacking
/**************************************************************************** * I.MX6 android Batter ...
随机推荐
- chrome的url列表里面也找不到chrome://plugins的原因
plugins 页面被移除后,可以访问: chrome://settings/content 调整 Flash.PDF 的设置. 原本 plugins 页面就基本只剩下这两货了,移除掉确实没啥影响. ...
- 51nod 1682 中位数计数(前缀和)
51nod 1682 中位数计数 思路: sum[i]表示到i为止的前缀和(比a[i]小的记为-1,相等的记为0,比a[i]大的记为1,然后求这些-1,0,1的前缀和): hash[sum[i]+N] ...
- 20170728xlVba SSC_TODAY
Public Sub SSC_TODAY() Dim strText As String Dim Reg As Object, Mh As Object, OneMh As Object Dim i ...
- Python基础--Python简介和入门
☞写在前面 在说Python之前,我想先说一下自己为什么要学Python,我本人之前也了解过Python,但没有深入学习.之前接触的语言都是Java,也写过一些Java自动化用例,对Java语言只能说 ...
- 42. Trapping Rain Water *HARD*
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 未能加载文件或程序集“LinqToExcel”或它的某一个依赖项。试图加载格式不正确的程序。
未能加载文件或程序集“*”或它的某一个依赖项.试图加载格式不正确的程序. 原因:操作系统是64位的,但发布的程序引用了一些32位的ddl,所以出现了兼容性的问题解决方案一:如果是64位机器,IIS—— ...
- spring boot 学习(八)定时任务 @Scheduled
SpringBoot 定时任务 @Scheduled 前言 有时候,我们有这样的需求,需要在每天的某个固定时间或者每隔一段时间让应用去执行某一个任务.一般情况下,可以使用多线程来实现这个功能:在 Sp ...
- list<map>排序
@Test public void test_when_test1() throws Exception { String[] strs = {"e", "ee" ...
- POJ 2752 KMP中next数组的理解
感觉这里讲的挺好的.http://cavenkaka.iteye.com/blog/1569062 就是不断递归next数组.长度不断减小. 题意:给你一个串,如果这个串存在一个长度为n的前缀串,和长 ...
- learning scala 操作符
scala 操作符: 算术运算符: + - * / % 关系统运算符: > , < ,= ,!= ,>=,<=, 逻辑运算符: && . || , ! 位 ...