修改旧版设备秤数量导致数据显示不准的问题
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package com.shuwei.intelligent.shelves.serial
|
package com.shuwei.intelligent.shelves.serial
|
||||||
|
|
||||||
|
import com.shuwei.intelligent.shelves.App
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 串口通信协议常量定义
|
* 串口通信协议常量定义
|
||||||
* 统一管理所有协议头、协议尾、各类指令及帧解析偏移量
|
* 统一管理所有协议头、协议尾、各类指令及帧解析偏移量
|
||||||
@@ -63,7 +65,11 @@ object ProtocolConstants {
|
|||||||
const val DEVICE_STATUS_WEIGHT_OFFSET = HEADER.length + 14
|
const val DEVICE_STATUS_WEIGHT_OFFSET = HEADER.length + 14
|
||||||
|
|
||||||
/** 货架数量(每侧5格,共10格,协议中12组) */
|
/** 货架数量(每侧5格,共10格,协议中12组) */
|
||||||
const val SHELF_SENSOR_COUNT = 12
|
fun shelfSensorCount(): Int {
|
||||||
|
return if (device2Columns.contains(App.deviceId)) 12
|
||||||
|
else if (device3Columns.contains(App.deviceId)) 24
|
||||||
|
else 0
|
||||||
|
}
|
||||||
|
|
||||||
/** 每组重量数据字节数(十六进制字符数) */
|
/** 每组重量数据字节数(十六进制字符数) */
|
||||||
const val BYTES_PER_WEIGHT = 8
|
const val BYTES_PER_WEIGHT = 8
|
||||||
@@ -72,10 +78,18 @@ object ProtocolConstants {
|
|||||||
const val TEMPERATURE_FIELD_LEN = 2
|
const val TEMPERATURE_FIELD_LEN = 2
|
||||||
|
|
||||||
/** 心跳帧最小有效长度 */
|
/** 心跳帧最小有效长度 */
|
||||||
const val HEARTBEAT_MIN_LENGTH = 200
|
fun heartbeatMinLength(): Int {
|
||||||
|
return if (device2Columns.contains(App.deviceId)) 200
|
||||||
|
else if (device3Columns.contains(App.deviceId)) 296
|
||||||
|
else 0
|
||||||
|
}
|
||||||
|
|
||||||
/** 心跳帧中温度字段起始偏移(经协议分析得出,前面含重量+其他传感器数据) */
|
/** 心跳帧中温度字段起始偏移(经协议分析得出,前面含重量+其他传感器数据) */
|
||||||
const val HEARTBEAT_TEMPERATURE_START = 158
|
fun heartbeatTemperatureStart(): Int {
|
||||||
|
return if (device2Columns.contains(App.deviceId)) 158
|
||||||
|
else if (device3Columns.contains(App.deviceId)) 158+96
|
||||||
|
else 0
|
||||||
|
}
|
||||||
|
|
||||||
/** 激活请求体有效长度 */
|
/** 激活请求体有效长度 */
|
||||||
const val ACTIVATE_BODY_LENGTH = 44
|
const val ACTIVATE_BODY_LENGTH = 44
|
||||||
|
|||||||
@@ -20,10 +20,7 @@ import com.shuwei.intelligent.shelves.serial.ProtocolConstants.DEVICE_STATUS_WEI
|
|||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.FIX_BODY_LENGTH
|
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.FIX_BODY_LENGTH
|
||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.FOOTER
|
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.FOOTER
|
||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.HEADER
|
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.HEADER
|
||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.HEARTBEAT_MIN_LENGTH
|
|
||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.HEARTBEAT_TEMPERATURE_START
|
|
||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.HEARTBEAT_WEIGHT_OFFSET
|
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.HEARTBEAT_WEIGHT_OFFSET
|
||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.SHELF_SENSOR_COUNT
|
|
||||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.TEMPERATURE_FIELD_LEN
|
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.TEMPERATURE_FIELD_LEN
|
||||||
import com.shuwei.intelligent.shelves.utils.binaryToHex
|
import com.shuwei.intelligent.shelves.utils.binaryToHex
|
||||||
import com.shuwei.intelligent.shelves.utils.hexToBinary
|
import com.shuwei.intelligent.shelves.utils.hexToBinary
|
||||||
@@ -122,7 +119,7 @@ class SerialProtocolHandler(private val callback: Callback) {
|
|||||||
private fun handleDeviceStatus(data: String, cmdFlag: String) {
|
private fun handleDeviceStatus(data: String, cmdFlag: String) {
|
||||||
callback.onLog("收到[获取设备状态]${cmdFlag}指令:${data}")
|
callback.onLog("收到[获取设备状态]${cmdFlag}指令:${data}")
|
||||||
val weightStartIndex = DEVICE_STATUS_WEIGHT_OFFSET
|
val weightStartIndex = DEVICE_STATUS_WEIGHT_OFFSET
|
||||||
val weightEndIndex = weightStartIndex + SHELF_SENSOR_COUNT * BYTES_PER_WEIGHT
|
val weightEndIndex = weightStartIndex + ProtocolConstants.shelfSensorCount() * BYTES_PER_WEIGHT
|
||||||
val temperatureEndIndex = weightEndIndex + TEMPERATURE_FIELD_LEN
|
val temperatureEndIndex = weightEndIndex + TEMPERATURE_FIELD_LEN
|
||||||
try {
|
try {
|
||||||
parseWeightData(start = weightStartIndex, end = weightEndIndex, data = data)
|
parseWeightData(start = weightStartIndex, end = weightEndIndex, data = data)
|
||||||
@@ -178,11 +175,11 @@ class SerialProtocolHandler(private val callback: Callback) {
|
|||||||
*/
|
*/
|
||||||
private fun parseHeartbeatData(data: String) {
|
private fun parseHeartbeatData(data: String) {
|
||||||
runCatching {
|
runCatching {
|
||||||
if (data.length < HEARTBEAT_MIN_LENGTH) return
|
if (data.length < ProtocolConstants.heartbeatMinLength()) return
|
||||||
val weightStartIndex = HEARTBEAT_WEIGHT_OFFSET
|
val weightStartIndex = HEARTBEAT_WEIGHT_OFFSET
|
||||||
val weightEndIndex = weightStartIndex + SHELF_SENSOR_COUNT * BYTES_PER_WEIGHT
|
val weightEndIndex = weightStartIndex + ProtocolConstants.shelfSensorCount() * BYTES_PER_WEIGHT
|
||||||
parseWeightData(start = weightStartIndex, end = weightEndIndex, data = data)
|
parseWeightData(start = weightStartIndex, end = weightEndIndex, data = data)
|
||||||
val temperatureEndIndex = HEARTBEAT_TEMPERATURE_START + TEMPERATURE_FIELD_LEN
|
val temperatureEndIndex = ProtocolConstants.heartbeatTemperatureStart() + TEMPERATURE_FIELD_LEN
|
||||||
parseFixDataBody(data, temperatureEndIndex)
|
parseFixDataBody(data, temperatureEndIndex)
|
||||||
}.onFailure { it.printStackTrace() }
|
}.onFailure { it.printStackTrace() }
|
||||||
}
|
}
|
||||||
@@ -212,7 +209,11 @@ class SerialProtocolHandler(private val callback: Callback) {
|
|||||||
firstByteBinary = "0${firstByteBinary.substring(1)}"
|
firstByteBinary = "0${firstByteBinary.substring(1)}"
|
||||||
val firstByteHex = binaryToHex(firstByteBinary)
|
val firstByteHex = binaryToHex(firstByteBinary)
|
||||||
weightHex = firstByteHex + weightHex.substring(2)
|
weightHex = firstByteHex + weightHex.substring(2)
|
||||||
-1 * (weightHex.toLongOrNull(16)?.toInt() ?: 0)
|
//-1 * (weightHex.toLongOrNull(16)?.toInt() ?: 0)
|
||||||
|
// 直接用 Long 解析,然后减去 2^32 得到有符号值
|
||||||
|
val unsigned = weightHex.toLongOrNull(16) ?: 0L
|
||||||
|
val signed = if (unsigned >= 0x80000000L) unsigned - 0x100000000L else unsigned
|
||||||
|
signed.toInt()
|
||||||
}
|
}
|
||||||
callback.onWeightUpdated(index, realWeight)
|
callback.onWeightUpdated(index, realWeight)
|
||||||
count += BYTES_PER_WEIGHT
|
count += BYTES_PER_WEIGHT
|
||||||
|
|||||||
Reference in New Issue
Block a user