master
parent
7e8e1b2eb6
commit
28c7f74e73
|
@ -28,3 +28,4 @@
|
|||
| 25 | buttondefence | 通用按钮地图效果 |
|
||||
| 26 | mouseline | 鼠标定位十字线 |
|
||||
| 27 | email | 邮件发送工具 |
|
||||
| 27 | ntpclient | NTP服务器时间同步 |
|
|
@ -0,0 +1,27 @@
|
|||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmntpclient.h"
|
||||
#include "ui_frmntpclient.h"
|
||||
#include "ntpclient.h"
|
||||
|
||||
frmNtpClient::frmNtpClient(QWidget *parent) : QWidget(parent), ui(new Ui::frmNtpClient)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
connect(NtpClient::Instance(), SIGNAL(receiveTime(QDateTime)), this, SLOT(receiveTime(QDateTime)));
|
||||
}
|
||||
|
||||
frmNtpClient::~frmNtpClient()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmNtpClient::on_btnGetTime_clicked()
|
||||
{
|
||||
NtpClient::Instance()->setNtpIP(ui->txtNtpIP->text().trimmed());
|
||||
NtpClient::Instance()->getDateTime();
|
||||
}
|
||||
|
||||
void frmNtpClient::receiveTime(const QDateTime &dateTime)
|
||||
{
|
||||
ui->txtTime->setText(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef FRMNTPCLIENT_H
|
||||
#define FRMNTPCLIENT_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QDateTime>
|
||||
|
||||
namespace Ui {
|
||||
class frmNtpClient;
|
||||
}
|
||||
|
||||
class frmNtpClient : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmNtpClient(QWidget *parent = 0);
|
||||
~frmNtpClient();
|
||||
|
||||
private:
|
||||
Ui::frmNtpClient *ui;
|
||||
|
||||
private slots:
|
||||
void on_btnGetTime_clicked();
|
||||
void receiveTime(const QDateTime &dateTime);
|
||||
};
|
||||
|
||||
#endif // FRMNTPCLIENT_H
|
|
@ -0,0 +1,68 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmNtpClient</class>
|
||||
<widget class="QWidget" name="frmNtpClient">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>10</y>
|
||||
<width>321</width>
|
||||
<height>48</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="labNtpIP">
|
||||
<property name="text">
|
||||
<string>NTP服务器</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLineEdit" name="txtNtpIP">
|
||||
<property name="text">
|
||||
<string>133.100.11.8</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2" rowspan="2">
|
||||
<widget class="QPushButton" name="btnGetTime">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Minimum" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>获取时间</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="labTime">
|
||||
<property name="text">
|
||||
<string>返回时间</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLineEdit" name="txtTime"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
|
@ -0,0 +1,31 @@
|
|||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmntpclient.h"
|
||||
#include <QApplication>
|
||||
#include <QTextCodec>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
a.setFont(QFont("Microsoft Yahei", 9));
|
||||
|
||||
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
|
||||
#if _MSC_VER
|
||||
QTextCodec *codec = QTextCodec::codecForName("gbk");
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
#endif
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
QTextCodec::setCodecForCStrings(codec);
|
||||
QTextCodec::setCodecForTr(codec);
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
#endif
|
||||
|
||||
frmNtpClient w;
|
||||
w.setWindowTitle("Ntp校时");
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
#include "ntpclient.h"
|
||||
#include "qmutex.h"
|
||||
#include "qudpsocket.h"
|
||||
|
||||
QScopedPointer<NtpClient> NtpClient::self;
|
||||
NtpClient *NtpClient::Instance()
|
||||
{
|
||||
if (self.isNull()) {
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
if (self.isNull()) {
|
||||
self.reset(new NtpClient);
|
||||
}
|
||||
}
|
||||
|
||||
return self.data();
|
||||
}
|
||||
|
||||
NtpClient::NtpClient(QObject *parent) : QObject(parent)
|
||||
{
|
||||
ntpIP = "202.120.2.101";
|
||||
|
||||
udpSocket = new QUdpSocket(this);
|
||||
connect(udpSocket, SIGNAL(connected()), this, SLOT(sendData()));
|
||||
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
|
||||
}
|
||||
|
||||
void NtpClient::sendData()
|
||||
{
|
||||
qint8 LI = 0;
|
||||
qint8 VN = 3;
|
||||
qint8 MODE = 3;
|
||||
qint8 STRATUM = 0;
|
||||
qint8 POLL = 4;
|
||||
qint8 PREC = -6;
|
||||
QDateTime epoch(QDate(1900, 1, 1));
|
||||
qint32 second = quint32(epoch.secsTo(QDateTime::currentDateTime()));
|
||||
|
||||
qint32 temp = 0;
|
||||
QByteArray timeRequest(48, 0);
|
||||
timeRequest[0] = (LI << 6) | (VN << 3) | (MODE);
|
||||
timeRequest[1] = STRATUM;
|
||||
timeRequest[2] = POLL;
|
||||
timeRequest[3] = PREC & 0xff;
|
||||
timeRequest[5] = 1;
|
||||
timeRequest[9] = 1;
|
||||
timeRequest[40] = (temp = (second & 0xff000000) >> 24);
|
||||
temp = 0;
|
||||
timeRequest[41] = (temp = (second & 0x00ff0000) >> 16);
|
||||
temp = 0;
|
||||
timeRequest[42] = (temp = (second & 0x0000ff00) >> 8);
|
||||
temp = 0;
|
||||
timeRequest[43] = ((second & 0x000000ff));
|
||||
|
||||
udpSocket->write(timeRequest);
|
||||
}
|
||||
|
||||
void NtpClient::readData()
|
||||
{
|
||||
QByteArray newTime;
|
||||
QDateTime epoch(QDate(1900, 1, 1));
|
||||
QDateTime unixStart(QDate(1970, 1, 1));
|
||||
|
||||
while (udpSocket->hasPendingDatagrams()) {
|
||||
newTime.resize(udpSocket->pendingDatagramSize());
|
||||
udpSocket->read(newTime.data(), newTime.size());
|
||||
};
|
||||
|
||||
QByteArray transmitTimeStamp ;
|
||||
transmitTimeStamp = newTime.right(8);
|
||||
quint32 seconds = transmitTimeStamp.at(0);
|
||||
quint8 temp = 0;
|
||||
|
||||
for (int i = 1; i <= 3; i++) {
|
||||
seconds = (seconds << 8);
|
||||
temp = transmitTimeStamp.at(i);
|
||||
seconds = seconds + temp;
|
||||
}
|
||||
|
||||
QDateTime dateTime;
|
||||
dateTime.setTime_t(seconds - epoch.secsTo(unixStart));
|
||||
|
||||
#ifdef __arm__
|
||||
#ifdef arma9
|
||||
dateTime = dateTime.addSecs(60 * 60 * 8);
|
||||
#endif
|
||||
#endif
|
||||
udpSocket->disconnectFromHost();
|
||||
|
||||
//有些时候返回的数据可能有误或者解析不正确,导致填充的时间不正确
|
||||
if (dateTime.isValid()) {
|
||||
emit receiveTime(dateTime);
|
||||
}
|
||||
}
|
||||
|
||||
void NtpClient::setNtpIP(const QString &ntpIP)
|
||||
{
|
||||
if (this->ntpIP != ntpIP) {
|
||||
this->ntpIP = ntpIP;
|
||||
}
|
||||
}
|
||||
|
||||
void NtpClient::getDateTime()
|
||||
{
|
||||
udpSocket->abort();
|
||||
udpSocket->connectToHost(ntpIP, 123);
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
#ifndef NTPCLIENT_H
|
||||
#define NTPCLIENT_H
|
||||
|
||||
/**
|
||||
* Ntp校时类 作者:feiyangqingyun(QQ:517216493) 2017-2-16
|
||||
* 1:可设置Ntp服务器IP地址
|
||||
* 2:收到时间信号发出
|
||||
*/
|
||||
|
||||
#include <QObject>
|
||||
#include <QDateTime>
|
||||
class QUdpSocket;
|
||||
|
||||
#ifdef quc
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
|
||||
#include <QtDesigner/QDesignerExportWidget>
|
||||
#else
|
||||
#include <QtUiPlugin/QDesignerExportWidget>
|
||||
#endif
|
||||
|
||||
class QDESIGNER_WIDGET_EXPORT NtpClient : public QObject
|
||||
#else
|
||||
class NtpClient : public QObject
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static NtpClient *Instance();
|
||||
explicit NtpClient(QObject *parent = 0);
|
||||
|
||||
private:
|
||||
static QScopedPointer<NtpClient> self;
|
||||
QString ntpIP;
|
||||
QUdpSocket *udpSocket;
|
||||
|
||||
private slots:
|
||||
void readData();
|
||||
void sendData();
|
||||
|
||||
signals:
|
||||
//收到时间返回
|
||||
void receiveTime(const QDateTime &dateTime);
|
||||
|
||||
public slots:
|
||||
//设置NTP服务器IP
|
||||
void setNtpIP(const QString &ntpIP);
|
||||
|
||||
//获取日期时间
|
||||
void getDateTime();
|
||||
|
||||
};
|
||||
|
||||
#endif // NTPCLIENT_H
|
|
@ -0,0 +1,23 @@
|
|||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2019-02-16T15:08:47
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core gui network
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
TARGET = ntpclient
|
||||
TEMPLATE = app
|
||||
DESTDIR = $$PWD/../bin
|
||||
CONFIG += warn_off
|
||||
|
||||
SOURCES += main.cpp
|
||||
SOURCES += frmntpclient.cpp
|
||||
SOURCES += ntpclient.cpp
|
||||
|
||||
HEADERS += frmntpclient.h
|
||||
HEADERS += ntpclient.h
|
||||
|
||||
FORMS += frmntpclient.ui
|
Loading…
Reference in New Issue