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 ...
随机推荐
- 编写 R Markdown 文档
数据分析师的工作不仅是将数据放入模型并得出一些结论.通常需要完成从数据收集.数据清理.可视化.建模再到最后编写报告或制作演示文稿的完整工作流程.在前面几章中,我们从不同方面深入学习 R 编程语言,从各 ...
- jq 便捷api jq 常用 api jq 快捷 api
jq 选择器 1.相对值用法 width("+=250px") $("input").width("+=250px"); 2.使用函数来 ...
- C#中的?
1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型也 ...
- Android Toast.makeText用法
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.下面用一个实例来看看如何使用Toast ...
- Linux 设置程序开机自启动 (命令systemctl 和 chkconfig用法区别比较)
之前在Linux centos 7 上安装了apache 和mysql,当时并没有设置开机自动启动. 最近又重新练习网页,每次开机总是要手动启动httpd和mysqld,不方便,就想设置成开机自动启动 ...
- 读写生信流程必备的 Perl 语法
最早就是写Perl的,后来来到公司转Python,现在又要负责流程了,开始重拾Perl,当然是借鉴别人现有的语法,我再重新组合. 基本语法就不介绍了,参照我之前文章 Perl 模块 use str ...
- Confluence 6 目录序列将会影响
这个部分将会对用户目录序列对登录和权限以及更新用户和用户组的影响进行描述. 登录 用户目录的排序在用户登录系统中的影响是非常重要的,尤其对一个相同用户名的用户在多个目录中存在的情况.当用户在系统中进行 ...
- Android出现declaration of org.apache.http.message.BasicLineFormatter appears in /system/framework/
这是由于使用了CloseableHttpClient造成的,把 CloseableHttpClient httpclient = HttpClients.createDefault(); Closea ...
- Mac百度云盘不限速操作步骤
第一步:下载所需工具:(①②步我放在同一个文件夹,可一起下载,链接失效请留言) 工具地址:链接: https://pan.baidu.com/s/1raicYzM 密码: ve3n ①下载Aria2G ...
- CentOS7 Install Docker(转)
https://linux.cn/article-4340-1.html CentOS 7 中 Docker 的安装 Docker 软件包已经包括在默认的 CentOS-Extras 软件源里.因此想 ...