write()
描述
寫資料進簡訊。
語法
SMS.write(val)
參數
val
:要寫入簡訊的字元
回傳
byte
:回傳成功寫入簡訊裡的字元數量
範例
#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()
- endSMS()
- available()
- remoteNumber()
- read()
- 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.