@@ -20,58 +20,88 @@ type Settings struct {
2020 RTU * bool // indicates RTU over TCP if true
2121}
2222
23- // Connection decorates a meters.Connection with slave id and error handling
23+ // Connection decorates a meters.Connection with transparent slave id and error handling
2424type Connection struct {
2525 slaveID uint8
26- meters.Connection
26+ conn meters.Connection
2727}
2828
2929func (mb * Connection ) handle (res []byte , err error ) ([]byte , error ) {
3030 if err != nil {
31- mb .Connection .Close ()
31+ mb .conn .Close ()
3232 }
3333 return res , err
3434}
3535
36- // Slave sets slave id
37- func (mb * Connection ) Slave ( ) {
38- mb .Connection . Slave ( mb . slaveID )
36+ // Logger sets logger implementation
37+ func (mb * Connection ) Logger ( logger meters. Logger ) {
38+ mb .conn . Logger ( logger )
3939}
4040
41- // ReadCoils decorates the mbmd meters.Connection with slave setting and error handling
41+ // ReadCoils wraps the underlying implementation
4242func (mb * Connection ) ReadCoils (address , quantity uint16 ) ([]byte , error ) {
43- mb .Connection .Slave (mb .slaveID )
44- return mb .handle (mb .Connection .ModbusClient ().ReadCoils (address , quantity ))
43+ mb .conn .Slave (mb .slaveID )
44+ return mb .handle (mb .conn .ModbusClient ().ReadCoils (address , quantity ))
4545}
4646
47- // WriteSingleCoil decorates the mbmd meters.Connection with slave setting and error handling
47+ // WriteSingleCoil wraps the underlying implementation
4848func (mb * Connection ) WriteSingleCoil (address , quantity uint16 ) ([]byte , error ) {
49- mb .Connection .Slave (mb .slaveID )
50- return mb .handle (mb .Connection .ModbusClient ().WriteSingleCoil (address , quantity ))
49+ mb .conn .Slave (mb .slaveID )
50+ return mb .handle (mb .conn .ModbusClient ().WriteSingleCoil (address , quantity ))
5151}
5252
53- // ReadInputRegisters decorates the mbmd meters.Connection with slave setting and error handling
53+ // ReadInputRegisters wraps the underlying implementation
5454func (mb * Connection ) ReadInputRegisters (address , quantity uint16 ) ([]byte , error ) {
55- mb .Connection .Slave (mb .slaveID )
56- return mb .handle (mb .Connection .ModbusClient ().ReadInputRegisters (address , quantity ))
55+ mb .conn .Slave (mb .slaveID )
56+ return mb .handle (mb .conn .ModbusClient ().ReadInputRegisters (address , quantity ))
5757}
5858
59- // ReadHoldingRegisters decorates the mbmd meters.Connection with slave setting and error handling
59+ // ReadHoldingRegisters wraps the underlying implementation
6060func (mb * Connection ) ReadHoldingRegisters (address , quantity uint16 ) ([]byte , error ) {
61- mb .Connection .Slave (mb .slaveID )
62- return mb .handle (mb .Connection .ModbusClient ().ReadHoldingRegisters (address , quantity ))
61+ mb .conn .Slave (mb .slaveID )
62+ return mb .handle (mb .conn .ModbusClient ().ReadHoldingRegisters (address , quantity ))
6363}
6464
65- // WriteSingleRegister decorates the mbmd meters.Connection with slave setting and error handling
65+ // WriteSingleRegister wraps the underlying implementation
6666func (mb * Connection ) WriteSingleRegister (address , value uint16 ) ([]byte , error ) {
67- mb .Connection .Slave (mb .slaveID )
68- return mb .handle (mb .Connection .ModbusClient ().WriteSingleRegister (address , value ))
67+ mb .conn .Slave (mb .slaveID )
68+ return mb .handle (mb .conn .ModbusClient ().WriteSingleRegister (address , value ))
6969}
7070
71- // WriteMultipleRegisters decorates the mbmd meters.Connection with slave setting and error handling
71+ // WriteMultipleRegisters wraps the underlying implementation
7272func (mb * Connection ) WriteMultipleRegisters (address , quantity uint16 , value []byte ) ([]byte , error ) {
73- mb .Connection .Slave (mb .slaveID )
74- return mb .handle (mb .Connection .ModbusClient ().WriteMultipleRegisters (address , quantity , value ))
73+ mb .conn .Slave (mb .slaveID )
74+ return mb .handle (mb .conn .ModbusClient ().WriteMultipleRegisters (address , quantity , value ))
75+ }
76+
77+ // ReadDiscreteInputs wraps the underlying implementation
78+ func (mb * Connection ) ReadDiscreteInputs (address , quantity uint16 ) (results []byte , err error ) {
79+ mb .conn .Slave (mb .slaveID )
80+ return mb .handle (mb .conn .ModbusClient ().ReadDiscreteInputs (address , quantity ))
81+ }
82+
83+ // WriteMultipleCoils wraps the underlying implementation
84+ func (mb * Connection ) WriteMultipleCoils (address , quantity uint16 , value []byte ) (results []byte , err error ) {
85+ mb .conn .Slave (mb .slaveID )
86+ return mb .handle (mb .conn .ModbusClient ().WriteMultipleCoils (address , quantity , value ))
87+ }
88+
89+ // ReadWriteMultipleRegisters wraps the underlying implementation
90+ func (mb * Connection ) ReadWriteMultipleRegisters (readAddress , readQuantity , writeAddress , writeQuantity uint16 , value []byte ) (results []byte , err error ) {
91+ mb .conn .Slave (mb .slaveID )
92+ return mb .handle (mb .conn .ModbusClient ().ReadWriteMultipleRegisters (readAddress , readQuantity , writeAddress , writeQuantity , value ))
93+ }
94+
95+ // MaskWriteRegister wraps the underlying implementation
96+ func (mb * Connection ) MaskWriteRegister (address , andMask , orMask uint16 ) (results []byte , err error ) {
97+ mb .conn .Slave (mb .slaveID )
98+ return mb .handle (mb .conn .ModbusClient ().MaskWriteRegister (address , andMask , orMask ))
99+ }
100+
101+ // ReadFIFOQueue wraps the underlying implementation
102+ func (mb * Connection ) ReadFIFOQueue (address uint16 ) (results []byte , err error ) {
103+ mb .conn .Slave (mb .slaveID )
104+ return mb .handle (mb .conn .ModbusClient ().ReadFIFOQueue (address ))
75105}
76106
77107var connections map [string ]meters.Connection
@@ -117,8 +147,8 @@ func NewConnection(uri, device, comset string, baudrate int, rtu bool, slaveID u
117147 }
118148
119149 slaveConn := & Connection {
120- slaveID : slaveID ,
121- Connection : conn ,
150+ slaveID : slaveID ,
151+ conn : conn ,
122152 }
123153
124154 return slaveConn , nil
0 commit comments