一、项目介绍
通过TCP/IP协议实现客户端和和服务端的链接,服务端和下位机通过串口通信的方式链接,传递信息,客户端通过账号登录进入进入智能家居服务系统,账号登录和QQ登录类似,我采用的是数据库的方式实现数据的存储和调用,界面使用qt-ui的方式进行建立,有摄像头操作,音乐播放器操作和视频操作,客户端通过按下开关的操作将json数据发送到服务端再经过串口的方式对下位机进行操作。
二、项目技术
qt-UI界面,c++,json,TCP/IP协议,摄像头,音乐播放器,视频操作,串口通信,stm32f407vet6。
三、项目内容
1.服务端头文件:需要在pro文件中添加 network(TCP数据链接) serialport(串口通信)
#ifndef SERVERWINDOW_H
#define SERVERWINDOW_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress> // 引入串口通信的两个头文件(第一步)
#include <QSerialPort> // 提供访问串口的功能
#include <QSerialPortInfo> // 提供系统中存在的串口信息
namespace Ui {
class serverwindow;
}
class serverwindow : public QMainWindow
{
Q_OBJECT
public:
explicit serverwindow(QWidget *parent = 0);
~serverwindow();
QTcpServer* m_server; //服务器对象
QTcpSocket* m_tcp; //客户端对象
QSerialPort m_serial;//串口
private slots:
//点击连接服务器
void on_pushButton_clicked();
//点击发送按钮
void on_pushButton_2_clicked();
//客户端连接上
void ClientConnect();
//有数据来了,则响应readyread信号
void readData();
//客户端断开连接
void Clientdisconnect();
//打开串口
void on_pushButton_3_clicked();
//关闭串口
void on_pushButton_4_clicked();
//串口接受信息的处理函数
void messlot();
//刷新串口
void on_pushButton_5_clicked();
private:
Ui::serverwindow *ui;
};
#endif // SERVERWINDOW_H
服务端.c文件:
#include "serverwindow.h"
#include "ui_serverwindow.h"
serverwindow::serverwindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::serverwindow)
{
ui->setupUi(this);
setWindowTitle(tr("服务端"));
}
serverwindow::~serverwindow()
{
delete ui;
}
//启动服务器
void serverwindow::on_pushButton_clicked()
{
//第一步:创建对象 QTcpserver对象
m_server= new QTcpServer(this);
//第二步:监听 listen函数
unsigned short port = ui->lineEdit->text().toShort();
m_server->listen(QHostAddress::AnyIPv4 ,port);
//第三步:处理信号槽函数处理 (槽函数中获取客户端的socket)
QObject::connect(m_server,&QTcpServer::newConnection,
this,&serverwindow::ClientConnect);
// nextpendingconnction函数可以获取到客户端的socket,也就是QTCPsocket对象
//第四步:如果连接上了,设置按钮为灰色
ui->pushButton->setEnabled(false);
}
//发送数据
void serverwindow::on_pushButton_2_clicked()
{
//使用write函数,QTcpsocket对象
QString temp = ui->textEdit_2->toPlainText();//获取textedit的文本
m_tcp->write(temp.toUtf8());
if(!temp.isEmpty() )
{
ui->textEdit->append(temp);
}
ui->textEdit_2->clear();
}
//客户端连接后响应
void serverwindow::ClientConnect()
{
//返回客户端的tcp
m_tcp = m_server->nextPendingConnection();
//打印下客户端的ip地址
QHostAddress addr = m_tcp->peerAddress();
ui->textEdit->append(addr.toString());
connect(m_tcp,&QTcpSocket::readyRead,this,&serverwindow::readData );
connect(m_tcp,&QTcpSocket::disconnected ,this,&serverwindow::Clientdisconnect );
}
//服务器接受数据
void serverwindow::readData()
{
QString temp = m_tcp->readAll();
if(!temp.isNull())
{
ui->textEdit->append(temp);
}
}
//客户端断开连接
void serverwindow::Clientdisconnect()
{
ui->textEdit->append("客户端断开连接");
m_tcp->close();
ui->pushButton->setEnabled(true);
ui->pushButton->setText("启动服务器");
}
//刷新串口
void serverwindow::on_pushButton_5_clicked()
{
//清空串口名
ui->comboBox->clear();
//遍历串口信息
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
ui->comboBox->addItem(info.portName());
}
}
//打开串口
void serverwindow::on_pushButton_3_clicked()
{
m_serial.setPortName(ui->comboBox->currentText());//设置串口名
m_serial.setBaudRate(ui->comboBox_2->currentText().toInt());//设置波特率
m_serial.open(QIODevice::ReadWrite);
if(m_serial.isOpen())
{
ui->label_7->setText("串口打开成功");
connect(&m_serial,&QSerialPort::readyRead,this,&serverwindow::messlot);
}
else
{
ui->label_7->setText("串口打开失败");
}
}
//串口接受信息的处理函数
void serverwindow::messlot()
{
QByteArray array = m_serial.readAll();
ui->textEdit->insertPlainText(array);
}
//关闭串口
void serverwindow::on_pushButton_4_clicked()
{
m_serial.close();
if(m_serial.isOpen())
{
ui->label_7->setText("串口关闭失败");
connect(&m_serial,&QSerialPort::readyRead,this,&serverwindow::messlot);
}
else
{
ui->label_7->setText("串口已关闭");
}
}
服务端ui界面:
2.客户端.h文件:需要在pro文件中加入serialport(TCP通信) sql(数据库) multimedia和 multimediawidgets(媒体,摄像头,音乐播放器)
①客户端界面1
#ifndef CLIENT1_H
#define CLIENT1_H
#include <QWidget>
#include <QSqlDataBase>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include<QMovie>
#include"client2.h"
#include"client3.h"
namespace Ui {
class client1;
}
class client1 : public QWidget
{
Q_OBJECT
public:
explicit client1(QWidget *parent = 0);
~client1();
private slots:
//显示密码
void on_checkBox_2_clicked();
//记住密码
void on_checkBox_clicked();
//登录
void on_pushButton_clicked();
//注册
void on_pushButton_2_clicked();
private:
Ui::client1 *ui;
};
static bool createConnection2()
{
QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE","connection1"); //创建指定连接名称的数据库
db1.setDatabaseName("data.db"); //设置数据库名称
if(!db1.open()){ //打开数据库
qDebug() << "cannot open the database";
return false;
}
QSqlQuery query1(db1); //对指定连接的数据库执行sql语句
query1.exec("create table data1(id varchar(20),mima varchar(20))");
query1.exec("insert into data1(id,mima) values('123456','123456')");
query1.exec("insert into data1(id,mima) values('654321','654321')");
query1.exec("insert into data1(id,mima) values('4444','4444')");
return true;
}
#endif // CLIENT1_H
界面1.c文件
#include "client1.h"
#include "ui_client1.h"
client1::client1(QWidget *parent) :
QWidget(parent),
ui(new Ui::client1)
{
ui->setupUi(this);
setWindowTitle(tr("登录"));
ui->lineEdit_2->setEchoMode(QLineEdit::Password);
QMovie *movie1 = new QMovie(":/new/prefix1/ies/ies1.gif");
ui->label_3->setMovie(movie1);
ui->label_3->setScaledContents(true);
movie1->start();
}
client1::~client1()
{
delete ui;
}
void client1::on_checkBox_clicked()
{
static int flag1 =1;
//-------------------------------------------------------------------------------------
//数据库处理
QSqlDatabase db1 = QSqlDatabase::database("connection1"); //得到指定连接名称数据库
QSqlQuery query1(db1);
query1.exec("select * from data1"); //执行sql语句
//---------------------------------------------------------------------------------------
if(flag1)
{