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();
- pinMode(powerPin,OUTPUT);
- digitalWrite(powerPin,LOW);
- Clock.turnOnAlarm();
- 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<)return "" +(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="" +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(,);
- year =dateTime.substring(,).toInt();
- month =dateTime.substring(,).toInt();
- date=dateTime.substring(,).toInt();
- hour=dateTime.substring(,).toInt();
- minute=dateTime.substring(,).toInt();
- second=dateTime.substring(,).toInt();
- dayOfWeek=dateTime.substring(,).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=;address<;address++){
- EEPROM.write(address,(byte)ReceivedCache[address]);
- //Serial.print((char)EEPROM.read(address));
- }
- //Serial.println("");
- String bTime=ReceivedCache.substring(,);
- String eTime=ReceivedCache.substring(,);
- 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=;
- String tmp="";
- char readChar=' ';
- for(int address=;address<;address++){
- readChar=(char)EEPROM.read(address);
- tmp +=readChar;
- }
- if(tmp!="SetF:"){
- return "";
- }
- tmp="";
- for(int address=;address<;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(,);
- ETime=tmp.substring(,);
- return true;
- }
- }
- //read Serial data and hand command
- //
- void handleCmd(){
- char readChar=' ';
- while(Serial.available()>){
- readChar=(char)Serial.read();
- ReceivedCache =ReceivedCache+ (String)readChar;
- //delayMicroseconds(10);
- }
- //Serial.println("ABC");
- // Serial.println(ReceivedCache);
- if(ReceivedCache.startsWith("GetT:")){
- handleGetTime();
- ReceivedCache=ReceivedCache.substring();
- }else if(ReceivedCache.startsWith("SetT:")){
- //like->SetT:2015-07-24 16:54:23,7
- if(ReceivedCache.length()>=){
- handleSetTime();
- ReceivedCache=ReceivedCache.substring();
- }
- }else if(ReceivedCache.startsWith("GetS:")){
- Serial.println("OK:"+(String)isFire);
- ReceivedCache=ReceivedCache.substring();
- }else if(ReceivedCache.startsWith("GetF:")){
- handleGetFire();
- ReceivedCache=ReceivedCache.substring();
- }else if(ReceivedCache.startsWith("SetF:")){
- if(ReceivedCache.length()>=){
- handleSetFire();
- ReceivedCache=ReceivedCache.substring();
- }
- }else if(ReceivedCache.startsWith("GetC:")){
- int temperature=Clock.getTemperature();
- Serial.println("OK:" +(String)temperature);
- ReceivedCache=ReceivedCache.substring();
- }
- else{
- if(ReceivedCache.length()>=){
- ReceivedCache="";
- }
- }
- if(readChar=='\n')ReceivedCache="";
- }
arduino 串口命令解析的更多相关文章
- 邵国际: C 语言对象化设计实例 —— 命令解析器
本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 邵国际 来源: 微信公众号linux阅码场(id: linuxdev) 内容简介 单片机工程师常常疑惑为什么 ...
- win10上使用php与python实现与arduino串口通信
注意: php 需要php7,安装及开启php_dio.dll com口按照实际的进行设置,如果不知道可以打开arduino编辑器进行查看 可以与用户实现命令行交互,但是效率过慢,不清楚如何优化,使用 ...
- linux mknod命令解析
linux mknod命令解析 http://www.cnblogs.com/cobbliu/archive/2011/07/05/2389014.html mknod:make node 生成设备 ...
- C#基础:命令解析
1.普通格式命令的解析 例如: RENA<SP>E:\\A.txt<SP>C:\\B.txt<CRLF> (SP -> 空格,CRLF -> 回车加换行 ...
- Appium Android Bootstrap源码分析之命令解析执行
通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...
- virsh的详细命令解析(一)
virsh的详细命令解析 virsh 有命令模式和交互模式如果直接在vrish后面添加参数是命令模式,如果直接写virsh,就会进入交互模式 virsh list 列出所有的虚拟机,虚拟机的状态有(8 ...
- IIC读写AT24C02代码2——串口命令控制多页读写
通过串口输入 R .W 进行控制程序读写IIC设备.波特率9600bps,晶振115200HZ. main.c /*------------------------------------------ ...
- (转)Linux命令:使用dig,nslookup命令解析域名
Linux命令:使用dig命令解析域名 Linux下解析域名除了使用nslookup之外,开可以使用dig命令来解析域名,dig命令可以得到更多的域名信息. dig的全称是 (domain infor ...
- angular-cli.json配置参数解析,常用命令解析
1.angular-cli.json配置参数解析 { "project": { "name": "ng-admin", //项目名称 &qu ...
随机推荐
- php用正则判断是否为数字
验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0-9]*)$验证有两位小数 ...
- 前端-CSS-9-文本和字体-背景颜色
字体属性 div{ width: 300px; height: 100px; /*background-color: red;*/ border: 1px solid red; /*设置字体大小 px ...
- javascript时间日期操作
Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- 程序员教程-10章-C++程序设计
本来这章是不打算写了的,毕竟自己学的是JAVA,C++就不想看了,但毕竟还有一点时间,就把C++也看一下吧 目录结构 10.1 C++程序基础 10.1.1 数据类型 1 基本数据类型 2 常量和变量 ...
- 【转】Phong和Blinn-Phong光照模型
来自:http://www.cnblogs.com/bluebean/p/5299358.html Phong和Blinn-Phong是计算镜面反射光的两种光照模型,两者仅仅有很小的不同之处. 1.P ...
- How to Pronounce OPPORTUNITY
How to Pronounce OPPORTUNITY Share Tweet Share Take the opportunity to learn this word! Learn how t ...
- 调整Mic音量
uses MMSystem; function GetLineInHandle(AudioType : integer) : integer;var i : integer; AudioCaps ...
- Hibernate迫切左外连接和迫切内连接
•迫切左外连接: •LEFT JOIN FETCH 关键字表示迫切左外连接检索策略. –list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee ...
- ingress 密码验证
traefik ingress 上面的方式需要引入haprox或者nginx,多引入了一个代理转发层,其实ingress本身就提供了basic auth的支持,在ingress规则中添加额外的认证an ...
- Java对称与非对称加密解密,AES与RSA
加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...