RaspberryPi树莓派NTP高精度对时服务解决方案_第1页
RaspberryPi树莓派NTP高精度对时服务解决方案_第2页
RaspberryPi树莓派NTP高精度对时服务解决方案_第3页
RaspberryPi树莓派NTP高精度对时服务解决方案_第4页
RaspberryPi树莓派NTP高精度对时服务解决方案_第5页
免费预览已结束,剩余10页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、目录1RaspberryPi树莓派系统NTP对时服务解决方案21.1解决方案概述21.2对时服务程序31.2.1main.cpp31.2.2config_file.h91.2.3config_file.cpp9RaspberryPi树莓派系统NTP对时服务解决方案解决方案概述RaspberryPi树莓派系统在实际的应用过程中,特别是作为产品级应用中,有对系统时间的精准度有高指标要求的应用场景,比如要求时间服务精度达到0.1秒内或者更高要求。树莓派设备本身没有硬件时钟, 因此每次设备通电开启后都需要重新授时来获得准确的系统时间,树莓派系统的时间主要是通过NTP协议同步网络时间来实现的。本方案提供

2、一套完整的高精度系统时间服务解决方案,思路如下:1、使用ntpdate取缔默认的ntp实现NTP对时服务功能;2、开发基于C语方的对时程序,实现NTP对时服务以及状态信息(最重要的是误差时间信息)产出,并提供专用调用接口服务,程序代码请参考下面章节。3、配置crontab定时任务定期执行对时脚本,实现定期系统对时,为什么要定时对时呢?因为树莓派设备本身的守时精度不是特别高的,因此需要定期执行时间同步操作来校正系统时间。4、实际应用程序在需要获取精准时间服务时,综合系统最新时间信息以及误差时间信息进行校正处理,得到精准时间,如下图所示。rooLPtaidedev;cat/hojne/usb/lL

3、g/Scatus/ntp,log2020-0t-0517:32:10.9645735Jun17:52:10nLpdrite21265:Jdjimr.rim白120.IOS.110心口电白,安白un.ntp.utq-cnrd:息id在dew:-在上图中为ntpdate执行对时服务后的状态信息,其中最重要的是offset状态数据,它代表的是与上次对时间的偏移量,也就是误差值,经过实际经验总结,如果对时后出现offset数值过大,需要进行重复多次执行同步,直到误差足句小(本人设定offset要小于0.01),要得到精准的时间,这个offset值起到关键作用,经大量实验证明, 精准时间=当前系统时间+

4、offset,当然前提是offset误差值一定要足够小才可以这样处理。以下附上使用ntpdate实现NTP对时服务功能代码:对时服务程序main.cpp#include#include#include#include#include#includeconfig_file.h#include#includeusingnamespacestd;intgetLogPath(char*retbuf,intlen,intserver)charconfigFile口=/home/pi/params/pi.ini;chardefPath口=/home/usb/log/ntp.log;memset(retbu

5、f,0,len);chartmp256;/resultintres=GetConfigStringValue(configFile,PI_PAR,LOGSTATUS_DIR,tmp);if(res)/novalue,setasdefaultstrcpy(retbuf,defPath);couterror0&strResultiSize-1=n)strResult=strResult.substr(0,iSize-1);)returnstrResult;)unsignedparseArg(intargc,char*argv)unsignedret=0;intch=0;while(ch=g

6、etopt(argc,argv,bd)!=-1)switch(ch)caseb:backupntpserverret|=0 x01;break;cased:debugret|=0 x02;default:break;)returnret;)/linuxintmain(intargc,char*argv)(unsignedmode=parseArg(argc,argv);/coutmode1)&0 x1;if(bDebug)(printf(Enterdebugmode!n);printf(Version19.07.14.n);)chartmp1024;charconfigFile口=/h

7、ome/pi/params/service.ini;intserverNo=mode&1;charlogfile1024;intlen=getLogPath(logfile,sizeof(logfile),serverNo);stringfilepath(logfile);coutfilepathendl;intres=GetConfigStringValue(configFile,SERVICE_PAR,NTP_SERVICE”,tmp);intuse=1;if(use=0)/never(printf(Notusentp.n);stringresponse(Fail.);WriteL

8、og(filepath,response,Nouse);return0;)if(!serverNo)res=GetConfigStringValue(configFile,SERVICE_PAR,NTP_SERVER”,tmp);elseres=GetConfigStringValue(configFile,SERVICE_PAR,NTP_SERVER1,tmp);stringserver(tmp);coutserverendl;stringresponse;if(res)printf(Errorreadingconfigfile!n);response=Fail.;WriteLog(file

9、path,response,server);return-1;)elsecharcmd256;memset(cmd,0,sizeof(cmd);sprintf(cmd,sudontpdate%sn,tmp);response=getCmdResult(cmd);stringmsg=response.empty()?Fail.:response;WriteLog(filepath,msg,server);return0;)return0;intCompareString(char*pInStr1,char*pInStr2);intGetKeyValue(FILE*fpConfig,char*pI

10、nKeyName,char*pOutKeyValue);intGetConfigIntValue(char*pInFileName,char*pInSectionName,char*pInKeyName,int*pOutKeyValue);intGetConfigStringValue(char*pInFileName,char*pInSectionName,char*pInKeyName,char*pOutKeyValue);#endif/CONFIG_FILE_H1.2.3config_file.cpp产*FileName:config.cFileFunc:定义实现文件Version:V0

