12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package protocol
- import (
- "encoding/hex"
- "fmt"
- "github.com/gogf/gf/os/glog"
- "strconv"
- )
- type Dlt_0x33323435 struct {
- //接收表号
- DeviceAddress []byte
- //表号
- DeviceID string
- //当前A相电压
- VoltageA float64
- //当前B相电压
- VoltageB float64
- //当前C相电压
- VoltageC float64
- }
- func (e *Dlt_0x33323435) MsgID() MsgID {
- return Msgdlt_0x33323435
- }
- func (e *Dlt_0x33323435) Encode(ctx *PacketContext) ([]byte, error) {
- writer := NewWriter()
- // 接收符号
- writer.Write([]byte{0x68})
- writer.Write(ctx.GetReceiveAddress())
- writer.Write([]byte{0x68, 0x11, 0x04, 0x33, 0x32, 0x34, 0x35})
- //cs效验位
- var one byte
- for _, v := range writer.Bytes() {
- one += v
- }
- writer.WriteByte(one)
- // 功能码
- writer.WriteByte(0x16)
- return writer.Bytes(), nil
- }
- func (e *Dlt_0x33323435) Decode(ctx *PacketContext, dataByte []byte) (data Data, err error) {
- bytea := dataByte[1:7]
- for i, j := 0, len(bytea)-1; i < j; i, j = i+1, j-1 {
- bytea[i], bytea[j] = bytea[j], bytea[i]
- }
- e.DeviceID = hex.EncodeToString(bytea)
- //正向总电能每个字节-33,1-4,分别为,小数位,个位,百位,万位
- byteb := make([]byte, 6)
- for i := 0; i < 6; i++ {
- byteb[i] = dataByte[14+i] - 0x33
- }
- _ = e.stringToVoltageBlock(hex.EncodeToString(byteb))
- glog.Debugf("数据读取成功:表号:%s,当前A相电压:%2fV,B相电压:%2fV,C相电压:%2fV", e.DeviceID, e.VoltageA, e.VoltageB, e.VoltageC)
- data.AV = e.VoltageA
- data.BV = e.VoltageB
- data.CV = e.VoltageC
- data.DataType = IsVData
- return data, nil
- }
- func (e *Dlt_0x33323435) stringToVoltageBlock(s string) error {
- a0, _ := strconv.ParseFloat(s[0:2], 32)
- a1, _ := strconv.ParseFloat(s[2:4], 32)
- b0, _ := strconv.ParseFloat(s[4:6], 32)
- b1, _ := strconv.ParseFloat(s[6:8], 32)
- c0, _ := strconv.ParseFloat(s[8:10], 32)
- c1, _ := strconv.ParseFloat(s[10:12], 32)
- e.VoltageA, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", a0/10+a1*10), 64)
- e.VoltageB, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", b0/10+b1*10), 64)
- e.VoltageC, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", c0/10+c1*10), 64)
- return nil
- }
|