arduino 串口命令解析

arduino 串口命令解析
/*

DS3231_test.pde

Eric Ayars

4/11



Test/demo of read routines for a DS3231 RTC.



Turn on the serial monitor after loading this to check if things are

working as they should.



*/



#include <DS3231.h>

#include <Wire.h>

#include <EEPROM.h>



#define powerPin 7

DS3231 Clock;



String ReceivedCache="";

String BTime="2010-07-24 11:15:00";

String ETime="2010-07-24 11:15:00";

boolean isFire=false;



void setup() {

      // Start the I2C interface

       Wire.begin();

       Clock.setClockMode(false);

       Serial.begin(9600);

       pinMode(powerPin,OUTPUT);

       digitalWrite(powerPin,LOW);

       Clock.turnOnAlarm(1);

       RetrieveFireSet();

}





void loop() {

 

  handleCmd();

  checkFire();

}



void checkFire(){

  String dateTime=GetTime();

  if(dateTime>=BTime && dateTime<=ETime){

    digitalWrite(powerPin,HIGH); 

    isFire=true;

  }else{

    digitalWrite(powerPin,LOW); 

    isFire=false;

  }

}



String formatNum(int a){

  if(a<10)return  "0" +(String)a;

  return (String)a;

}



String GetTime(){

  bool Century=false;

  bool h12=false;

  bool PM=false;

    int second,minute,hour,date,month,year,temperature; 

    second=Clock.getSecond();

    minute=Clock.getMinute();

    hour=Clock.getHour(h12, PM);

    date=Clock.getDate();

    month=Clock.getMonth(Century);

    year=Clock.getYear();

    

    String dateTime="20" +formatNum(year) +"-" 

                     +formatNum(month) +"-"

                     +formatNum(date) +" "

                     +formatNum(hour) +":"

                     +formatNum(minute)+":"

                     +formatNum(second);

   return dateTime;

}



void handleGetTime(){

  

   String dateTime=GetTime();

    Serial.println("OK:"+dateTime);

    

}

void handleSetTime(){

  

  int second,minute,hour,date,month,year,dayOfWeek; 

  String dateTime=ReceivedCache.substring(5,24);

  

  year  =dateTime.substring(2,4).toInt();

  month  =dateTime.substring(5,7).toInt();

  date=dateTime.substring(8,10).toInt();

  

  hour=dateTime.substring(11,13).toInt();

  minute=dateTime.substring(14,16).toInt();

  second=dateTime.substring(17,19).toInt();

  dayOfWeek=dateTime.substring(20,21).toInt();

      Clock.setSecond(second);//Set the second 

      Clock.setMinute(minute);//Set the minute 

      Clock.setHour(hour);  //Set the hour 

      Clock.setDoW(dayOfWeek);    //Set the day of the week

      Clock.setDate(date);  //Set the date of the month

      Clock.setMonth(month);  //Set the month of the year

      Clock.setYear(year);  //Set the year (Last two digits of the year)



  Serial.println("OK:");

}



void handleGetFire(){

  String tmp=_ReadFireSet();

  if(tmp==""){

   Serial.println("EE:fire time not set!"); 

  }else{

   Serial.println("OK:" + tmp);

  }

}



void handleSetFire(){

  

   for(int address=0;address<43;address++){

     EEPROM.write(address,(byte)ReceivedCache[address]);

     //Serial.print((char)EEPROM.read(address));

   }

   //Serial.println("");

   String bTime=ReceivedCache.substring(5,24);

   String eTime=ReceivedCache.substring(24,43);

   bool flag=RetrieveFireSet();

  // Serial.println("flag:" + (String)flag);

   if(flag && (bTime==BTime && eTime==ETime)){

     Serial.println("OK:"); 

   }else{

     Serial.println("EE:Set Fail"); 

   }

}



String _ReadFireSet(){

    int address=0;

    String tmp="";

    char readChar=' ';

   for(int address=0;address<5;address++){

     readChar=(char)EEPROM.read(address);

     tmp +=readChar;

  }

 

  if(tmp!="SetF:"){

    return "";

  }

  

  tmp="";

   for(int address=5;address<43;address++){

     readChar=(char)EEPROM.read(address);

     tmp +=readChar;

  }

  //Serial.println(tmp);

  return tmp;

}



bool RetrieveFireSet(){

   String tmp=_ReadFireSet();

  if(tmp==""){

    return false;

  }else{

   BTime=tmp.substring(0,19);

   ETime=tmp.substring(19,38);

   return true;

  }

}



//read Serial data and hand command

//

void handleCmd(){

   char readChar=' ';

   

   while(Serial.available()>0){

      readChar=(char)Serial.read(); 

      ReceivedCache =ReceivedCache+ (String)readChar;

      //delayMicroseconds(10);

   }

   //Serial.println("ABC");

  // Serial.println(ReceivedCache);

   if(ReceivedCache.startsWith("GetT:")){

     handleGetTime();

     ReceivedCache=ReceivedCache.substring(5);

     

   }else if(ReceivedCache.startsWith("SetT:")){

     //like->SetT:2015-07-24 16:54:23,7

     if(ReceivedCache.length()>=26){

       handleSetTime();

       ReceivedCache=ReceivedCache.substring(26);

     }

   }else if(ReceivedCache.startsWith("GetS:")){

     Serial.println("OK:"+(String)isFire);

     ReceivedCache=ReceivedCache.substring(5);

   }else if(ReceivedCache.startsWith("GetF:")){

     handleGetFire();

     ReceivedCache=ReceivedCache.substring(5);

   }else if(ReceivedCache.startsWith("SetF:")){

     if(ReceivedCache.length()>=43){

       handleSetFire();

       ReceivedCache=ReceivedCache.substring(43);

     }

   }else if(ReceivedCache.startsWith("GetC:")){

     int temperature=Clock.getTemperature();

     Serial.println("OK:" +(String)temperature);

     ReceivedCache=ReceivedCache.substring(5);

   }

   else{

     if(ReceivedCache.length()>=5){

        ReceivedCache=""; 

     }

   }

   

   if(readChar=='\n')ReceivedCache="";

}
View Code

 

你可能感兴趣的:(arduino)