instruction.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. func ReadDeviceInfo(number int) (data []byte) {
  55. data = append(data, 0xFE)
  56. data = append(data, 0x01)
  57. data = append(data, 0x00)
  58. data = append(data, 0x00)
  59. data = append(data, 0x00)
  60. data = append(data, byte(number))
  61. crc16 := utils.CRC16(data)
  62. data = append(data, byte(crc16&0xFF))
  63. data = append(data, byte(crc16>>8))
  64. return
  65. }