sendProtocal.js 3.7 KB

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