peek()
描述
回傳未讀簡訊資料中的第一個字元,回傳後不把該字元認為是已讀。由於沒有把回傳的字元認為是已讀,所以如果沒有呼叫 read()
每次呼叫 peek()
都會得到同一個字元。
peek()
是從 Stream
繼承而來。
語法
SMS.peek()
參數
無參數
回傳
int
:回傳未讀的簡訊內容中最前面的字元,如果沒有簡訊內容可以讀回傳 -1
範例
/* SMS receiver 監控是否有收到簡訊,收到簡訊時把簡訊內容輸出到序列埠監控視窗 Circuit: * GSM shield created 25 Feb 2012 by Javier Zorzano / TD This example is in the public domain. */ // 函式庫 #include <GSM.h> // PIN 碼 #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()
- available()
- remoteNumber()
- read()
- write()
- print()
- 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.