sendProtocal.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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)
  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. return Buffer(data);
  73. },
  74. // 查询当前模式
  75. searchModel:function () {
  76. let data = [];
  77. data.push(0x05);
  78. data.push(0x00);
  79. data.push(0x01);
  80. data.push(0x00);
  81. return Buffer(data);
  82. },
  83. // 进入配置模式
  84. inSetModel:function () {
  85. let data = [];
  86. data.push(0x05);
  87. data.push(0x00);
  88. data.push(0x02);
  89. data.push(0x00);
  90. return Buffer(data);
  91. },
  92. // 进入传输模式
  93. inTransModel:function () {
  94. let data = [];
  95. data.push(0x05);
  96. data.push(0x00);
  97. data.push(0x03);
  98. data.push(0x00);
  99. return Buffer(data);
  100. },
  101. // 蓝牙模块发送 查询配置指令
  102. blueToothSendCommand:function (arrData,cmd,type) {
  103. let head = 0xf7
  104. let realcmd = 0x00
  105. if(type == 'get') {
  106. realcmd = 0x01
  107. } else {
  108. realcmd = 0x02
  109. }
  110. let id = 0x00
  111. if(cmd == 'wifiAccount') {
  112. id = 0xa1
  113. } else if (cmd == 'wifiIp') {
  114. id = 0xa2
  115. } else if(cmd == 'serverIp') {
  116. id = 0xa3
  117. }
  118. let data = [];
  119. data.push(head)
  120. // 计算长度
  121. let size = (1+1+arrData.length)
  122. data.push(size)
  123. data.push(0x00)
  124. data.push(realcmd)
  125. data.push(id)
  126. let sum = 0
  127. for(let i in arrData) {
  128. data.push(parseInt(arrData[i],16))
  129. sum += parseInt(arrData[i],16)
  130. }
  131. let checkSum = (realcmd + id + sum) & 0xff
  132. data.push(checkSum)
  133. console.log(Buffer(data))
  134. return Buffer(data);
  135. }
  136. }
  137. function getWhichCmd(cmd) {
  138. let returnCmd = 0x00
  139. switch (cmd) {
  140. case 'caiyang':
  141. returnCmd = 0x01
  142. break;
  143. case 'chuangan':
  144. returnCmd = 0x02
  145. break;
  146. case 'qudong':
  147. returnCmd = 0x03;
  148. break;
  149. case 'gongneng':
  150. returnCmd = 0x04
  151. break;
  152. case 'shebei':
  153. returnCmd = 0x05
  154. break
  155. }
  156. return returnCmd
  157. }