Bläddra i källkod

串口

茶饮配方
pry 7 månader sedan
förälder
incheckning
8e4e04c875
2 ändrade filer med 124 tillägg och 17 borttagningar
  1. +0
    -17
      .idea/deploymentTargetDropDown.xml
  2. +124
    -0
      app/src/main/java/com/bonait/bnframework/common/helper/BPASerialPort.java

+ 0
- 17
.idea/deploymentTargetDropDown.xml Visa fil

@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="P5J1XA341F" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-03-23T08:35:23.875508600Z" />
</component>
</project>

+ 124
- 0
app/src/main/java/com/bonait/bnframework/common/helper/BPASerialPort.java Visa fil

@@ -0,0 +1,124 @@
package com.bonait.bnframework.common.helper;

import android.os.Build;
import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;

public class BPASerialPort {
private static final String TAG = "BPASerialPort";
private FileDescriptor mFd = null;
private FileInputStream mFileInputStream = null;
private FileOutputStream mFileOutputStream = null;
private FileLock lock = null;
public boolean isOpen;

public BPASerialPort(String path, int baudrate, int nBits, char nEvent, int nStop) {
File device = new File(path);
if (!device.canRead() || !device.canWrite()) {
try {
Process su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\nexit\n";
su.getOutputStream().write(cmd.getBytes());
if (su.waitFor() != 0 || !device.canRead() || !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception var11) {
var11.printStackTrace();
throw new SecurityException();
}
}

this.mFd = open(device.getAbsolutePath(), baudrate, nBits, nEvent, nStop);
if (this.mFd == null) {
Log.e("BPASerialPort", "打开串口异常");

try {
throw new IOException();
} catch (IOException var10) {
var10.printStackTrace();
}
} else {
this.mFileInputStream = new FileInputStream(this.mFd);
this.mFileOutputStream = new FileOutputStream(this.mFd);

try {
this.lock = this.mFileOutputStream.getChannel().tryLock();
if (this.lock == null) {
Log.e("BPASerialPort", "BPASerialPort: 串口被占用");
} else {
this.isOpen = true;
}
} catch (IOException var9) {
var9.printStackTrace();
}
}

}

public int write(byte[] data, int len) {
if (this.mFileOutputStream != null && data != null && len > 0) {
try {
this.mFileOutputStream.write(data, 0, len);
return 0;
} catch (IOException var4) {
Log.e("BPASerialPort", "write: 写入数据异常");
var4.printStackTrace();
return -1;
}
} else {
return -1;
}
}

public int read(byte[] data) {
if (this.mFileInputStream != null && data != null) {
try {
int len = this.mFileInputStream.read(data);
return len;
} catch (IOException var4) {
Log.e("SerialPort", "read: 读取数据异常");
var4.printStackTrace();
return -2;
}
} else {
return -2;
}
}

public void clearBuffer() {
try {
this.mFileInputStream.skip((long)this.mFileInputStream.available());
} catch (IOException var2) {
var2.printStackTrace();
}

}

public void Close() {
this.isOpen = false;

try {
if (Build.VERSION.SDK_INT >= 19 && this.lock != null) {
this.lock.close();
}
} catch (IOException var2) {
var2.printStackTrace();
}

this.close();
}

private static native FileDescriptor open(String var0, int var1, int var2, char var3, int var4);

private native void close();

static {
System.loadLibrary("COM");
}
}

Laddar…
Avbryt
Spara