|
@@ -3,6 +3,7 @@ package com.grkj.iscs.modbus
|
|
|
import android.content.Context
|
|
import android.content.Context
|
|
|
import androidx.annotation.WorkerThread
|
|
import androidx.annotation.WorkerThread
|
|
|
import com.epton.sdk.SerialPort
|
|
import com.epton.sdk.SerialPort
|
|
|
|
|
+import com.google.android.datatransport.runtime.dagger.Reusable
|
|
|
import com.grkj.iscs.util.Executor
|
|
import com.grkj.iscs.util.Executor
|
|
|
import com.grkj.iscs.util.SPUtils
|
|
import com.grkj.iscs.util.SPUtils
|
|
|
import com.grkj.iscs.util.ToastUtils
|
|
import com.grkj.iscs.util.ToastUtils
|
|
@@ -27,7 +28,7 @@ class PortManager private constructor(
|
|
|
/**
|
|
/**
|
|
|
* 向串口发送数据
|
|
* 向串口发送数据
|
|
|
*/
|
|
*/
|
|
|
- fun send(data: ByteArray) : Boolean {
|
|
|
|
|
|
|
+ fun send(data: ByteArray): Boolean {
|
|
|
try {
|
|
try {
|
|
|
output.write(data)
|
|
output.write(data)
|
|
|
output.flush()
|
|
output.flush()
|
|
@@ -38,7 +39,7 @@ class PortManager private constructor(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var t : Thread ? = null
|
|
|
|
|
|
|
+ var t: Thread? = null
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 监听串口数据
|
|
* 监听串口数据
|
|
@@ -75,7 +76,8 @@ class PortManager private constructor(
|
|
|
try {
|
|
try {
|
|
|
input.close()
|
|
input.close()
|
|
|
output.close()
|
|
output.close()
|
|
|
- } catch (e: Exception) { }
|
|
|
|
|
|
|
+ } catch (e: Exception) {
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
companion object {
|
|
@@ -88,7 +90,7 @@ class PortManager private constructor(
|
|
|
* @param bps 波特率,正整数
|
|
* @param bps 波特率,正整数
|
|
|
*/
|
|
*/
|
|
|
@WorkerThread
|
|
@WorkerThread
|
|
|
- fun open(port: Int, bps: Int, usb: Boolean) : PortManager? {
|
|
|
|
|
|
|
+ fun open(port: Int, bps: Int, usb: Boolean): PortManager? {
|
|
|
var blocked = true
|
|
var blocked = true
|
|
|
val thread = Thread.currentThread()
|
|
val thread = Thread.currentThread()
|
|
|
Executor.delayOnMain(500) {
|
|
Executor.delayOnMain(500) {
|
|
@@ -99,7 +101,7 @@ class PortManager private constructor(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- val file = File(if(usb) "/dev/ttyUSB${port}" else "/dev/ttyS${port}")
|
|
|
|
|
|
|
+ val file = File(if (usb) "/dev/ttyUSB${port}" else "/dev/ttyS${port}")
|
|
|
LogUtil.i("连接 port file")
|
|
LogUtil.i("连接 port file")
|
|
|
SerialPort(file, bps, 0).run {
|
|
SerialPort(file, bps, 0).run {
|
|
|
blocked = false
|
|
blocked = false
|
|
@@ -124,7 +126,10 @@ class PortManager private constructor(
|
|
|
* @param bps 波特率,正整数
|
|
* @param bps 波特率,正整数
|
|
|
*/
|
|
*/
|
|
|
@WorkerThread
|
|
@WorkerThread
|
|
|
- fun open(port: String?, bps: Int) : PortManager? {
|
|
|
|
|
|
|
+ fun open(port: String?, bps: Int): PortManager? {
|
|
|
|
|
+ if (port == null) {
|
|
|
|
|
+ return null
|
|
|
|
|
+ }
|
|
|
var blocked = true
|
|
var blocked = true
|
|
|
val thread = Thread.currentThread()
|
|
val thread = Thread.currentThread()
|
|
|
Executor.delayOnMain(500) {
|
|
Executor.delayOnMain(500) {
|
|
@@ -155,7 +160,7 @@ class PortManager private constructor(
|
|
|
* 打开主控板
|
|
* 打开主控板
|
|
|
*/
|
|
*/
|
|
|
@WorkerThread
|
|
@WorkerThread
|
|
|
- fun openCtrlBord(ctx: Context) : PortManager? {
|
|
|
|
|
|
|
+ fun openCtrlBord(ctx: Context): PortManager? {
|
|
|
// TODO 端口号待定:大屏调试设备-1,小屏调试设备-0
|
|
// TODO 端口号待定:大屏调试设备-1,小屏调试设备-0
|
|
|
// val port = 4
|
|
// val port = 4
|
|
|
// val bps = 115200
|
|
// val bps = 115200
|