@@ -50,6 +50,7 @@ type InfluxConfig struct {
5050type AdapterConfig struct {
5151 Device string
5252 RTU bool
53+ UDP bool
5354 Baudrate int
5455 Comset string
5556}
@@ -77,12 +78,16 @@ func NewDeviceConfigHandler() *DeviceConfigHandler {
7778 return conf
7879}
7980
80- // createConnection parses adapter string to create TCP or RTU connection
81- func createConnection (device string , rtu bool , baudrate int , comset string , timeout time.Duration ) (res meters.Connection ) {
81+ // createConnection parses adapter string to create TCP, UDP or RTU connection
82+ func createConnection (device string , rtu bool , udp bool , baudrate int , comset string , timeout time.Duration ) (res meters.Connection ) {
8283 if device == "mock" {
8384 res = meters .NewMock (device ) // mocked connection
8485 } else if tcp , _ := regexp .MatchString (":[0-9]+$" , device ); tcp {
85- if rtu {
86+ if udp {
87+ // special case: RTU over UDP
88+ log .Printf ("config: creating RTU over UDP connection for %s" , device )
89+ res = meters .NewRTUOverUDP (device ) // udp connection
90+ } else if rtu {
8691 // special case: RTU over TCP
8792 log .Printf ("config: creating RTU over TCP connection for %s" , device )
8893 res = meters .NewRTUOverTCP (device ) // tcp connection
@@ -106,10 +111,10 @@ func createConnection(device string, rtu bool, baudrate int, comset string, time
106111}
107112
108113// ConnectionManager returns connection manager from cache or creates new connection wrapped by manager
109- func (conf * DeviceConfigHandler ) ConnectionManager (connSpec string , rtu bool , baudrate int , comset string , timeout time.Duration ) * meters.Manager {
114+ func (conf * DeviceConfigHandler ) ConnectionManager (connSpec string , rtu bool , udp bool , baudrate int , comset string , timeout time.Duration ) * meters.Manager {
110115 manager , ok := conf .Managers [connSpec ]
111116 if ! ok {
112- conn := createConnection (connSpec , rtu , baudrate , comset , timeout )
117+ conn := createConnection (connSpec , rtu , udp , baudrate , comset , timeout )
113118 manager = meters .NewManager (conn )
114119 conf .Managers [connSpec ] = manager
115120 }
@@ -222,9 +227,9 @@ func (conf *DeviceConfigHandler) CreateDeviceFromSpec(deviceDef string, timeout
222227 log .Fatalf ("Error parsing device id %s: %v. See -h for help." , devID , err )
223228 }
224229
225- // If this is an RTU over TCP device, a default RTU over TCP should already
226- // have been created of the --rtu flag was specified. We'll not re-check this here.
227- manager := conf .ConnectionManager (connSpec , false , 0 , "" , timeout )
230+ // If this is an RTU over TCP/UDP device, a default connection should already
231+ // have been created if the --rtu or --udp flag was specified. We'll not re-check this here.
232+ manager := conf .ConnectionManager (connSpec , false , false , 0 , "" , timeout )
228233
229234 meter := conf .createDeviceForManager (manager , meterType , subdevice )
230235 if err := manager .Add (uint8 (id ), meter ); err != nil {
0 commit comments