sendProtocal.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var machineInfo = require('./global')
  2. var checksum = require('./checksum')
  3. module.exports = {
  4. // 开始取零
  5. beginSetZero:function () {
  6. let data = [];
  7. data.push(0x04);
  8. data.push(0x00);
  9. data.push(0x02);
  10. data.push(0x00);
  11. return Buffer(data);
  12. },
  13. // 停止取零
  14. endSetZero:function () {
  15. let data = [];
  16. data.push(0x04);
  17. data.push(0x00);
  18. data.push(0x04);
  19. data.push(0x00);
  20. return Buffer(data);
  21. },
  22. // 开始采集
  23. startCollection:function () {
  24. let data = [];
  25. data.push(0x04);
  26. data.push(0x00);
  27. data.push(0x03);
  28. data.push(0x00);
  29. return Buffer(data);
  30. },
  31. // 停止采集
  32. endCollection:function () {
  33. let data = [];
  34. data.push(0x04);
  35. data.push(0x00);
  36. data.push(0x04);
  37. data.push(0x00);
  38. return Buffer(data);
  39. },
  40. // 复位
  41. resetDevice:function () {
  42. let data = [];
  43. data.push(0x16);
  44. data.push(0x00);
  45. return Buffer(data);
  46. },
  47. // 查询采样频率、传感器阈值 驱动电压、功能角色、设备型号指令
  48. sendMsg:function (arrData,cmd,type) {
  49. let head = 0xf5
  50. let realcmd = 0x00
  51. if(type == 'get') {
  52. realcmd = 0x01
  53. } else {
  54. realcmd = 0x02
  55. }
  56. let id = getWhichCmd(cmd)
  57. let data = [];
  58. data.push(head)
  59. // 计算长度
  60. let size = (1+1+arrData.length).toString(16)
  61. data.push(size)
  62. data.push(0x00)
  63. data.push(realcmd)
  64. data.push(id)
  65. let sum = 0
  66. for(let i in arrData) {
  67. data.push(parseInt(arrData[i],16))
  68. sum += parseInt(arrData[i],16)
  69. }
  70. let checkSum = (realcmd + id + sum) & 0xff
  71. data.push(checkSum)
  72. console.log(Buffer(data))
  73. return Buffer(data);
  74. },
  75. }
  76. function getWhichCmd(cmd) {
  77. let returnCmd = 0x00
  78. switch (cmd) {
  79. case 'caiyang':
  80. returnCmd = 0x01
  81. break;
  82. case 'chuangan':
  83. returnCmd = 0x02
  84. break;
  85. case 'qudong':
  86. returnCmd = 0x03;
  87. break;
  88. case 'gongneng':
  89. returnCmd = 0x04
  90. break;
  91. case 'shebei':
  92. returnCmd = 0x05
  93. break
  94. }
  95. return returnCmd
  96. }