123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
-
-
- #include <unistd.h>
- #include <iostream>
- #include <signal.h>
- #include <stdio.h>
- #include "hm11.h"
-
- using namespace std;
- using namespace upm;
-
- void printUsage(char *progname)
- {
- cout << "Usage: " << progname << " [AT command]" << endl;
- cout << endl;
-
- cout << "If an argument is supplied on the command line, that argument is"
- << endl;
- cout << "sent to the module and the response is printed out." << endl;
- cout << endl;
- cout << "If no argument is used, then the address and PIN of the module"
- << endl;
- cout << "are queried and the results printed out." << endl;
- cout << endl;
- cout << endl;
- }
-
-
- void sendCommand(upm::HM11* ble, char *cmd)
- {
- char buffer[BUFSIZ];
- ble->writeData(cmd, strlen(cmd));
-
-
- if (ble->dataAvailable(1000))
- {
- memset(buffer, 0, BUFSIZ);
-
- ble->readData(buffer, BUFSIZ);
- cout << "Returned: " << buffer << endl;
- }
- else
- {
- cerr << "Timed out waiting for response" << endl;
- }
- }
-
-
- int main (int argc, char **argv)
- {
-
- char buffer[BUFSIZ];
-
-
- upm::HM11* ble = new upm::HM11(0);
-
-
- if (!ble->setupTty(B9600))
- {
- cerr << "Failed to setup tty port parameters" << endl;
- return 1;
- }
-
- printUsage(argv[0]);
-
- if (argc > 1)
- {
- cout << "Sending command line argument (" << argv[1] << ")..." << endl;
- sendCommand(ble, argv[1]);
- }
- else
- {
-
- char addr[] = "AT+ADDR?";
- cout << "Querying module address (" << addr << ")..." << endl;
- sendCommand(ble, addr);
-
- sleep(1);
-
-
- char pin[] = "AT+PASS?";
- cout << "Querying module PIN (" << pin << ")..." << endl;
- sendCommand(ble, pin);
-
-
-
-
-
-
-
-
-
-
-
- }
-
-
-
- delete ble;
- return 0;
- }
|