From 8e4e04c8750b4a574a7f9b2c83458e5cdab6d709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Mon, 25 Mar 2024 14:19:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B2=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/deploymentTargetDropDown.xml | 17 --- .../common/helper/BPASerialPort.java | 124 ++++++++++++++++++ 2 files changed, 124 insertions(+), 17 deletions(-) delete mode 100644 .idea/deploymentTargetDropDown.xml create mode 100644 app/src/main/java/com/bonait/bnframework/common/helper/BPASerialPort.java diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml deleted file mode 100644 index 84e3a2fe..00000000 --- a/.idea/deploymentTargetDropDown.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/java/com/bonait/bnframework/common/helper/BPASerialPort.java b/app/src/main/java/com/bonait/bnframework/common/helper/BPASerialPort.java new file mode 100644 index 00000000..c71842b5 --- /dev/null +++ b/app/src/main/java/com/bonait/bnframework/common/helper/BPASerialPort.java @@ -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"); + } +}