available()

描述

檢查是否有尚未讀取的簡訊。

語法


SMS.available()

參數

無參數

回傳

int:簡訊內容大小

範例

#define PINNUMBER ""

// 初始化 GSM 函式庫
GSM gsmAccess;
GSM_SMS sms;

// 用來存放電話號碼的字元陣列
char remoteNumber[20];

void setup() 
{
  // 初始化 Serial
  Serial.begin(9600); 

  Serial.println("SMS Messages Receiver");

  // 連線狀態
  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");
  Serial.println("Waiting for messages");
}

void loop() 
{
  char c;

  // 檢查是否有未讀的簡訊
  if (sms.available())
  {
    Serial.println("Message received from:");

    // 取得傳送簡訊的電話號碼
    sms.remoteNumber(remoteNumber, 20);
    Serial.println(remoteNumber);

    // 這是一個刪除簡訊的範例,在這裡把開頭為 # 的簡訊刪除
    if(sms.peek()=='#')
    {
      Serial.println("Discarded SMS");
      sms.flush();
    }

    // 讀取簡訊內容並輸出到序列埠監控視窗
    while(c=sms.read())
      Serial.print(c);

    Serial.println("\nEND OF MESSAGE");

    // 刪除簡訊
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

  delay(1000);

}

See also

GSM_SMS
beginSMS()
ready()
endSMS()
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.