func (m *mk2Ser) stateDecode(frame []byte) {
	m.info.ChargeState = m.applyScaleAndSign(frame[1:3], ramVarChargeState)
	logrus.Debugf("battery state decode %#v", m.info)
	m.updateReport()
}
Decode charge state of battery.
package mk2driver
import (
	"errors"
	"fmt"
	"io"
	"sync"
	"time"
	"github.com/sirupsen/logrus"
)
type scaling struct {
	scale     float64
	offset    float64
	signed    bool
	supported bool
}
const (
	ramVarVMains = iota
	ramVarIMains
	ramVarVInverter
	ramVarIInverter
	ramVarVBat
	ramVarIBat
	ramVarVBatRipple
	ramVarInverterPeriod
	ramVarMainPeriod
	ramVarIACLoad
	ramVarVirSwitchPos
	ramVarIgnACInState
	ramVarMultiFuncRelay
	ramVarChargeState
	ramVarInverterPower1
	ramVarInverterPower2
	ramVarOutPower
	ramVarMaxOffset = 14
)
const (
	infoFrameHeader = 0x20
	frameHeader     = 0xff
)
const (
	acL1InfoFrame  = 0x08
	dcInfoFrame    = 0x0C
	setTargetFrame = 0x41
	infoReqFrame   = 0x46
	ledFrame       = 0x4C
	vFrame         = 0x56
	winmonFrame    = 0x57
)
const (
	infoReqAddrDC   = 0x00
	infoReqAddrACL1 = 0x01
)
const (
	commandReadRAMVar    = 0x30
	commandGetRAMVarInfo = 0x36
	commandReadRAMResponse       = 0x85
	commandGetRAMVarInfoResponse = 0x8E
)
type mk2Ser struct {
	info       *Mk2Info
	p          io.ReadWriter
	scales     []scaling
	scaleCount int
	run        chan struct{}
	frameLock  bool
	infochan   chan *Mk2Info
	wg         sync.WaitGroup
}
func NewMk2Connection(dev io.ReadWriter) (Mk2, error) {
	mk2 := &mk2Ser{}
	mk2.p = dev
	mk2.info = &Mk2Info{}
	mk2.scaleCount = 0
	mk2.frameLock = false
	mk2.scales = make([]scaling, 0, ramVarMaxOffset)
	mk2.setTarget()
	mk2.run = make(chan struct{})
	mk2.infochan = make(chan *Mk2Info)
	mk2.wg.Add(1)
	go mk2.frameLocker()
	return mk2, nil
}
func (m *mk2Ser) frameLocker() {
	frame := make([]byte, 256)
	var frameLength byte
	for {
		select {
		case <-m.run:
			m.wg.Done()
			return
		default:
		}
		if m.frameLock {
			frameLength = m.readByte()
			frameLengthOffset := int(frameLength) + 1
			l, err := io.ReadFull(m.p, frame[:frameLengthOffset])
			if err != nil {
				m.addError(fmt.Errorf("Read Error: %v", err))
				m.frameLock = false
			} else if l != frameLengthOffset {
				m.addError(errors.New("Read Length Error"))
				m.frameLock = false
			} else {
				m.handleFrame(frameLength, frame[:frameLengthOffset])
			}
		} else {
			tmp := m.readByte()
			frameLengthOffset := int(frameLength)
			if tmp == frameHeader || tmp == infoFrameHeader {
				l, err := io.ReadFull(m.p, frame[:frameLengthOffset])
				if err != nil {
					m.addError(fmt.Errorf("Read Error: %v", err))
					time.Sleep(1 * time.Second)
				} else if l != frameLengthOffset {
					m.addError(errors.New("Read Length Error"))
				} else {
					if checkChecksum(frameLength, tmp, frame[:frameLengthOffset]) {
						m.frameLock = true
						logrus.Info("Locked")
					}
				}
			}
			frameLength = tmp
		}
	}
}
func (m *mk2Ser) Close() {
	close(m.run)
	m.wg.Wait()
}
func (m *mk2Ser) C() chan *Mk2Info {
	return m.infochan
}
func (m *mk2Ser) readByte() byte {
	buffer := make([]byte, 1)
	_, err := io.ReadFull(m.p, buffer)
	if err != nil {
		m.addError(fmt.Errorf("Read error: %v", err))
		return 0
	}
	return buffer[0]
}
func (m *mk2Ser) addError(err error) {
	if m.info.Errors == nil {
		m.info.Errors = make([]error, 0)
	}
	m.info.Errors = append(m.info.Errors, err)
	m.info.Valid = false
}
func (m *mk2Ser) updateReport() {
	m.info.Timestamp = time.Now()
	select {
	case m.infochan <- m.info:
	default:
	}
	m.info = &Mk2Info{}
}
func (m *mk2Ser) handleFrame(l byte, frame []byte) {
	logrus.Debugf("frame %#v", frame)
	if checkChecksum(l, frame[0], frame[1:]) {
		switch frame[0] {
		case frameHeader:
			switch frame[1] {
			case vFrame:
				m.versionDecode(frame[2:])
			case winmonFrame:
				switch frame[2] {
				case commandGetRAMVarInfoResponse:
					m.scaleDecode(frame[2:])
				case commandReadRAMResponse:
					m.stateDecode(frame[2:])
				}
			case ledFrame:
				m.ledDecode(frame[2:])
			}
		case infoFrameHeader:
			switch frame[5] {
			case dcInfoFrame:
				m.dcDecode(frame[1:])
			case acL1InfoFrame:
				m.acDecode(frame[1:])
			}
		}
	} else {
		logrus.Errorf("Invalid incoming frame checksum: %x", frame)
		m.frameLock = false
	}
}
func (m *mk2Ser) setTarget() {
	cmd := make([]byte, 3)
	cmd[0] = setTargetFrame
	cmd[1] = 0x01
	cmd[2] = 0x00
	m.sendCommand(cmd)
}
func (m *mk2Ser) reqScaleFactor(in byte) {
	cmd := make([]byte, 4)
	cmd[0] = winmonFrame
	cmd[1] = commandGetRAMVarInfo
	cmd[2] = in
	m.sendCommand(cmd)
}
func int16Abs(in int16) uint16 {
	if in < 0 {
		return uint16(-in)
	}
	return uint16(in)
}
func (m *mk2Ser) scaleDecode(frame []byte) {
	tmp := scaling{}
	logrus.Debugf("Scale frame(%d): 0x%x", len(frame), frame)
	if len(frame) < 6 {
		tmp.supported = false
		logrus.Warnf("Skiping scaling factors for: %d", m.scaleCount)
	} else {
		tmp.supported = true
		var scl int16
		var ofs int16
		if len(frame) == 6 {
			scl = int16(frame[2])<<8 + int16(frame[1])
			ofs = int16(uint16(frame[4])<<8 + uint16(frame[3]))
		} else {
			scl = int16(frame[2])<<8 + int16(frame[1])
			ofs = int16(uint16(frame[5])<<8 + uint16(frame[4]))
		}
		if scl < 0 {
			tmp.signed = true
		}
		tmp.offset = float64(ofs)
		scale := int16Abs(scl)
		if scale >= 0x4000 {
			tmp.scale = 1 / (0x8000 - float64(scale))
		} else {
			tmp.scale = float64(scale)
		}
	}
	logrus.Debugf("scalecount %v: %#v \n", m.scaleCount, tmp)
	m.scales = append(m.scales, tmp)
	m.scaleCount++
	if m.scaleCount < ramVarMaxOffset {
		m.reqScaleFactor(byte(m.scaleCount))
	} else {
		logrus.Info("Monitoring starting.")
	}
}
func (m *mk2Ser) versionDecode(frame []byte) {
	logrus.Debugf("versiondecode %v", frame)
	m.info.Version = 0
	m.info.Valid = true
	for i := 0; i < 4; i++ {
		m.info.Version += uint32(frame[i]) << uint(i) * 8
	}
	if m.scaleCount < ramVarMaxOffset {
		logrus.Info("Get scaling factors.")
		m.reqScaleFactor(byte(m.scaleCount))
	} else {
		cmd := make([]byte, 2)
		cmd[0] = infoReqFrame
		cmd[1] = infoReqAddrDC
		m.sendCommand(cmd)
	}
}
func (m *mk2Ser) applyScaleAndSign(data []byte, scale int) float64 {
	var value float64
	if !m.scales[scale].supported {
		return 0
	}
	if m.scales[scale].signed {
		value = getSigned(data)
	} else {
		value = getUnsigned16(data)
	}
	return m.applyScale(value, scale)
}
func (m *mk2Ser) applyScale(value float64, scale int) float64 {
	if !m.scales[scale].supported {
		return value
	}
	return m.scales[scale].scale * (value + m.scales[scale].offset)
}
func getSigned(data []byte) float64 {
	return float64(int16(data[0]) + int16(data[1])<<8)
}
func getUnsigned16(data []byte) float64 {
	return float64(uint16(data[0]) + uint16(data[1])<<8)
}
func getUnsigned(data []byte) float64 {
	return float64(uint32(data[0]) + uint32(data[1])<<8 + uint32(data[2])<<16)
}
func (m *mk2Ser) dcDecode(frame []byte) {
	m.info.BatVoltage = m.applyScaleAndSign(frame[5:7], ramVarVBat)
	usedC := m.applyScale(getUnsigned(frame[7:10]), ramVarIBat)
	chargeC := m.applyScale(getUnsigned(frame[10:13]), ramVarIBat)
	m.info.BatCurrent = usedC - chargeC
	m.info.OutFrequency = 10 / (m.applyScale(float64(frame[13]), ramVarInverterPeriod))
	logrus.Debugf("dcDecode %#v", m.info)
	cmd := make([]byte, 2)
	cmd[0] = infoReqFrame
	cmd[1] = infoReqAddrACL1
	m.sendCommand(cmd)
}
func (m *mk2Ser) acDecode(frame []byte) {
	m.info.InVoltage = m.applyScaleAndSign(frame[5:7], ramVarVMains)
	m.info.InCurrent = m.applyScaleAndSign(frame[7:9], ramVarIMains)
	m.info.OutVoltage = m.applyScaleAndSign(frame[9:11], ramVarVInverter)
	m.info.OutCurrent = m.applyScaleAndSign(frame[11:13], ramVarIInverter)
	if frame[13] == 0xff {
		m.info.InFrequency = 0
	} else {
		m.info.InFrequency = 10 / (m.applyScale(float64(frame[13]), ramVarMainPeriod))
	}
	logrus.Debugf("acDecode %#v", m.info)
	cmd := make([]byte, 1)
	cmd[0] = ledFrame
	m.sendCommand(cmd)
}