Ei kuvausta

grovegprs.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*jslint node:true, vars:true, bitwise:true, unparam:true */
  2. /*jshint unused:true */
  3. /*
  4. * Author: Jon Trulson <jtrulson@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. var sensorObj = require('jsupm_grovegprs');
  27. /************** Functions **************/
  28. function printUsage(progname)
  29. {
  30. var outputStr = "Usage: " + progname + " [AT command]\n\n" +
  31. "If an argument is supplied on the command line, that argument is\n" +
  32. "sent to the module and the response is printed out.\n\n" +
  33. "If no argument is used, then the manufacturer and the current\n" +
  34. "saved profiles are queried and the results printed out.\n\n"
  35. console.log(outputStr);
  36. }
  37. // simple helper function to send a command and wait for a response
  38. function sendCommand(sensor, cmd, callback)
  39. {
  40. // commands need to be terminated with a carriage return
  41. cmd += "\r";
  42. sensor.writeDataStr(cmd);
  43. // wait up to 1 second
  44. if (sensor.dataAvailable(1000))
  45. {
  46. console.log("Returned: " + sensor.readDataStr(1024));
  47. }
  48. else
  49. console.log("Timed out waiting for response");
  50. if (callback)
  51. callback();
  52. }
  53. /************** Main code **************/
  54. // Instantiate a GROVEGPRS Module on UART 0
  55. var sensor = new sensorObj.GroveGPRS(0);
  56. // Set the baud rate, 19200 baud is the default.
  57. if (sensor.setBaudRate(19200))
  58. {
  59. console.log("Failed to set baud rate");
  60. process.exit(0);
  61. }
  62. printUsage(process.argv[1]);
  63. // Note: in nodeJS, command-line argument 0 is "node".
  64. // Command-line argument 1 is "grovegprs.js"
  65. // If you have a third argument, then it's a command
  66. if (process.argv.length > 2)
  67. {
  68. console.log("Sending command line argument (" + process.argv[2] + ")...");
  69. sendCommand(sensor, process.argv[2]);
  70. }
  71. else
  72. {
  73. // sending this command as a synchronous callback ensures better timing
  74. var callbackFunc = function()
  75. {
  76. setTimeout(function()
  77. {
  78. // query the saved profiles
  79. console.log("Querying the saved profiles (AT&V)...");
  80. sendCommand(sensor, "AT&V");
  81. // A comprehensive list is available from the
  82. // datasheet at:
  83. // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf
  84. }, 1000);
  85. };
  86. // query the module manufacturer
  87. console.log("Querying module manufacturer (AT+CGMI)...");
  88. sendCommand(sensor, "AT+CGMI", callbackFunc);
  89. }
  90. /************** Exit code **************/
  91. process.on('SIGINT', function()
  92. {
  93. sensor = null;
  94. sensorObj.cleanUp();
  95. sensorObj = null;
  96. console.log("Exiting...");
  97. process.exit(0);
  98. });