Ingen beskrivning

hm11.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/python
  2. # Author: Zion Orent <zorent@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. import time, sys, signal, atexit
  24. import pyupm_hm11 as upmHm11
  25. # Instantiate a HM11 BLE Module on UART 0
  26. my_ble_obj = upmHm11.HM11(0)
  27. ## Exit handlers ##
  28. # This stops python from printing a stacktrace when you hit control-C
  29. def SIGINTHandler(signum, frame):
  30. raise SystemExit
  31. # This function lets you run code on exit,
  32. # including functions from my_ble_obj
  33. def exitHandler():
  34. print "Exiting"
  35. sys.exit(0)
  36. # Register exit handlers
  37. atexit.register(exitHandler)
  38. signal.signal(signal.SIGINT, SIGINTHandler)
  39. bufferLength = 256
  40. # make sure port is initialized properly. 9600 baud is the default.
  41. if (not my_ble_obj.setupTty(upmHm11.cvar.int_B9600)):
  42. print "Failed to setup tty port parameters"
  43. sys.exit(0)
  44. usageStr = ("Usage:\n"
  45. "Pass a commandline argument (any argument) to this program\n"
  46. "to query the radio configuration and output it. NOTE: the\n"
  47. "radio must be in CONFIG mode for this to work.\n\n"
  48. "Running this program without arguments will simply transmit\n"
  49. "'Hello World!' every second, and output any data received from\n"
  50. "another radio.\n\n")
  51. print usageStr
  52. # simple helper function to send a command and wait for a response
  53. def sendCommand(bleObj, cmd):
  54. bleBuffer = upmHm11.charArray(bufferLength)
  55. bleObj.writeData(cmd, len(cmd))
  56. # wait up to 1 second
  57. if (bleObj.dataAvailable(1000)):
  58. bleObj.readData(bleBuffer, bufferLength)
  59. bleData = ""
  60. # read only the number of characters
  61. # specified by myGPSSensor.readData
  62. for x in range(0, bufferLength):
  63. if (bleBuffer.__getitem__(x) == '\0'):
  64. break
  65. else:
  66. bleData += bleBuffer.__getitem__(x)
  67. print bleData
  68. else:
  69. print "Timed out waiting for response"
  70. if (len(sys.argv) > 1):
  71. print "Sending command line argument (" + sys.argv[1] + ")..."
  72. sendCommand(my_ble_obj, sys.argv[1])
  73. else:
  74. # query the module address
  75. addr = "AT+ADDR?";
  76. print "Querying module address (" + addr + ")..."
  77. sendCommand(my_ble_obj, addr)
  78. time.sleep(1)
  79. # query the module address
  80. pin = "AT+PASS?";
  81. print "Querying module PIN (" + pin + ")..."
  82. sendCommand(my_ble_obj, pin)
  83. # Other potentially useful commands are:
  84. #
  85. # AT+VERS? - query module version
  86. # AT+ROLE0 - set as slave
  87. # AT+ROLE1 - set as master
  88. # AT+CLEAR - clear all previous settings
  89. # AT+RESET - restart the device
  90. #
  91. # A comprehensive list is available from the datasheet at:
  92. # http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf