|
@@ -1,8 +1,10 @@
|
|
|
package com.example.whatever.modbus
|
|
package com.example.whatever.modbus
|
|
|
|
|
|
|
|
import com.example.whatever.extentions.crc16
|
|
import com.example.whatever.extentions.crc16
|
|
|
|
|
+import com.example.whatever.extentions.toHexStrings
|
|
|
import com.example.whatever.util.Executor
|
|
import com.example.whatever.util.Executor
|
|
|
import com.example.whatever.util.jvmSeconds
|
|
import com.example.whatever.util.jvmSeconds
|
|
|
|
|
+import com.example.whatever.util.log.LogUtil
|
|
|
import java.util.concurrent.LinkedBlockingQueue
|
|
import java.util.concurrent.LinkedBlockingQueue
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -124,7 +126,7 @@ class ModBusManager(
|
|
|
init {
|
|
init {
|
|
|
portManager.listen { res ->
|
|
portManager.listen { res ->
|
|
|
if (verbose) {
|
|
if (verbose) {
|
|
|
-// Logger.i("ModBus", "接收:${res.toHexString()}")
|
|
|
|
|
|
|
+ LogUtil.i("接收:${res.toHexStrings()}")
|
|
|
}
|
|
}
|
|
|
synchronized(lock) {
|
|
synchronized(lock) {
|
|
|
sending?.run {
|
|
sending?.run {
|
|
@@ -132,7 +134,7 @@ class ModBusManager(
|
|
|
done?.let { it(res) }
|
|
done?.let { it(res) }
|
|
|
sending = null
|
|
sending = null
|
|
|
} else {
|
|
} else {
|
|
|
-// Logger.i("ModBus", "响应: ${res.toHexString()}未匹配, running:${running}")
|
|
|
|
|
|
|
+ LogUtil.i("响应: ${res.toHexStrings()}未匹配, running:${running}")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -158,13 +160,14 @@ class ModBusManager(
|
|
|
if (portManager.send(req)) {
|
|
if (portManager.send(req)) {
|
|
|
afterSent()
|
|
afterSent()
|
|
|
if (verbose) {
|
|
if (verbose) {
|
|
|
-// Logger.i("ModBus", "发送:${req.toHexString()}")
|
|
|
|
|
|
|
+ LogUtil.i("发送:${req.toHexStrings()}")
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
// Tip.toast("无法与主控板通讯")
|
|
// Tip.toast("无法与主控板通讯")
|
|
|
|
|
+ LogUtil.w("无法与主控板通讯")
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
-// Logger.i("ModBus", "未响应: ${req.toHexString()}")
|
|
|
|
|
|
|
+ LogUtil.i("未响应: ${req.toHexStrings()}")
|
|
|
// 放弃处理,回调空数据
|
|
// 放弃处理,回调空数据
|
|
|
done?.let { it(byteArrayOf()) }
|
|
done?.let { it(byteArrayOf()) }
|
|
|
sending = null
|
|
sending = null
|
|
@@ -243,7 +246,9 @@ class ModBusManager(
|
|
|
done: ((res: List<ByteArray>) -> Unit)?,
|
|
done: ((res: List<ByteArray>) -> Unit)?,
|
|
|
resList: ArrayList<ByteArray>
|
|
resList: ArrayList<ByteArray>
|
|
|
) {
|
|
) {
|
|
|
- sendTo(index, frame) { res ->
|
|
|
|
|
|
|
+ // TODO 临时禁止重发
|
|
|
|
|
+ sendTo(index, frame, false) { res ->
|
|
|
|
|
+// sendTo(index, frame) { res ->
|
|
|
resList.add(res)
|
|
resList.add(res)
|
|
|
if (index >= slaveCount - 1) {
|
|
if (index >= slaveCount - 1) {
|
|
|
// 已经发送完
|
|
// 已经发送完
|
|
@@ -271,7 +276,7 @@ class ModBusManager(
|
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
|
) {
|
|
) {
|
|
|
if (slaveCount <= 0) {
|
|
if (slaveCount <= 0) {
|
|
|
-// Logger.i("ModBus", "sendTo($index), slaveCount为0, 返回空数据")
|
|
|
|
|
|
|
+ LogUtil.i("sendTo($index), slaveCount为0, 返回空数据")
|
|
|
done?.invoke(byteArrayOf())
|
|
done?.invoke(byteArrayOf())
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -395,7 +400,9 @@ class MBFrame(
|
|
|
*/
|
|
*/
|
|
|
fun compile(index: Int): ByteArray {
|
|
fun compile(index: Int): ByteArray {
|
|
|
val bytes = ByteArray(4 + data.size)
|
|
val bytes = ByteArray(4 + data.size)
|
|
|
- bytes[0] = (0x80 + index).toByte()
|
|
|
|
|
|
|
+ // TODO 从机开始地址0x01
|
|
|
|
|
+// bytes[0] = (0x80 + index).toByte()
|
|
|
|
|
+ bytes[0] = (1 + index).toByte()
|
|
|
bytes[1] = type
|
|
bytes[1] = type
|
|
|
for (i in data.indices) {
|
|
for (i in data.indices) {
|
|
|
bytes[2 + i] = data[i]
|
|
bytes[2 + i] = data[i]
|
|
@@ -404,6 +411,9 @@ class MBFrame(
|
|
|
bytes[bytes.size - 2] = crc16[0]
|
|
bytes[bytes.size - 2] = crc16[0]
|
|
|
bytes[bytes.size - 1] = crc16[1]
|
|
bytes[bytes.size - 1] = crc16[1]
|
|
|
return bytes
|
|
return bytes
|
|
|
|
|
+
|
|
|
|
|
+// val cmd = byteArrayOf((1 + index).toByte()) + byteArrayOf(type) + data
|
|
|
|
|
+// return cmd + cmd.crc16()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
companion object {
|
|
@@ -413,7 +423,7 @@ class MBFrame(
|
|
|
*/
|
|
*/
|
|
|
val READ_STATUS = MBFrame(
|
|
val READ_STATUS = MBFrame(
|
|
|
FRAME_TYPE_READ,
|
|
FRAME_TYPE_READ,
|
|
|
- byteArrayOf(0x00, 0x00, 0x00, 0x04)
|
|
|
|
|
|
|
+ byteArrayOf(0x00, 0x00, 0x00, 0x01)
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -485,22 +495,6 @@ class MBFrame(
|
|
|
byteArrayOf(0x00, 0x05, 0b11110111.toByte(), 0b00000000)
|
|
byteArrayOf(0x00, 0x05, 0b11110111.toByte(), 0b00000000)
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 关托盘
|
|
|
|
|
- */
|
|
|
|
|
- val WRITE_PALLET_OFF = MBFrame(
|
|
|
|
|
- FRAME_TYPE_WRITE,
|
|
|
|
|
- byteArrayOf(0x00, 0x10, 0b01111111.toByte(), 0b10000000.toByte())
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 开托盘
|
|
|
|
|
- */
|
|
|
|
|
- val WRITE_PALLET_ON = MBFrame(
|
|
|
|
|
- FRAME_TYPE_WRITE,
|
|
|
|
|
- byteArrayOf(0x00, 0x10, 0b10111111.toByte(), 0b01000000.toByte())
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 读取称重传感器原始读取值
|
|
* 读取称重传感器原始读取值
|
|
|
*/
|
|
*/
|