11、.11.2.2config_file.h#ifndefCONFIG_FILE_H#defineCONFIG_FILE_H#defineSUCCESS#defineFAILURE0 x00/*成功*/0 x01/*失败*/#defineFILENAME_NOTEXIST0 x02/*配置文件名不存在*/#defineSECTIONNAME_NOTEXIST#defineKEYNAME_NOTEXIST#defineSTRING_LENNOTEQUAL#defineSTRINGNOTEQUAL0 x03/*节名不存在*/0 x04/*键名不存在*/0 x05/*两个字符串长度不同*/0 x06/*

12、两个字符串内容不相同*/#defineSTRING_EQUAL0 x00/*两个字符串内容相同*/Author:SunrierDate:2012-05-09Descp:Linux下获取配置文件信息*/#include#include#include#includeconfig_file.hintGetConfigStringValue(char*pInFileName,char*pInSectionName,char*pInKeyName,char*pOutKeyValue)FILE*fpConfig;charszBuffer150;char*pStr1,*pStr2;intiRetCode=

13、0;/*test*/*printf(pInFileName:%s!n,pInFileName);printf(pInSectionName:%s!n,pInSectionName);printf(pInKeyName:%s!n,pInKeyName);*/memset(szBuffer,0,sizeof(szBuffer);if(NULL=(fpConfig=fopen(pInFileName,r)returnFILENAME_NOTEXIST;while(!feof(fpConfig)if(NULL=fgets(szBuffer,150,fpConfig)break;pStr1=szBuff

14、er;while(=*pStr1)|(t=*pStr1)pStr1+;if(=*pStr1)pStr1+;while(=*pStr1)|(t=*pStr1)pStr1+;pStr2=pStr1;while(!=*pStr1)&(0!=*pStr1)pStr1+;if(0=*pStr1)continue;while(=*(pStr1-1)pStr1-;*pStr1=0;iRetCode=CompareString(pStr2,pInSectionName);if(!iRetCode)/*检查节名*/iRetCodeGetKeyValue(fpConfig,pInKeyName,pOutK

15、eyValue);fclose(fpConfig);returniRetCode;fclose(fpConfig);returnSECTIONNAME_NOTEXIST;/*区分大小写*/intCompareString(char*pInStr1,char*pInStr2)if(strlen(pInStr1)!=strlen(pInStr2)returnSTRING_LENNOTEQUAL;/*while(toupper(*pInStr1)=toupper(*pInStr2)*/while(*pInStr1=*pInStr2)if(0=*pInStr1)break;pInStr1+;pInSt

16、r2+;if(0=*pInStr1)returnSTRING_EQUAL;returnSTRING_NOTEQUAL;intGetKeyValue(FILE*fpConfig,char*pInKeyName,char*pOutKeyValue)(charszBuffer150;)*/*#includechar*pStr1,*pStr2,*pStr3;unsignedintuiLen;intiRetCode=0;memset(szBuffer,0,sizeof(szBuffer);while(!feof(fpConfig)(if(NULL=fgets(szBuffer,150,fpConfig)

17、break;pStr1=szBuffer;while(=*pStr1)|(t=*pStr1)pStr1+;if(#=*pStr1)continue;if(/=*pStr1)&(/=*(pStr1+1)continue;if(0=*pStr1)|(0 x0d=*pStr1)|(0 x0a=*pStr1)continue;if(=*pStr1)(pStr2=pStr1;while(T!=*pStr1)&(0!=*pStr1)pStr1+;if(=*pStr1)break;pStr1=pStr2;pStr2=pStrl;while(=!=*pStr1)&(0!=*pStr1)

18、pStr1+;if(0=*pStr1)continue;pStr3=pStr1+1;if(pStr2=pStr1)continue;*pStr1=0;pStr1-;while(=*pStr1)|(t=*pStr1)*pStr1=0;pStr1-;iRetCode=CompareString(pStr2,pInKeyName);if(!iRetCode)/*检查键名*/pStr1=pStr3;while(=*pStr1)|(t=*pStr1)pStr1+;pStr3=pStr1;while(0!=*pStr1)&(0 x0d!=*pStr1)&(0 x0a!=*pStr1)if(7

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论