dlt645_0x33323435.go 2.0 KB

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