暫無描述

hm11.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*jslint node:true, vars:true, bitwise:true, unparam:true */
  2. /*jshint unused:true */
  3. /*
  4. * Author: Zion Orent <zorent@ics.com>
  5. * Copyright (c) 2015 Intel Corporation.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. /************** Variables **************/
  27. // normal read/write mode
  28. var bufferLength = 256;
  29. var ble = require('jsupm_hm11');
  30. /************** Functions **************/
  31. function printUsage(progname)
  32. {
  33. var outputStr = "Usage: " + progname + " [AT command]\n\n" +
  34. "If an argument is supplied on the command line, that argument is\n" +
  35. "sent to the module and the response is printed out.\n\n" +
  36. "If no argument is used, then the address and PIN of the module\n" +
  37. "are queried and the results printed out.\n\n"
  38. console.log(outputStr);
  39. }
  40. // simple helper function to send a command and wait for a response
  41. function sendCommand(bleObj, cmd, callback)
  42. {
  43. var bleBuffer = new ble.charArray(bufferLength);
  44. bleObj.writeData(cmd, cmd.length);
  45. // wait up to 1 second
  46. if (bleObj.dataAvailable(1000))
  47. {
  48. bleObj.readData(bleBuffer, bufferLength);
  49. var bleData = "";
  50. // read only the number of characters
  51. // specified by myGPSSensor.readData
  52. for (var x = 0; x < bufferLength; x++)
  53. {
  54. if (bleBuffer.getitem(x) == '\0')
  55. break;
  56. else
  57. bleData += bleBuffer.getitem(x);
  58. }
  59. console.log(bleData);
  60. }
  61. else
  62. console.log("Timed out waiting for response");
  63. if (callback)
  64. callback();
  65. }
  66. /************** Main code **************/
  67. // Instantiate a HM11 BLE Module on UART 0
  68. var my_ble_obj = new ble.HM11(0);
  69. // make sure port is initialized properly. 9600 baud is the default.
  70. if (!my_ble_obj.setupTty(ble.int_B9600))
  71. {
  72. console.log("Failed to setup tty port parameters");
  73. process.exit(0);
  74. }
  75. printUsage(process.argv[1]);
  76. // Note: in nodeJS, command-line argument 0 is "node".
  77. // Command-line argument 1 is "hm11.js"
  78. // If you have a third argument, then it's a command for BLE
  79. if (process.argv.length > 2)
  80. {
  81. console.log("Sending command line argument (" + process.argv[2] + ")...");
  82. sendCommand(my_ble_obj, process.argv[2]);
  83. }
  84. else
  85. {
  86. // query the module address
  87. var addr = "AT+ADDR?";
  88. console.log("Querying module address (" + addr + ")...");
  89. // sending this command as a synchronous callback ensures better timing
  90. var callbackFunc = function()
  91. {
  92. setTimeout(function()
  93. {
  94. // query the module address
  95. var pin = "AT+PASS?";
  96. console.log("Querying module PIN (" + pin + ")...");
  97. sendCommand(my_ble_obj, pin);
  98. // Other potentially useful commands are:
  99. //
  100. // AT+VERS? - query module version
  101. // AT+ROLE0 - set as slave
  102. // AT+ROLE1 - set as master
  103. // AT+CLEAR - clear all previous settings
  104. // AT+RESET - restart the device
  105. //
  106. // A comprehensive list is available from the datasheet at:
  107. // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
  108. }, 1000);
  109. };
  110. sendCommand(my_ble_obj, addr, callbackFunc);
  111. }
  112. /************** Exit code **************/
  113. process.on('SIGINT', function()
  114. {
  115. my_ble_obj = null;
  116. ble.cleanUp();
  117. ble = null;
  118. console.log("Exiting...");
  119. process.exit(0);
  120. });