把之前在新浪博客里写的分享也备份移植到CSDN博客,之前本文博客地址是:http://blog.sina.com.cn/s/blog_14ed06d6c0102wqdl.html
在Android串口通信:基本知识梳理(http://gqdy365.iteye.com/admin/blogs/2188846)的基础上,我结合我项目中使用串口的实例,进行总结;
Android使用jni直接进行串口设备的读写网上已经有开源项目了,本文是基于网上的开源项目在实际项目中的使用做的调整和优化;
Google串口开源项目见:https://code.google.com/p/android-serialport-api/
下面是我项目中的相关代码及介绍:
1、SerialPort.cpp
-
- #include
- #include
- #include
- #include
<<span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">assert .h> -
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #include
"android/log.h" - static
const char *TAG = "serial_port"; - #define
LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args) - #define
LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args) - #define
LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args) -
- static
speed_t getBaudrate(jint baudrate) { -
switch (baudrate) { -
case 0: -
return B0; -
case 50: -
return B50; -
case 75: