dlt645_0x33323435.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package protocol
  2. import (
  3. "encoding/hex"
  4. "github.com/gogf/gf/os/glog"
  5. "strconv"
  6. )
  7. type Dlt_0x33323435 struct {
  8. //接收表号
  9. DeviceAddress []byte
  10. //表号
  11. DeviceID string
  12. //当前A相电压
  13. VoltageA float64
  14. //当前B相电压
  15. VoltageB float64
  16. //当前C相电压
  17. VoltageC float64
  18. }
  19. func (e *Dlt_0x33323435) MsgID() MsgID {
  20. return Msgdlt_0x33323435
  21. }
  22. func (e *Dlt_0x33323435) Encode(ctx *PacketContext) ([]byte, error) {
  23. writer := NewWriter()
  24. // 接收符号
  25. writer.Write([]byte{0x68})
  26. writer.Write(ctx.GetReceiveAddress())
  27. writer.Write([]byte{0x68, 0x11, 0x04, 0x33, 0x32, 0x34, 0x35})
  28. //cs效验位
  29. var one byte
  30. for _, v := range writer.Bytes() {
  31. one += v
  32. }
  33. writer.WriteByte(one)
  34. // 功能码
  35. writer.WriteByte(0x16)
  36. return writer.Bytes(), nil
  37. }
  38. func (e *Dlt_0x33323435) Decode(ctx *PacketContext, dataByte []byte) (data Data, err error) {
  39. bytea := dataByte[1:7]
  40. for i, j := 0, len(bytea)-1; i < j; i, j = i+1, j-1 {
  41. bytea[i], bytea[j] = bytea[j], bytea[i]
  42. }
  43. e.DeviceID = hex.EncodeToString(bytea)
  44. //正向总电能每个字节-33,1-4,分别为,小数位,个位,百位,万位
  45. byteb := make([]byte, 6)
  46. for i := 0; i < 2; i++ {
  47. byteb[i] = dataByte[14+i] - 0x33
  48. }
  49. _ = e.stringToVoltageBlock(hex.EncodeToString(byteb))
  50. glog.Debugf("数据读取成功:表号:%s,当前A相电压:%2fV,B相电压:%2fV,C相电压:%2fV", e.DeviceID, e.VoltageA, e.VoltageB, e.VoltageC)
  51. data.AV = e.VoltageA
  52. data.BV = e.VoltageB
  53. data.CV = e.VoltageC
  54. data.DataType = IsVData
  55. return data, nil
  56. }
  57. func (e *Dlt_0x33323435) stringToVoltageBlock(s string) error {
  58. a0, _ := strconv.ParseFloat(s[0:2], 64)
  59. a1, _ := strconv.ParseFloat(s[2:4], 64)
  60. b0, _ := strconv.ParseFloat(s[4:6], 64)
  61. b1, _ := strconv.ParseFloat(s[6:8], 64)
  62. c0, _ := strconv.ParseFloat(s[8:10], 64)
  63. c1, _ := strconv.ParseFloat(s[10:12], 64)
  64. e.VoltageA = a0*0.1 + a1*10
  65. e.VoltageB = b0*0.1 + b1*10
  66. e.VoltageC = c0*0.1 + c1*10
  67. return nil
  68. }