endSMS()
描述
簡訊完成後呼叫 endSMS() ,告知 Arduino GSM shield 可以傳送簡訊了。
語法
SMS.endSMS()
參數
無參數
回傳
int:在非同步模式中回傳 0 表示最後呼叫的指令仍然在執行中,回傳 1 表示傳送簡訊成功,而回傳大於 1 的值代表有錯誤發生;在同步模式中回傳 1 表示傳送簡訊成功,回傳 0 表示傳送簡訊失敗
範例
#include <GSM.h>
#define PINNUMBER ""
// 初始化 GSM 函式庫
GSM gsmAccess;
GSM_SMS sms;
void setup()
{
  // 初始化 Serial
  Serial.begin(9600);
  Serial.println("SMS Messages Sender");
  // 連線狀態
  boolean notConnected = true;
  // 啟動 Arduino GSM shield,如果你的 SIM 卡有 PIN 碼,請當作 begin() 的參數
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("GSM initialized");
}
void loop()
{
  Serial.print("Enter a mobile number: ");
  // 用來存放簡訊目的地的電話號碼
  char remoteNumber[20];
  readSerial(remoteNumber);
  Serial.println(remoteNumber);
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);
  // 傳送簡訊
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");
}
/*
  從序列埠監控視窗讀取輸入
 */
int readSerial(char result[])
{
  int i = 0;
  while(1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if(inChar!='\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}
See also
- GSM_SMS
- beginSMS()
- ready()
- available()
- remoteNumber()
- read()
- write()
- print()
- peek()
- flush()
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.
