适配”HR-DGWS-485“型温湿度传感器,支持修改温湿度、更改传感器地址

This commit is contained in:
BBIT-Kai
2026-07-15 14:20:33 +08:00
parent 8828612aa7
commit 26b47daac2
2 changed files with 23 additions and 8 deletions
+2 -2
View File
@@ -10,8 +10,8 @@ android {
applicationId "com.example.iot_controlhost" applicationId "com.example.iot_controlhost"
minSdkVersion 25 minSdkVersion 25
targetSdkVersion 30 targetSdkVersion 30
versionCode 159 versionCode 160
versionName "3.5.0.159" versionName "3.5.0.160"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk { ndk {
moduleName "mcu" moduleName "mcu"
@@ -1,6 +1,8 @@
package com.example.iot_controlhost.model.sensor package com.example.iot_controlhost.model.sensor
import com.blankj.utilcode.util.StringUtils import com.blankj.utilcode.util.StringUtils
import com.example.iot_controlhost.model.Device.getCRC
import com.example.iot_controlhost.model.sensor.Sensor.serialPortUtil
import com.example.iot_controlhost.utils.DataUtils import com.example.iot_controlhost.utils.DataUtils
@@ -25,7 +27,7 @@ class THSensor(name: String?) : Sensor(name) {
//获得指令 //获得指令
var str = address + "0300000002" var str = address + "0300000002"
val crc = getCRC(str) val crc = getCRC(str)
str = str + crc str += crc
//利用指令获得数据 //利用指令获得数据
val data = serialPortUtil.sendAndReceiveSerialData( val data = serialPortUtil.sendAndReceiveSerialData(
this, str this, str
@@ -41,8 +43,14 @@ class THSensor(name: String?) : Sensor(name) {
initData() initData()
} }
if (DataUtils.CRC16Check(wsd)) { if (DataUtils.CRC16Check(wsd)) {
wd = WDTransform(wsd[3] + wsd[4]) if (model == models[0]) {
sd = WDTransform(wsd[5] + wsd[6]) wd = WDTransform(wsd[3] + wsd[4])
sd = WDTransform(wsd[5] + wsd[6])
} else if (model == models[1]) {
// HR-DGWS-485 与 TH10S-B 命令基本一致,只是温湿度解析位置不同
wd = WDTransform(wsd[5] + wsd[6])
sd = WDTransform(wsd[3] + wsd[4])
}
} }
if (wd > 100 || sd > 100) { if (wd > 100 || sd > 100) {
initData() initData()
@@ -60,7 +68,7 @@ class THSensor(name: String?) : Sensor(name) {
} }
override fun getModels(): Array<String> { override fun getModels(): Array<String> {
return arrayOf("TH10S-B") return arrayOf("TH10S-B", "HR-DGWS-485")
} }
override fun toString(): String { override fun toString(): String {
@@ -81,8 +89,15 @@ class THSensor(name: String?) : Sensor(name) {
newAddress: String newAddress: String
): Boolean { ): Boolean {
//修改地址指令 //修改地址指令
var editAddress = oldAddress + "06006400" + newAddress var editAddress = if (model == models[0]) {
editAddress = editAddress + getCRC(editAddress) oldAddress + "06006400" + newAddress
} else if (model == models[1]) {
oldAddress + "06010000" + newAddress
} else {
// 不会出现这种
oldAddress
}
editAddress += getCRC(editAddress)
return editAddress == serialPortUtil.sendAndReceiveSerialData( return editAddress == serialPortUtil.sendAndReceiveSerialData(
this, editAddress this, editAddress
) )