• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #include <unistd.h>
26 #include <iostream>
27 #include <signal.h>
28 #include <stdio.h>
29 #include "grovegprs.h"
30 
31 using namespace std;
32 using namespace upm;
33 
printUsage(char * progname)34 void printUsage(char *progname)
35 {
36   cout << "Usage: " << progname << " [AT command]" << endl;
37   cout << endl;
38 
39   cout << "If an argument is supplied on the command line, that argument is"
40        << endl;
41   cout << "sent to the module and the response is printed out." << endl;
42   cout << endl;
43   cout << "If no argument is used, then the manufacturer and the current"
44        << endl;
45   cout << "saved profiles are queried and the results printed out." << endl;
46   cout << endl;
47   cout << endl;
48 }
49 
50 // simple helper function to send a command and wait for a response
sendCommand(upm::GroveGPRS * sensor,string cmd)51 void sendCommand(upm::GroveGPRS* sensor, string cmd)
52 {
53   // commands need to be terminated with a carriage return
54   cmd += "\r";
55 
56   sensor->writeDataStr(cmd);
57 
58   // wait up to 1 second
59   if (sensor->dataAvailable(1000))
60     {
61       cout << "Returned: " << sensor->readDataStr(1024) << endl;
62     }
63   else
64     {
65       cerr << "Timed out waiting for response" << endl;
66     }
67 }
68 
69 
main(int argc,char ** argv)70 int main(int argc, char **argv)
71 {
72 //! [Interesting]
73 
74   // Instantiate a GroveGPRS Module on UART 0
75   upm::GroveGPRS* sensor = new upm::GroveGPRS(0);
76 
77   // Set the baud rate, 19200 baud is the default.
78   if (sensor->setBaudRate(19200) != mraa::SUCCESS)
79     {
80       cerr << "Failed to set tty baud rate" << endl;
81       return 1;
82     }
83 
84   printUsage(argv[0]);
85 
86   if (argc > 1)
87     {
88       cout << "Sending command line argument (" << argv[1] << ")..." << endl;
89       sendCommand(sensor, argv[1]);
90     }
91   else
92     {
93       // query the module manufacturer
94       cout << "Querying module manufacturer (AT+CGMI)..." << endl;
95       sendCommand(sensor, "AT+CGMI");
96 
97       sleep(1);
98 
99       // query the saved profiles
100       cout << "Querying the saved profiles (AT&V)..." << endl;
101       sendCommand(sensor, "AT&V");
102 
103       // A comprehensive list is available from the datasheet at:
104       // http://www.seeedstudio.com/wiki/images/7/72/AT_Commands_v1.11.pdf
105     }
106 
107 //! [Interesting]
108 
109   delete sensor;
110   return 0;
111 }
112