instruction.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package modbus
  2. import "server/utils"
  3. var (
  4. ReadDeviceId = []byte{0xFE, 0x04, 0x03, 0xEE, 0x00, 0x08, 0x85, 0xB2} //读设备id
  5. ReadDeviceIdAndDOAndDI = []byte{0xFE, 0x04, 0x03, 0xE8, 0x00, 0x14, 0x64, 0x7A} //读设备id、do、di
  6. OperationCommand []byte
  7. )
  8. func DeviceLoopSwitch(loop int, state int) (data []byte) {
  9. data = append(data, 0xFE)
  10. data = append(data, 0x05)
  11. data = append(data, 0x00)
  12. data = append(data, byte(loop-1))
  13. if state == 1 {
  14. data = append(data, 0xFF)
  15. } else {
  16. data = append(data, 0x00)
  17. }
  18. data = append(data, 0x00)
  19. crc16 := utils.CRC16(data)
  20. data = append(data, byte(crc16&0xFF))
  21. data = append(data, byte(crc16>>8))
  22. return
  23. }
  24. func DeviceSwitch(number int, state int) (data []byte) {
  25. data = append(data, 0xFE)
  26. data = append(data, 0x0F)
  27. data = append(data, 0x00)
  28. data = append(data, 0x00)
  29. data = append(data, 0x00)
  30. data = append(data, byte(number))
  31. data = append(data, 0x01)
  32. if state == 1 {
  33. data = append(data, 0xFF)
  34. } else {
  35. data = append(data, 0x00)
  36. }
  37. crc16 := utils.CRC16(data)
  38. data = append(data, byte(crc16&0xFF))
  39. data = append(data, byte(crc16>>8))
  40. return
  41. }
  42. func SolarEnergyData() (data []byte) {
  43. data = append(data, 0x11)
  44. data = append(data, 0x03)
  45. data = append(data, 0x00)
  46. data = append(data, 0x04)
  47. data = append(data, 0x00)
  48. data = append(data, 0x1b)
  49. crc16 := utils.CRC16(data)
  50. data = append(data, byte(crc16&0xFF))
  51. data = append(data, byte(crc16>>8))
  52. return
  53. }
  54. // UpdateRegisterAddress 修改寄存器地址
  55. func UpdateRegisterAddress() (data []byte) {
  56. data = append(data, 0x01)
  57. data = append(data, 0x10)
  58. data = append(data, 0x00)
  59. data = append(data, 0x00)
  60. data = append(data, 0x00)
  61. data = append(data, 0x01)
  62. data = append(data, 0x02)
  63. data = append(data, 0x00)
  64. data = append(data, 0x11)
  65. crc16 := utils.CRC16(data)
  66. data = append(data, byte(crc16&0xFF))
  67. data = append(data, byte(crc16>>8))
  68. return
  69. }
  70. func ReadDeviceInfo(number int) (data []byte) {
  71. data = append(data, 0xFE)
  72. data = append(data, 0x01)
  73. data = append(data, 0x00)
  74. data = append(data, 0x00)
  75. data = append(data, 0x00)
  76. data = append(data, byte(number))
  77. crc16 := utils.CRC16(data)
  78. data = append(data, byte(crc16&0xFF))
  79. data = append(data, byte(crc16>>8))
  80. return
  81. }