dlt645_0x33323535.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package protocol
  2. import (
  3. "encoding/hex"
  4. "github.com/gogf/gf/os/glog"
  5. "strconv"
  6. )
  7. type Dlt_0x33323535 struct {
  8. //接收表号
  9. DeviceAddress []byte
  10. //表号
  11. DeviceID string
  12. // 当前A相电流
  13. CurrentA float64
  14. // 当前B相电流
  15. CurrentB float64
  16. // 当前C相电流
  17. CurrentC float64
  18. }
  19. func (e *Dlt_0x33323535) MsgID() MsgID {
  20. return Msgdlt_0x33323535
  21. }
  22. func (e *Dlt_0x33323535) 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, 0x35, 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_0x33323535) 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, 9)
  46. for i := 0; i < 2; i++ {
  47. byteb[i] = dataByte[14+i] - 0x33
  48. }
  49. _ = e.stringToCurrentBlock(hex.EncodeToString(byteb))
  50. glog.Debugf("数据读取成功:表号:%s,当前A相电流:%2fA,B相电流:%2fA,C相电流:%2fA", e.DeviceID, e.CurrentA, e.CurrentB, e.CurrentC)
  51. data.AI = e.CurrentA
  52. data.BI = e.CurrentB
  53. data.CI = e.CurrentC
  54. data.DataType = IsIData
  55. return data, nil
  56. }
  57. func (e *Dlt_0x33323535) stringToCurrentBlock(s string) error {
  58. a0, _ := strconv.ParseFloat(s[0:2], 64)
  59. a1, _ := strconv.ParseFloat(s[2:4], 64)
  60. a2, _ := strconv.ParseFloat(s[4:6], 64)
  61. b0, _ := strconv.ParseFloat(s[6:8], 64)
  62. b1, _ := strconv.ParseFloat(s[8:10], 64)
  63. b2, _ := strconv.ParseFloat(s[10:12], 64)
  64. c0, _ := strconv.ParseFloat(s[12:14], 64)
  65. c1, _ := strconv.ParseFloat(s[14:16], 64)
  66. c2, _ := strconv.ParseFloat(s[16:18], 64)
  67. e.CurrentA = a0*0.001 + a1*0.1 + a2*10
  68. e.CurrentB = b0*0.001 + b1*0.1 + b2*10
  69. e.CurrentC = c0*0.001 + c1*0.1 + c2*10
  70. return nil
  71. }