Skip to content

Commit

Permalink
增加可以获取SerialPort的属性
Browse files Browse the repository at this point in the history
  • Loading branch information
licheedev committed Oct 22, 2020
1 parent 6661aad commit f66b632
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

```
dependencies {
implementation 'com.licheedev:android-serialport:2.1.1'
implementation 'com.licheedev:android-serialport:2.1.2'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ ext {
targetSdkVersion = 29

versionCode = 2
versionName = "2.1.1"
versionName = "2.1.2"
}
3 changes: 3 additions & 0 deletions serialport/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies {
// })
// compile 'com.android.support:appcompat-v7:25.3.0'
// testCompile 'junit:junit:4.12'


api "androidx.annotation:annotation:1.1.0"
}

apply from: './bintray.gradle'
Expand Down
79 changes: 74 additions & 5 deletions serialport/src/main/java/android/serialport/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package android.serialport;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
Expand All @@ -32,13 +34,19 @@ public final class SerialPort {
public static final String DEFAULT_SU_PATH = "/system/bin/su";

private static String sSuPath = DEFAULT_SU_PATH;
private File device;
private int baudrate;
private int dataBits;
private int parity;
private int stopBits;
private int flags;

/**
* Set the su binary path, the default su binary path is {@link #DEFAULT_SU_PATH}
*
* @param suPath su binary path
*/
public static void setSuPath(String suPath) {
public static void setSuPath(@Nullable String suPath) {
if (suPath == null) {
return;
}
Expand All @@ -50,6 +58,7 @@ public static void setSuPath(String suPath) {
*
* @return
*/
@NonNull
public static String getSuPath() {
return sSuPath;
}
Expand All @@ -73,8 +82,15 @@ public static String getSuPath() {
* @throws SecurityException
* @throws IOException
*/
public SerialPort(File device, int baudrate, int dataBits, int parity, int stopBits, int flags)
throws SecurityException, IOException {
public SerialPort(@NonNull File device, int baudrate, int dataBits, int parity, int stopBits,
int flags) throws SecurityException, IOException {

this.device = device;
this.baudrate = baudrate;
this.dataBits = dataBits;
this.parity = parity;
this.stopBits = stopBits;
this.flags = flags;

/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
Expand Down Expand Up @@ -110,7 +126,7 @@ public SerialPort(File device, int baudrate, int dataBits, int parity, int stopB
* @throws SecurityException
* @throws IOException
*/
public SerialPort(File device, int baudrate) throws SecurityException, IOException {
public SerialPort(@NonNull File device, int baudrate) throws SecurityException, IOException {
this(device, baudrate, 8, 0, 1, 0);
}

Expand All @@ -125,26 +141,79 @@ public SerialPort(File device, int baudrate) throws SecurityException, IOExcepti
* @throws SecurityException
* @throws IOException
*/
public SerialPort(File device, int baudrate, int dataBits, int parity, int stopBits)
public SerialPort(@NonNull File device, int baudrate, int dataBits, int parity, int stopBits)
throws SecurityException, IOException {
this(device, baudrate, dataBits, parity, stopBits, 0);
}

// Getters and setters
@NonNull
public InputStream getInputStream() {
return mFileInputStream;
}

@NonNull
public OutputStream getOutputStream() {
return mFileOutputStream;
}

/** 串口设备文件 */
@NonNull
public File getDevice() {
return device;
}

/** 波特率 */
public int getBaudrate() {
return baudrate;
}

/** 数据位;默认8,可选值为5~8 */
public int getDataBits() {
return dataBits;
}

/** 奇偶校验;0:无校验位(NONE,默认);1:奇校验位(ODD);2:偶校验位(EVEN) */
public int getParity() {
return parity;
}

/** 停止位;默认1;1:1位停止位;2:2位停止位 */
public int getStopBits() {
return stopBits;
}

public int getFlags() {
return flags;
}

// JNI
private native FileDescriptor open(String absolutePath, int baudrate, int dataBits, int parity,
int stopBits, int flags);

public native void close();

/** 关闭流和串口,已经try-catch */
public void tryClose() {
try {
mFileInputStream.close();
} catch (IOException e) {
//e.printStackTrace();
}

try {
mFileOutputStream.close();
} catch (IOException e) {
//e.printStackTrace();
}

try {
close();
} catch (Exception e) {
//e.printStackTrace();
}
}

static {
System.loadLibrary("serial_port");
}
Expand Down

0 comments on commit f66b632

Please sign in to comment.