Modbus: normalise RFID tags

This commit is contained in:
andig 2022-12-07 18:53:31 +01:00
parent 20f423b434
commit 8e2331d8b7
5 changed files with 15 additions and 14 deletions

View file

@ -27,7 +27,6 @@ import (
"encoding/binary"
"fmt"
"math"
"strings"
"time"
"github.com/evcc-io/evcc/api"
@ -296,24 +295,22 @@ func (wb *BenderCC) currents() (float64, float64, float64, error) {
// identify implements the api.Identifier interface
func (wb *BenderCC) identify() (string, error) {
if !wb.legacy {
var id []byte
b, err := wb.conn.ReadHoldingRegisters(bendRegSmartVehicleDetected, 1)
if err == nil && binary.BigEndian.Uint16(b) != 0 {
id, err = wb.conn.ReadHoldingRegisters(bendRegEVCCID, 6)
b, err = wb.conn.ReadHoldingRegisters(bendRegEVCCID, 6)
}
if id := strings.TrimSpace(string(id)); id != "" || err != nil {
if id := bytesAsString(b); id != "" || err != nil {
return id, err
}
}
id, err := wb.conn.ReadHoldingRegisters(bendRegUserID, 10)
b, err := wb.conn.ReadHoldingRegisters(bendRegUserID, 10)
if err != nil {
return "", err
}
return strings.TrimSpace(string(id)), nil
return bytesAsString(b), nil
}
var _ api.Diagnosis = (*BenderCC)(nil)

View file

@ -222,7 +222,7 @@ func (wb *Dadapower) Identify() (string, error) {
return "", err
}
return string(u), nil
return bytesAsString(u), nil
}
var _ api.Diagnosis = (*Dadapower)(nil)

View file

@ -1,6 +1,7 @@
package charger
import (
"bytes"
"fmt"
"strings"
)
@ -46,3 +47,8 @@ func ensureChargerWithFeature[Charger, Feature any](
return "", *new(Feature), err
}
// bytesAsString normalises a string by stripping leading 0x00 and trimming white space
func bytesAsString(b []byte) string {
return strings.TrimSpace(string(bytes.TrimLeft(b, "\x00")))
}

View file

@ -20,7 +20,6 @@ package charger
import (
"encoding/binary"
"fmt"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
@ -218,12 +217,12 @@ func (wb *KSE) Currents() (float64, float64, float64, error) {
// Identify implements the api.Identifier interface
func (wb *KSE) identify() (string, error) {
id, err := wb.conn.ReadHoldingRegisters(kseRegNFCTransactionID, 4)
b, err := wb.conn.ReadHoldingRegisters(kseRegNFCTransactionID, 4)
if err != nil {
return "", err
}
return strings.TrimSpace(string(id)), nil
return bytesAsString(b), nil
}
var _ api.Diagnosis = (*KSE)(nil)

View file

@ -20,7 +20,6 @@ package charger
import (
"encoding/binary"
"fmt"
"strings"
"time"
"github.com/evcc-io/evcc/api"
@ -234,12 +233,12 @@ var _ api.Identifier = (*WebastoNext)(nil)
// Identify implements the api.Identifier interface
func (wb *WebastoNext) Identify() (string, error) {
id, err := wb.conn.ReadHoldingRegisters(tqRegUserID, 10)
b, err := wb.conn.ReadHoldingRegisters(tqRegUserID, 10)
if err != nil {
return "", err
}
return strings.TrimSpace(string(id)), nil
return bytesAsString(b), nil
}
var _ api.Diagnosis = (*WebastoNext)(nil)