Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func meterHelp() string {
s += fmt.Sprintf("\n %s", "TCP")
s += fmt.Sprintf("\n %-10s%s", "SUNS", "Sunspec-compatible MODBUS TCP device (SMA, SolarEdge, KOSTAL, etc)")

s += fmt.Sprintf("\n %s", "UDP")
udpTypes := []string{"VM3P"}
for _, t := range udpTypes {
if p, ok := rs485.Producers[t]; ok {
s += fmt.Sprintf("\n %-10s%s", t, p().Description())
}
}

return s
}

Expand Down
21 changes: 13 additions & 8 deletions cmd/confighandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type InfluxConfig struct {
type AdapterConfig struct {
Device string
RTU bool
UDP bool
Baudrate int
Comset string
}
Expand Down Expand Up @@ -77,12 +78,16 @@ func NewDeviceConfigHandler() *DeviceConfigHandler {
return conf
}

// createConnection parses adapter string to create TCP or RTU connection
func createConnection(device string, rtu bool, baudrate int, comset string, timeout time.Duration) (res meters.Connection) {
// createConnection parses adapter string to create TCP, UDP or RTU connection
func createConnection(device string, rtu bool, udp bool, baudrate int, comset string, timeout time.Duration) (res meters.Connection) {
if device == "mock" {
res = meters.NewMock(device) // mocked connection
} else if tcp, _ := regexp.MatchString(":[0-9]+$", device); tcp {
if rtu {
if udp {
// special case: RTU over UDP
log.Printf("config: creating RTU over UDP connection for %s", device)
res = meters.NewRTUOverUDP(device) // udp connection
} else if rtu {
// special case: RTU over TCP
log.Printf("config: creating RTU over TCP connection for %s", device)
res = meters.NewRTUOverTCP(device) // tcp connection
Expand All @@ -106,10 +111,10 @@ func createConnection(device string, rtu bool, baudrate int, comset string, time
}

// ConnectionManager returns connection manager from cache or creates new connection wrapped by manager
func (conf *DeviceConfigHandler) ConnectionManager(connSpec string, rtu bool, baudrate int, comset string, timeout time.Duration) *meters.Manager {
func (conf *DeviceConfigHandler) ConnectionManager(connSpec string, rtu bool, udp bool, baudrate int, comset string, timeout time.Duration) *meters.Manager {
manager, ok := conf.Managers[connSpec]
if !ok {
conn := createConnection(connSpec, rtu, baudrate, comset, timeout)
conn := createConnection(connSpec, rtu, udp, baudrate, comset, timeout)
manager = meters.NewManager(conn)
conf.Managers[connSpec] = manager
}
Expand Down Expand Up @@ -222,9 +227,9 @@ func (conf *DeviceConfigHandler) CreateDeviceFromSpec(deviceDef string, timeout
log.Fatalf("Error parsing device id %s: %v. See -h for help.", devID, err)
}

// If this is an RTU over TCP device, a default RTU over TCP should already
// have been created of the --rtu flag was specified. We'll not re-check this here.
manager := conf.ConnectionManager(connSpec, false, 0, "", timeout)
// If this is an RTU over TCP/UDP device, a default connection should already
// have been created if the --rtu or --udp flag was specified. We'll not re-check this here.
manager := conf.ConnectionManager(connSpec, false, false, 0, "", timeout)

meter := conf.createDeviceForManager(manager, meterType, subdevice)
if err := manager.Add(uint8(id), meter); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func inspect(cmd *cobra.Command, args []string) {
defaultDevice := viper.GetString("adapter")
if defaultDevice != "" {
confHandler.DefaultDevice = defaultDevice
confHandler.ConnectionManager(defaultDevice, viper.GetBool("rtu"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
confHandler.ConnectionManager(defaultDevice, viper.GetBool("rtu"), viper.GetBool("udp"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
}

// create devices from command line
Expand Down
2 changes: 1 addition & 1 deletion cmd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func modbusClient() (meters.Connection, modbus.Client) {
}

// connection
conn := createConnection(adapter, viper.GetBool("rtu"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
conn := createConnection(adapter, viper.GetBool("rtu"), viper.GetBool("udp"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
client := conn.ModbusClient()

// raw log
Expand Down
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ Only applicable if the default adapter is an RTU device`,
`Use RTU over TCP for default adapter.
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection`,
)
rootCmd.PersistentFlags().Bool(
"udp",
false,
`Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address`,
)
rootCmd.PersistentFlags().BoolP(
"help", "h",
Expand Down
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func run(cmd *cobra.Command, args []string) {
defaultDevice := viper.GetString("adapter")
if defaultDevice != "" {
confHandler.DefaultDevice = defaultDevice
confHandler.ConnectionManager(defaultDevice, viper.GetBool("rtu"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
confHandler.ConnectionManager(defaultDevice, viper.GetBool("rtu"), viper.GetBool("udp"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
}

// create devices from command line
Expand All @@ -239,7 +239,7 @@ func run(cmd *cobra.Command, args []string) {
if len(devices) == 0 {
// add adapters from configuration
for _, a := range conf.Adapters {
confHandler.ConnectionManager(a.Device, a.RTU, a.Baudrate, a.Comset, viper.GetDuration("timeout"))
confHandler.ConnectionManager(a.Device, a.RTU, a.UDP, a.Baudrate, a.Comset, viper.GetDuration("timeout"))
}

// add devices from configuration
Expand Down
2 changes: 1 addition & 1 deletion cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func scan(cmd *cobra.Command, args []string) {
log.Fatal("missing adapter configuration")
}

conn := createConnection(adapter, viper.GetBool("rtu"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
conn := createConnection(adapter, viper.GetBool("rtu"), viper.GetBool("udp"), viper.GetInt("baudrate"), viper.GetString("comset"), viper.GetDuration("timeout"))
client := conn.ModbusClient()

// raw log
Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Easily read and distribute data from ModBus meters and grid inverters
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ See each sub-command's help for details on how to use the generated script.
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ mbmd completion bash
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ mbmd completion fish [flags]
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ mbmd completion powershell [flags]
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ mbmd completion zsh [flags]
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
6 changes: 6 additions & 0 deletions docs/mbmd_inspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ mbmd inspect [flags]
SDM72 Eastron SDM72
SDM72V2 Eastron SDM72 v2
SEMTR SolarEdge SE-MTR-3Y
VM3P Victron VM-3P75CT/VM-3P5A
WAGO87930 Wago 879-30XX
WS100 B+G e-tech WS100
X961A Eastron SMART X96-1A
TCP
SUNS Sunspec-compatible MODBUS TCP device (SMA, SolarEdge, KOSTAL, etc)
UDP
VM3P Victron VM-3P75CT/VM-3P5A
To use an adapter different from default, append RTU device or TCP address separated by @.
If the adapter is a TCP connection (identified by :port), the device type (SUNS) is ignored and
any type is considered valid.
Expand All @@ -75,6 +78,9 @@ mbmd inspect [flags]
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_read.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ mbmd read [flags] register length
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
6 changes: 6 additions & 0 deletions docs/mbmd_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ mbmd run [flags]
SDM72 Eastron SDM72
SDM72V2 Eastron SDM72 v2
SEMTR SolarEdge SE-MTR-3Y
VM3P Victron VM-3P75CT/VM-3P5A
WAGO87930 Wago 879-30XX
WS100 B+G e-tech WS100
X961A Eastron SMART X96-1A
TCP
SUNS Sunspec-compatible MODBUS TCP device (SMA, SolarEdge, KOSTAL, etc)
UDP
VM3P Victron VM-3P75CT/VM-3P5A
To use an adapter different from default, append RTU device or TCP address separated by @.
If the adapter is a TCP connection (identified by :port), the device type (SUNS) is ignored and
any type is considered valid.
Expand Down Expand Up @@ -86,6 +89,9 @@ mbmd run [flags]
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ mbmd scan [flags]
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ mbmd version [flags]
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
3 changes: 3 additions & 0 deletions docs/mbmd_write.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ mbmd write [flags] register length value
Typically used with RS485 to Ethernet adapters that don't perform protocol conversion (e.g. USR-TCP232).
Only applicable if the default adapter is a TCP connection
--timeout duration Timeout for MODBUS communication (default 300ms)
--udp Use RTU over UDP for default adapter.
Applicable for devices that use Modbus/UDP (e.g. Victron VM-3P75CT, VM-3P5A).
Only applicable if the default adapter is a UDP address
-v, --verbose Verbose mode
```

Expand Down
Loading