sendsms.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include "SerialPort.h"
00003 #include "pdu.h"
00004 
00005 using namespace std;
00006 using std::string;
00007 using namespace SerialPorts;
00008 
00009 int main (int argc, char *argv[], char *envp[]) {
00010 
00011   //string pduString = "0011000C912394986343F50000AA08E8329BFD563753";
00012   string resultingPDU;
00013   pdu MyPDU;
00014   char *buf_message = argv[2];
00015   char *buf_telnr = argv[1];
00016 
00017 
00018 
00019 
00020 
00021   cout << "-------------------------------------------" << endl;
00022   cout << "Get and print arguments:" << endl;
00023 
00024   if ( argc != 3 ) {
00025     cout << endl;
00026     cout << "USAGE:   Please provide exact 2 arguments." << endl;
00027     cout << "         The 1st argument should be an internationan cell phone number." << endl;
00028     cout << "         The 2nd argument should be you SMS message." << endl;
00029     cout << endl;
00030     cout << "EXAMPLE: sendsms +32478306445 \'hello there!\'" << endl;
00031     return 1;
00032   }
00033 
00034   cout << "Number of arguments: " << argc - 1 << endl;
00035   cout << "Telephone: " << buf_telnr << endl;
00036   cout << "Message: " << buf_message << endl;
00037 
00038 
00039 
00040 
00041 
00042   cout << "-------------------------------------------" << endl;
00043   cout << "PDU API test-runs:" << endl;
00044   
00045   // Decoding test 
00046   //MyPDU.parsePDU(pduString);
00047   //cout << MyPDU.getText();
00048   
00049   //cout << MyPDU.setText("hellohello") << endl;
00050   cout << MyPDU.setText(buf_message) << endl;
00051   //cout << MyPDU.setSender("+32478306445") << endl;
00052   cout << MyPDU.setSender(buf_telnr) << endl;
00053   resultingPDU = MyPDU.encodePDU();
00054   //make pdu lenght variable
00055   int pdulenght = ((resultingPDU.length()/2) - 1);
00056   //cout << "AT command and lenght: " << "AT+CMGS=" << ((resultingPDU.length()/2) - 1) << endl;
00057   cout << "AT command and lenght: " << "AT+CMGS=" << pdulenght << endl;
00058   cout << "Encoded PDU: " << resultingPDU << endl;
00059   MyPDU.parsePDU(resultingPDU);
00060   cout << "Decoding the encoded PDU back: " << MyPDU.getText() << endl;
00061   return 0;
00062 
00063 
00064 
00065 
00066 
00067   cout << "-------------------------------------------" << endl;
00068   cout << "Serial-Port API test-runs:" << endl;
00069   
00070   SerialPort MyPort;
00071   char buf1[30];
00072   //char *tmpbuf[1024];
00073   //char buf2[1024];
00074   char buf3[5];
00075 
00076   //open the devicei, like: rfcomm0 for BT, ttyS0 for rs232 or ttyUSB0 for USB 
00077   MyPort.open("/dev/rfcomm0");
00078 
00079   cout << "Baud: " << MyPort.setBaudRate(BAUD_9600) << endl;
00080   cout << "CS: " << MyPort.setCharSize(CHAR_SIZE_8) << endl;
00081   cout << "Parity: " << MyPort.setParity(PARITY_NONE) << endl;
00082   cout << "Stopbits: " << MyPort.setStopBits(1) << endl;
00083   cout << "Flow Control:" << MyPort.setFlowControl(FLOW_CONTROL_NONE) << endl;
00084 
00085   // Device command for sending PDU formated SMS
00086   //sprintf(buf1,"AT+CMGS=23%c%c", (char)0x0D, (char)0x0A);
00087   sprintf(buf1,"AT+CMGS=%d%c%c", pdulenght, (char)0x0D, (char)0x0A);
00088   
00089   // PDU writes : hard-coded test
00090   //sprintf(buf2,"0011000B912374386044F50000AA0AE8329BFD4697D9EC37%c", (char)0x1A);
00091   
00092   // PDU writes : with string conversion from PDU library
00093   char *buf2 = strdup(resultingPDU.c_str());
00094   sprintf(buf3,"%c", (char)0x1A);
00095   
00096   cout << "START.. sending" << endl;
00097   cout << "Written: " << MyPort.write(buf1, strlen(buf1)) << " characters" << endl;
00098   usleep(1000000);
00099   cout << "Written: " << MyPort.write(buf2, strlen(buf2)) << " characters" << endl;
00100   usleep(1000000);
00101   cout << "Written: " << MyPort.write(buf3, strlen(buf3)) << " characters" << endl;
00102   cout << "Status: " << MyPort.status() << "\n";
00103 
00104   // free-up the converted PDU buffer 
00105   free (buf2);
00106 
00107 
00108 
00109 
00110 
00111   cout << "-------------------------------------------" << endl;
00112   cout << "Get device returnvalues:" << endl;
00113 
00114   int bytes;
00115   int prompt = 1;
00116   string returnval;
00117 
00118   // A better way than usleep's for writing serial data to the cell phone
00119   // for now only used to get the device answer...
00120   //while(prompt) {
00121     usleep(4000000);
00122     if ( MyPort.dataWaiting() > 0) {
00123       bytes = MyPort.dataWaiting();
00124       returnval =  MyPort.readBytes(bytes);
00125       cout << "Return-bytes: " << bytes << endl;
00126       cout << "Return-val: " << returnval << endl;
00127     }
00128     //usleep(1000000);
00129   //}
00130   
00131   // close communication port
00132   MyPort.close();
00133 
00134 
00135 
00136 
00137 
00138   return 0;
00139 
00140 }

Generated on Tue May 31 20:21:08 2005 for sendsms by  doxygen 1.4.2