Skip to content

Commit 8ba0cfa

Browse files
committed
debug and fix write bitflags, also fix offset bitflag reading
1 parent 5af8eae commit 8ba0cfa

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

classes/protocol_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ def process_register_ushort(self, registry : dict[int, int], entry : registry_ma
10491049
for i in range(start_bit, end): # Iterate over each bit position (0 to 15)
10501050
# Check if the i-th bit is set
10511051
if (val >> i) & 1:
1052-
flag_index = "b"+str(i+offset)
1052+
flag_index = "b"+str(i+offset-start_bit)
10531053
if flag_index in self.codes[entry.documented_name+"_codes"]:
10541054
flags.append(self.codes[entry.documented_name+"_codes"][flag_index])
10551055

classes/transports/modbus_base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ def evaluate_score(entry : registry_map_entry, val):
377377
def write_variable(self, entry : registry_map_entry, value : str, registry_type : Registry_Type = Registry_Type.HOLDING):
378378
""" writes a value to a ModBus register; todo: registry_type to handle other write functions"""
379379

380+
value = value.strip()
380381

381382
temp_map = [entry]
382383
ranges = self.protocolSettings.calculate_registry_ranges(temp_map, self.protocolSettings.registry_map_size[registry_type], init=True) #init=True to bypass timechecks
@@ -443,10 +444,10 @@ def write_variable(self, entry : registry_map_entry, value : str, registry_type
443444
#16 bit flags
444445
flag_size : int = Data_Type.getSize(entry.data_type)
445446

446-
if re.match(r"^[0-1]{"+flag_size+"}$", current_value): #bitflag string
447+
if re.match(rf"^[0-1]{{{flag_size}}}$", value): #bitflag string
447448
#is string of 01... s
448449
# Convert binary string to an integer
449-
value = int(current_value, 2)
450+
value = int(value[::-1], 2) #reverse string
450451

451452
# Ensure it fits within ushort range (0-65535)
452453
if value > 65535:
@@ -456,7 +457,7 @@ def write_variable(self, entry : registry_map_entry, value : str, registry_type
456457

457458
#apply bitmasks
458459
bit_index = entry.register_bit
459-
bit_mask = ((1 << bit_size) - 1) << bit_index # Create a mask for extracting X bits starting from bit_index
460+
bit_mask = ((1 << flag_size) - 1) << bit_index # Create a mask for extracting X bits starting from bit_index
460461
clear_mask = ~(bit_mask) # Mask for clearing the bits to be updated
461462

462463
# Clear the bits to be updated in the current_value

protocols/test.holding_registry_map.csv

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ variable name,data type,writable,register,documented name,description,values,uni
1515
,8bit,W,12.b8,Time_Month,,1-12,,
1616
,8bit,W,13,Time_Date,,1-31,,
1717
,8bit,W,13.b8,Time_Hour,,0-23,,
18-
,8bit,W,14,Time_Minute,,0-59,,
18+
,8bit,W,14,Time_Minute,,0-59,,
1919
,8bit,W,14.b8,Time_Second,,0-59,,
2020
,,W,21,decimal,,0-59,0.1,
21-
,8bit_flags,W,22.b8,bit_flags,,"{""b0"": ""bit 0"", ""b1"": ""bit 1"",""b2"": ""bit 2"",""b3"": ""bit 3"", ""b4"": ""bit 4"",""b5"": ""bit 5"", ""b6"": ""bit 6"", ""b7"": ""bit 7""}",,
21+
,8bit_flags,W,22,bit_flags1,,"{""b0"": ""bit 0"", ""b1"": ""bit 1"",""b2"": ""bit 2"",""b3"": ""bit 3"", ""b4"": ""bit 4"",""b5"": ""bit 5"", ""b6"": ""bit 6"", ""b7"": ""bit 7""}",,
22+
,8bit_flags,W,22.b8,bit_flags2,,"{""b0"": ""bit 0"", ""b1"": ""bit 1"",""b2"": ""bit 2"",""b3"": ""bit 3"", ""b4"": ""bit 4"",""b5"": ""bit 5"", ""b6"": ""bit 6"", ""b7"": ""bit 7""}",,

0 commit comments

Comments
 (0)