摘要:本篇介紹android中SerialPort串口通訊學(xué)習(xí)和使用。主要用到android-serialport-api。
SerialPort
幾個工程參考學(xué)習(xí)使用
android-serialport-api
Google開源的Android串口通信Demo android-serialport-api
源碼下載
cepr/android-serialport-api
SerialPort獲取串口輸入輸出流
SerialPortFinder獲取硬件地址
可以推薦看android串口通信——android-serialport-api 源
Android-SerialPort-API
源碼下載
licheedev/Android-SerialPort-API
readme
版本
添加依賴:implementation ‘com.licheedev:android-serialport:2.1.3’
allprojects {
repositories {
...
jcenter()
mavenCentral() // since 2.1.3
}
}
dependencies {
implementation 'com.licheedev:android-serialport:2.1.3'
}
除了選擇2.1.3版本,還可以看 releases 選擇其他版本。
Android-SerialPort-Tool
源碼下載
licheedev/Android-SerialPort-Tool
Android-Serialport
源碼下載
xmaihh/Android-Serialport
使用方法
Android移植谷歌官方串口庫支持校驗位、數(shù)據(jù)位、停止位、流控配置
1. 檢驗參數(shù)合法性
/* Check arguments */
{
speed = getBaudrate(baudrate);
if (speed == -1) {
/* TODO: throw an exception */
LOGE("Invalid baudrate");
return NULL;
}
}
2. 打開串口
/* Opening device */
{
jboolean iscopy;
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
fd = open(path_utf, O_RDWR | flags);
LOGD("open() fd = %d", fd);
(*env)->ReleaseStringUTFChars(env, path, path_utf);
if (fd == -1) {
/* Throw an exception */
LOGE("Cannot open port");
/* TODO: throw an exception */
return NULL;
}
}
3.配置波特率
/* Configure device */
{
struct termios cfg;
LOGD("Configuring serial port");
if (tcgetattr(fd, &cfg)) {
LOGE("tcgetattr() failed");
close(fd);
/* TODO: throw an exception */
return NULL;
}
cfmakeraw(&cfg);
cfsetispeed(&cfg, speed);
cfsetospeed(&cfg, speed);
4.配置數(shù)據(jù)位
cfg.c_cflag &= ~CSIZE;
switch (dataBits) {
case 5:
cfg.c_cflag |= CS5; //使用5位數(shù)據(jù)位
break;
case 6:
cfg.c_cflag |= CS6; //使用6位數(shù)據(jù)位
break;
case 7:
cfg.c_cflag |= CS7; //使用7位數(shù)據(jù)位
break;
case 8:
cfg.c_cflag |= CS8; //使用8位數(shù)據(jù)位
break;
default:
cfg.c_cflag |= CS8;
break;
}
5.配置校驗位
switch (parity) {
case 0:
cfg.c_cflag &= ~PARENB; //無奇偶校驗
break;
case 1:
cfg.c_cflag |= (PARODD | PARENB); //奇校驗
break;
case 2:
cfg.c_iflag &= ~(IGNPAR | PARMRK); // 偶校驗
cfg.c_iflag |= INPCK;
cfg.c_cflag |= PARENB;
cfg.c_cflag &= ~PARODD;
break;
default:
cfg.c_cflag &= ~PARENB;
break;
}
6.配置停止位
switch (stopBits) {
case 1:
cfg.c_cflag &= ~CSTOPB; //1位停止位
break;
case 2:
cfg.c_cflag |= CSTOPB; //2位停止位
break;
default:
break;
}
7.配置流控
switch (flowCon) {
case 0:
cfg.c_cflag &= ~CRTSCTS; //不使用流控
break;
case 1:
cfg.c_cflag |= CRTSCTS; //硬件流控
break;
case 2:
cfg.c_cflag |= IXON | IXOFF | IXANY; //軟件流控
break;
default:
cfg.c_cflag &= ~CRTSCTS;
break;
}
readme
android中使用串口通信
使用android-serialport-api方式
import android.serialport.SerialPort;
第1種 鏈接
在gradle/build.gradle中添加
maven { url "https://jitpack.io" }
在app/build.gradle文件配置中的dependencies,加上
implementation 'com.github.licheedev.Android-SerialPort-API:serialport:1.0.1'
第2種 導(dǎo)入SerialPort庫
1、將android-serialport-api中的libs的so庫資源放到項目libs中
2、將Android-SerialPort-API中的java下的文件復(fù)制到項目中java
3、修改app的build.gradle文件,在android {}里添加支持的cpu架構(gòu)
defaultConfig {
ndk {
abiFilters "armeabi","armeabi-v7a" // "x86", "arm64-v8a"
}
}
第3種 編譯SerialPort模塊
1、將Android-SerialPort-API中的serialport模塊復(fù)制到工程中,目錄與app文件同級
2、修改settings.gradle,將改成如下,即增加serialport模塊
rootProject.name = "SerialPort"
include ':app',':serialport'
3、修改app的build.gradle文件,在dependencies{}中添加project
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':serialport')
}
串口通信部分代碼
待續(xù)
好的文章推薦參考文章來源:http://www.zghlxwxcb.cn/news/detail-739584.html
Android串口通訊SerialPort(使用篇)
與君共勉!待續(xù)
歡迎指錯,一起學(xué)習(xí)文章來源地址http://www.zghlxwxcb.cn/news/detail-739584.html
到了這里,關(guān)于Android Studio的筆記--SerialPort串口通訊學(xué)習(xí)和使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!