Skip to content

Commit

Permalink
fix(inputs.modbus): Check number of register for datatype (influxdata…
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored Jun 23, 2023
1 parent 577db89 commit c459d7b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
20 changes: 19 additions & 1 deletion plugins/inputs/modbus/configuration_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,30 @@ func (c *ConfigurationOriginal) validateFieldDefinitions(fieldDefs []fieldDefini
return fmt.Errorf("invalid byte order %q and address '%v' in %q - %q", item.ByteOrder, item.Address, registerType, item.Name)
}

// Check for the request size corresponding to the data-type
var requiredAddresses int
switch item.DataType {
case "INT8L", "INT8H", "UINT8L", "UINT8H", "UINT16", "INT16", "FLOAT16-IEEE":
requiredAddresses = 1
case "UINT32", "INT32", "FLOAT32-IEEE":
requiredAddresses = 2

case "UINT64", "INT64", "FLOAT64-IEEE":
requiredAddresses = 4
}
if requiredAddresses > 0 && len(item.Address) != requiredAddresses {
return fmt.Errorf(
"invalid address '%v' length '%v'in %q - %q, expecting %d entries for datatype",
item.Address, len(item.Address), registerType, item.Name, requiredAddresses,
)
}

// search duplicated
if len(item.Address) > len(removeDuplicates(item.Address)) {
return fmt.Errorf("duplicate address '%v' in %q - %q", item.Address, registerType, item.Name)
}
} else if len(item.Address) != 1 {
return fmt.Errorf("invalid address'%v' length'%v' in %q - %q", item.Address, len(item.Address), registerType, item.Name)
return fmt.Errorf("invalid address '%v' length '%v'in %q - %q", item.Address, len(item.Address), registerType, item.Name)
}
}
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid address
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[[inputs.modbus]]
name = "Device"
slave_id = 1
timeout = "1s"
controller = "tcp://localhost:502"

holding_registers = [
{ name = "data", byte_order = "DCBA", data_type = "UINT64", scale=0.01, address = [0, 1]}
]

0 comments on commit c459d7b

Please sign in to comment.