暂无描述

hm11.cxx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Author: Jon Trulson <jtrulson@ics.com>
  3. * Copyright (c) 2015 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <unistd.h>
  25. #include <iostream>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include "hm11.h"
  29. using namespace std;
  30. using namespace upm;
  31. void printUsage(char *progname)
  32. {
  33. cout << "Usage: " << progname << " [AT command]" << endl;
  34. cout << endl;
  35. cout << "If an argument is supplied on the command line, that argument is"
  36. << endl;
  37. cout << "sent to the module and the response is printed out." << endl;
  38. cout << endl;
  39. cout << "If no argument is used, then the address and PIN of the module"
  40. << endl;
  41. cout << "are queried and the results printed out." << endl;
  42. cout << endl;
  43. cout << endl;
  44. }
  45. // simple helper function to send a command and wait for a response
  46. void sendCommand(upm::HM11* ble, char *cmd)
  47. {
  48. char buffer[BUFSIZ];
  49. ble->writeData(cmd, strlen(cmd));
  50. // wait up to 1 second
  51. if (ble->dataAvailable(1000))
  52. {
  53. memset(buffer, 0, BUFSIZ);
  54. ble->readData(buffer, BUFSIZ);
  55. cout << "Returned: " << buffer << endl;
  56. }
  57. else
  58. {
  59. cerr << "Timed out waiting for response" << endl;
  60. }
  61. }
  62. int main (int argc, char **argv)
  63. {
  64. //! [Interesting]
  65. char buffer[BUFSIZ];
  66. // Instantiate a HM11 BLE Module on UART 0
  67. upm::HM11* ble = new upm::HM11(0);
  68. // make sure port is initialized properly. 9600 baud is the default.
  69. if (!ble->setupTty(B9600))
  70. {
  71. cerr << "Failed to setup tty port parameters" << endl;
  72. return 1;
  73. }
  74. printUsage(argv[0]);
  75. if (argc > 1)
  76. {
  77. cout << "Sending command line argument (" << argv[1] << ")..." << endl;
  78. sendCommand(ble, argv[1]);
  79. }
  80. else
  81. {
  82. // query the module address
  83. char addr[] = "AT+ADDR?";
  84. cout << "Querying module address (" << addr << ")..." << endl;
  85. sendCommand(ble, addr);
  86. sleep(1);
  87. // query the module address
  88. char pin[] = "AT+PASS?";
  89. cout << "Querying module PIN (" << pin << ")..." << endl;
  90. sendCommand(ble, pin);
  91. // Other potentially useful commands are:
  92. //
  93. // AT+VERS? - query module version
  94. // AT+ROLE0 - set as slave
  95. // AT+ROLE1 - set as master
  96. // AT+CLEAR - clear all previous settings
  97. // AT+RESET - restart the device
  98. //
  99. // A comprehensive list is available from the datasheet at:
  100. // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
  101. }
  102. //! [Interesting]
  103. delete ble;
  104. return 0;
  105. }