我已经在一个名为CONFIG.BAT的文件中将一个数字保存到sd卡中,它是一个电话号码,格式为“ +441234567890”,在串行监视器中,我可以使用
myFile = SD.open("CONFIG.DAT");
if (myFile) {
Serial.println("CONFIG.DAT:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
我得到这个回应
Initializing SD card...initialization done.
CONFIG.DAT:
+441234567890
我想做的就是将此数字放入变量“ telNo”中,我尝试使用
telNo = (myFile.read());
我得到的答复是
Initializing SD card...initialization done.
CONFIG.DAT:
10
我不确定这个“ 10”代表什么。
我也尝试过“ concat”
while (myFile.available()) {
number.concat(myFile.read());
}
我得到的响应是数字的十进制代码,最后还有一些我不明白的数字
Initializing SD card...initialization done.
CONFIG.DAT:
48555653515353555354561310
我不明白最后四个数字(1310)
我想要实现的是“ telNo = CONFIG.DAT文件中的电话号码,因此如果有人可以帮助我将不胜感激
这是我的完整草图
#include <SoftwareSerial.h>
//#include <SD.h>
#define SD_CS_PIN SS
#include <SPI.h>
#include "SdFat.h"
SdFat SD;
File myFile;
//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8
//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7
//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
//const int ledPin3 = 11;//Define the Bluetooth led pin
const int ledPin2 = 2;//Define the interupt pin to signify bluetooth connect or disconnect
String readString = "";
String telNoString = "";
String number = "";
volatile int ledonState = 0;
int lastLedonState = 0;
const int thresholdvalue=680;//The threshold to turn the led on
void setup() {
pinMode (ledPin2, INPUT);//set input for interupt pin
//pinMode (ledPin3, OUTPUT);//set output for bluetooth pin
attachInterrupt(0, pin_ISR, CHANGE);
Serial.begin(9600);
while(!Serial);
//Being serial communication witj Arduino and SIM800
serialSIM800.begin(9600);
delay(1000);
SysCall::yield();
Serial.println("Setup Complete!");
}
void pin_ISR() {
ledonState = digitalRead(ledPin2);
if (ledonState != lastLedonState) {
if (ledonState == HIGH) {
//digitalWrite(ledPin3, HIGH);//turn led on
Serial.println("HC-05 is now connected");
//Serial.println();
}else{
//digitalWrite(ledPin3, LOW);//turn led off
Serial.println("HC-05 is now Disconnected");
//Serial.println();
}
lastLedonState = ledonState;
}
}
void sound_detect(){
int sensorValue = analogRead(A0);//use A0 to read the electrical signal
if(sensorValue > thresholdvalue) {
// digitalWrite(ledPin1,HIGH);//if the value read from A0 is larger than 400,then light the LED
// delay(10);
// digitalWrite(ledPin1,LOW);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (!SD.exists("CONFIG.DAT")) {
Serial.println("No Number Exists! Please go to Setup Device to add Number for Alert");
}else{
// open the file for reading:
myFile = SD.open("CONFIG.DAT");
if (myFile) {
Serial.println("CONFIG.DAT:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
number.concat(myFile.read());
//number = (myFile.read());
//Serial.write(myFile.read());
}
//Serial.println(myFile);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening CONFIG.DAT");
}
}
// //Set SMS format to ASCII
// serialSIM800.write("AT+CMGF=1\r\n");
// delay(1000);
//
// //Send new SMS command and message number
// serialSIM800.write("AT+CMGS=\"+44"+number+"\"\r\n");
// delay(1000);
//
// //Send SMS content
// serialSIM800.write("TEST SMS NOISE DETECT");
// delay(1000);
//
// //Send Ctrl+Z / ESC to denote SMS message is complete
// serialSIM800.write((char)26);
// delay(1000);
//
// Serial.println("SMS Sent!");
Serial.println("this is the phone number: ");
Serial.print(number);
}
}
void insertNo(){
while(Serial.available()==0) { // Wait for User to Input Data
}
telNoString = Serial.readString();
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
if (SD.exists("CONFIG.DAT")) {
SD.remove("CONFIG.DAT");
}
if (!SD.exists("CONFIG.DAT")) {
myFile = SD.open("CONFIG.DAT", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to CONFIG.DAT...");
myFile.println(telNoString);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening CONFIG.DAT");
}
// re-open the file for reading:
myFile = SD.open("CONFIG.DAT");
if (myFile) {
Serial.println("CONFIG.DAT:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening CONFIG.DAT");
}
}
return;
}
void loop() {
while (Serial.available()) {
delay(10);
char c = Serial.read();
readString += c;
}
if(readString == "setup device") {
Serial.println(readString);
readString = "";
insertNo();
}else{
if(readString == "start device") {
Serial.println(readString);
readString = "";
}
sound_detect();
}
}
注释掉的代码部分是用于发送短信的AT命令。
最佳答案
telNo = (myFile.read());
可以编译吗? telNo
是什么类型?如果它是int
或其他数字,则不会得到所需的结果,因为read()
读取字符而不是数字。.看到的10是newline
字符的ascii代码('\ n '),它是文件中的第一个字符(这就是为什么您的数字低于CONFIG.DAT:
的原因)。read()
一次读取一个字节,如果没有可用字节,则返回-1。要获取全部内容,您应该使用循环来一次读取一个字符并将这些字符附加到char[]
:
unsigned int MAX_SIZE = 100;
char string[MAX_SIZE];
unsigned int index = 0;
char next_char;
while (next_char = myfile.read()) string[index++] = next_char;
string[index] = '/0'; // terminate with the null character
现在,如果您确实需要
int
,则需要过滤掉newline
,+
和其他任何无效数字,然后将数字转换为int
:char new_string[MAX_SIZE];
unsigned int i = 0, unsigned int j = 0;
while (i < MAX_SIZE) {
if ((string[i] >= 48) && (string[i] <= 57))
new_string[j++] = string[i++];
else i++;
}
int number = atoi(new_string);