Skip to content

Commit ec91e52

Browse files
authored
Em2Go: work around current being reset (#17050)
1 parent c148080 commit ec91e52

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

charger/em2go.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
type Em2Go struct {
3535
log *util.Logger
3636
conn *modbus.Connection
37-
current int64
37+
current uint16
3838
workaround bool
3939
phases int
4040
}
@@ -99,7 +99,7 @@ func NewEm2Go(uri string, slaveID uint8) (api.Charger, error) {
9999
wb := &Em2Go{
100100
log: log,
101101
conn: conn,
102-
current: 6,
102+
current: 60,
103103
workaround: false,
104104
}
105105

@@ -177,30 +177,41 @@ func (wb *Em2Go) Enable(enable bool) error {
177177
}
178178

179179
// re-set 1p if required
180-
if wb.workaround && wb.phases == 1 && enable {
181-
binary.BigEndian.PutUint16(b, uint16(wb.phases))
182-
if _, err := wb.conn.WriteMultipleRegisters(em2GoRegPhases, 1, b); err != nil {
183-
return err
180+
if wb.workaround && enable {
181+
if wb.phases == 1 {
182+
binary.BigEndian.PutUint16(b, uint16(wb.phases))
183+
if _, err := wb.conn.WriteMultipleRegisters(em2GoRegPhases, 1, b); err != nil {
184+
return err
185+
}
184186
}
185187

186188
// send default current
187-
return wb.MaxCurrent(wb.current)
189+
return wb.setCurrent(wb.current)
188190
}
189191

190192
return nil
191193
}
192194

195+
func (wb *Em2Go) setCurrent(current uint16) error {
196+
b := make([]byte, 2)
197+
binary.BigEndian.PutUint16(b, current)
198+
199+
_, err := wb.conn.WriteMultipleRegisters(em2GoRegCurrentLimit, 1, b)
200+
return err
201+
}
202+
193203
// MaxCurrent implements the api.Charger interface
194204
func (wb *Em2Go) MaxCurrent(current int64) error {
195205
return wb.maxCurrentMillis(float64(current))
196206
}
197207

198208
// maxCurrentMillis implements the api.ChargerEx interface
199209
func (wb *Em2Go) maxCurrentMillis(current float64) error {
200-
b := make([]byte, 2)
201-
binary.BigEndian.PutUint16(b, uint16(10*current))
202-
203-
_, err := wb.conn.WriteMultipleRegisters(em2GoRegCurrentLimit, 1, b)
210+
curr := uint16(current * 10)
211+
err := wb.setCurrent(curr)
212+
if err == nil {
213+
wb.current = curr
214+
}
204215
return err
205216
}
206217

0 commit comments

Comments
 (0)