Skip to content

Commit

Permalink
Add r4dcb08 modbus helper support
Browse files Browse the repository at this point in the history
  • Loading branch information
pszafer committed Sep 25, 2024
1 parent 28a421e commit 4059607
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 86 deletions.
12 changes: 6 additions & 6 deletions boneio/bonecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ def get_arguments() -> argparse.Namespace:
modbus_parser.add_argument(
"--bytesize",
type=int,
required=True,
required=False,
default=8,
help="Bytesize",
)

modbus_parser.add_argument(
"--stopbits",
type=int,
required=True,
required=False,
default=1,
help="stopbits",
)
Expand All @@ -131,7 +131,7 @@ def get_arguments() -> argparse.Namespace:
type=str,
choices=["P", "E", "N"],
default="N",
required=True,
required=False,
help="Parity",
)

Expand All @@ -142,7 +142,7 @@ def get_arguments() -> argparse.Namespace:
set_modbus_parser.add_argument(
"--device",
type=str,
choices=["cwt"],
choices=["cwt", "r4dcb08"],
help="Choose device to set modbus address/baudrate",
required=True,
)
Expand All @@ -156,8 +156,8 @@ def get_arguments() -> argparse.Namespace:
set_modbus_parser_group.add_argument(
"--new-baudrate",
type=int,
choices=[2400, 4800, 9600, 19200],
help="Choose new baudrate to set",
choices=[1200, 2400, 4800, 9600, 19200],
help="Choose new baudrate to set. CWT doesn't work on 1200.",
)

get_modbus_parser = modbus_sub_parser.add_parser("get")
Expand Down
5 changes: 4 additions & 1 deletion boneio/modbus/cwt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"model": "CWT Temp and Humidity sensor",
"set_base": {
"set_address_address": 2000,
"set_baudrate_address": 2001
"set_baudrate": {
"address": 2001,
"possible_baudrates": [2400, 4800, 9600, 19200]
}
},
"registers_base": [
{
Expand Down
20 changes: 14 additions & 6 deletions boneio/modbus/modbuscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
_LOGGER = logging.getLogger(__name__)
SET_BASE = "set_base"
SET_ADDRESS = "set_address_address"
SET_BAUDRATE = "set_baudrate_address"
SET_BAUDRATE = "set_baudrate"


class ModbusHelper:
Expand Down Expand Up @@ -76,20 +76,23 @@ async def check_connection(self) -> bool:
return False
return True

def set_connection_speed(self, new_baudrate: int):
ind = self.baud_rates.index(new_baudrate)
def set_connection_speed(self, new_baudrate: int) -> int:
baudrate_model = self._model[SET_BAUDRATE]
ind = baudrate_model["possible_baudrates"].index(new_baudrate)
if ind:
result = self._modbus.client.write_register(
address=self._model[SET_BAUDRATE],
address=baudrate_model["address"],
value=ind,
unit=self._device_address,
)
if result.isError():
_LOGGER.error("Operation failed.")
return 1
else:
_LOGGER.info(
"Operation succeeded. Now restart device by disconnecting it."
)
return 0

def set_new_address(self, new_address: int):
if 0 < new_address < 253:
Expand Down Expand Up @@ -128,6 +131,9 @@ async def async_run_modbus_set(
if not first_reg_base:
return False
first_record = first_reg_base.get(REGISTERS, [])[0]
_LOGGER.debug(
f"Connecting with params uart: {uart}, baudrate: {baudrate}, stopbits: {stopbits}, bytesize: {bytesize}, parity: {parity}."
)
modbus_helper = ModbusHelper(
device=device,
uart=uart,
Expand All @@ -144,8 +150,7 @@ async def async_run_modbus_set(
_LOGGER.error("Can't connect with sensor. Exiting")
return 1
if new_baudrate:
modbus_helper.set_connection_speed(new_baudrate=new_baudrate)
return 0
return modbus_helper.set_connection_speed(new_baudrate=new_baudrate)
if new_address:
modbus_helper.set_new_address(new_address=new_address)
return 0
Expand All @@ -164,6 +169,9 @@ async def async_run_modbus_get(
parity: str = "N",
):
"""Run Modbus Get Function."""
_LOGGER.debug(
f"Connecting with params uart: {uart}, baudrate: {baudrate}, stopbits: {stopbits}, bytesize: {bytesize}, parity: {parity}."
)
_modbus = Modbus(
uart=UARTS[uart],
baudrate=baudrate,
Expand Down
159 changes: 87 additions & 72 deletions boneio/modbus/r4dcb08.json
Original file line number Diff line number Diff line change
@@ -1,76 +1,91 @@
{
"model": "R4DCB08",
"registers_base": [
"model": "R4DCB08",
"set_base": {
"set_address_address": 254,
"set_baudrate": {
"address": 255,
"possible_baudrates": [1200, 2400, 4800, 9600, 19200]
}
},
"registers_base": [
{
"base": 0,
"length": 16,
"register_type": "holding",
"registers": [
{
"base": 0,
"length": 16,
"register_type": "holding",
"registers": [
{
"name": "Temperature 1",
"address": 0,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
},
{
"name": "Temperature 2",
"address": 1,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
},
{
"name": "Temperature 3",
"address": 2,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
},
{
"name": "Temperature 4",
"address": 3,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
},
{
"name": "Temperature 5",
"address": 4,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
},
{
"name": "Temperature 6",
"address": 5,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
},
{
"name": "Temperature 7",
"address": 6,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
},
{
"name": "Temperature 8",
"address": 7,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"return_type": "multiply0_1"
}
]
"name": "Temperature 1",
"address": 0,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
},
{
"name": "Temperature 2",
"address": 1,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
},
{
"name": "Temperature 3",
"address": 2,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
},
{
"name": "Temperature 4",
"address": 3,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
},
{
"name": "Temperature 5",
"address": 4,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
},
{
"name": "Temperature 6",
"address": 5,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
},
{
"name": "Temperature 7",
"address": 6,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
},
{
"name": "Temperature 8",
"address": 7,
"unit_of_measurement": "°C",
"state_class": "measurement",
"device_class": "temperature",
"value_type": "S_WORD",
"filters": [{ "multiply": 0.1 }]
}
]
]
}
]
}
2 changes: 1 addition & 1 deletion boneio/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# flake8: noqa
__version__ = "0.8.0dev3"
__version__ = "0.8.0dev5"

0 comments on commit 4059607

Please sign in to comment.