⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendsms.cpp

📁 用一个开源码ASCII ENCODE,DECODE,来发送ASCII短信
💻 CPP
字号:
#include <iostream>#include "SerialPort.h"#include "pdu.h"using namespace std;using std::string;using namespace SerialPorts;int main (int argc, char *argv[], char *envp[]) {  //string pduString = "0011000C912394986343F50000AA08E8329BFD563753";  string resultingPDU;  pdu MyPDU;  char *buf_message = argv[2];  char *buf_telnr = argv[1];  cout << "-------------------------------------------" << endl;  cout << "Get and print arguments:" << endl;  if ( argc != 3 ) {    cout << endl;    cout << "USAGE:   Please provide exact 2 arguments." << endl;    cout << "         The 1st argument should be an internationan cell phone number." << endl;    cout << "         The 2nd argument should be you SMS message." << endl;    cout << endl;    cout << "EXAMPLE: sendsms +32478306445 \'hello there!\'" << endl;    return 1;  }  cout << "Number of arguments: " << argc - 1 << endl;  cout << "Telephone: " << buf_telnr << endl;  cout << "Message: " << buf_message << endl;  cout << "-------------------------------------------" << endl;  cout << "PDU API test-runs:" << endl;    // Decoding test   //MyPDU.parsePDU(pduString);  //cout << MyPDU.getText();    //cout << MyPDU.setText("hellohello") << endl;  cout << MyPDU.setText(buf_message) << endl;  //cout << MyPDU.setSender("+32478306445") << endl;  cout << MyPDU.setSender(buf_telnr) << endl;  resultingPDU = MyPDU.encodePDU();  //make pdu lenght variable  int pdulenght = ((resultingPDU.length()/2) - 1);  //cout << "AT command and lenght: " << "AT+CMGS=" << ((resultingPDU.length()/2) - 1) << endl;  cout << "AT command and lenght: " << "AT+CMGS=" << pdulenght << endl;  cout << "Encoded PDU: " << resultingPDU << endl;  MyPDU.parsePDU(resultingPDU);  cout << "Decoding the encoded PDU back: " << MyPDU.getText() << endl;  cout << "-------------------------------------------" << endl;  cout << "Serial-Port API test-runs:" << endl;    SerialPort MyPort;  char buf1[30];  //char *tmpbuf[1024];  //char buf2[1024];  char buf3[5];  //open the devicei, like: rfcomm0 for BT, ttyS0 for rs232 or ttyUSB0 for USB   MyPort.open("/dev/rfcomm0");  cout << "Baud: " << MyPort.setBaudRate(BAUD_9600) << endl;  cout << "CS: " << MyPort.setCharSize(CHAR_SIZE_8) << endl;  cout << "Parity: " << MyPort.setParity(PARITY_NONE) << endl;  cout << "Stopbits: " << MyPort.setStopBits(1) << endl;  cout << "Flow Control:" << MyPort.setFlowControl(FLOW_CONTROL_NONE) << endl;  // Device command for sending PDU formated SMS  //sprintf(buf1,"AT+CMGS=23%c%c", (char)0x0D, (char)0x0A);  sprintf(buf1,"AT+CMGS=%d%c%c", pdulenght, (char)0x0D, (char)0x0A);    // PDU writes : hard-coded test  //sprintf(buf2,"0011000B912374386044F50000AA0AE8329BFD4697D9EC37%c", (char)0x1A);    // PDU writes : with string conversion from PDU library  char *buf2 = strdup(resultingPDU.c_str());  sprintf(buf3,"%c", (char)0x1A);    cout << "START.. sending" << endl;  cout << "Written: " << MyPort.write(buf1, strlen(buf1)) << " characters" << endl;  usleep(1000000);  cout << "Written: " << MyPort.write(buf2, strlen(buf2)) << " characters" << endl;  usleep(1000000);  cout << "Written: " << MyPort.write(buf3, strlen(buf3)) << " characters" << endl;  cout << "Status: " << MyPort.status() << "\n";  // free-up the converted PDU buffer   free (buf2);  cout << "-------------------------------------------" << endl;  cout << "Get device returnvalues:" << endl;  int bytes;  int prompt = 1;  string returnval;  // A better way than usleep's for writing serial data to the cell phone  // for now only used to get the device answer...  //while(prompt) {    usleep(4000000);    if ( MyPort.dataWaiting() > 0) {      bytes = MyPort.dataWaiting();      returnval =  MyPort.readBytes(bytes);      cout << "Return-bytes: " << bytes << endl;      cout << "Return-val: " << returnval << endl;    }    //usleep(1000000);  //}    // close communication port  MyPort.close();  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -