desc:数据标定命令封装

main
xiaowusky 1 year ago
parent e0b8deff5a
commit e6c5a02c50

@ -0,0 +1,55 @@
package com.yinuo.safetywatcher.watcher.port.cmd
/**
* 校准命令
* @param index 传感器编号 00 01 02 03
* @param target 目标值
*/
class CalibrationCmd(private val index: Int, private val target: Int) {
val ZERO = 0x87.toByte()
val SPAN = 0x88.toByte()
fun buildZeroCmd(): ByteArray {
return build(ZERO)
}
fun buildSPANCmd(): ByteArray {
return build(SPAN)
}
private fun build(cmd: Byte): ByteArray {
var highBit: Byte = 0x00
var lowBit: Byte = 0x00
if (index != 0) {
highBit = (target shr 8 and FF).toByte()
lowBit = (target and FF).toByte()
}
return byteArrayOf(
0xFF.toByte(),
index.toByte(),
cmd,
highBit,
lowBit,
0x00,
0x00,
0x00
).calibrationSum()
}
}
private fun ByteArray.calibrationSum(): ByteArray {
return this.plus(getCheckSum(this))
}
fun getCheckSum(packet: ByteArray): Byte {
var checksum = 0
for (i in packet.indices) {
if (i in 1..7) {
checksum += packet[i]
}
}
checksum = 0xffff - checksum
checksum += 1
return checksum.toByte()
}
Loading…
Cancel
Save