Skip to content

Commit

Permalink
修正了串口数据bug,加入简易手机客户端支持
Browse files Browse the repository at this point in the history
  • Loading branch information
r4ym0n committed Jul 3, 2017
1 parent dc0c973 commit 071103d
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 42 deletions.
161 changes: 136 additions & 25 deletions SmartHome/MainWindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,20 @@ MainWindow::MainWindow(QWidget *parent) :
mCamera = new cameraGet(this);
connect(mCamera, SIGNAL(pageChanged(int)), this, SLOT(widgeIndexChanged(int)));
connect(mCamera, SIGNAL(callVoice()), this, SLOT(on_pushButtonTTS_clicked()));

connect(mSerialPort, SIGNAL(readFinish()), this, SLOT(serialDataProcess()));// 连接串口数据与显示槽

initButtons();


Time_Dialog *time_dialog=new Time_Dialog(this);
// ui->verticalLayout->addWidget(time_dialog);

//状态栏添加时钟
ui->statusbar->addPermanentWidget(time_dialog);


status_dialog *dialogStatue=new status_dialog;
LightWindowDialog *dialogLight = new LightWindowDialog();
DialogSerial *dialogSerial = new DialogSerial();
qDebug() << "serial initial completed!";
PowerDialog *dialogPower = new PowerDialog(this);
qDebug() << "power initial completed";
//air_Dialog *dialogAir = new air_Dialog();

client = new QTcpSocket(this);

ui->stackedWidget->addWidget(dialogStatue);
ui->stackedWidget->addWidget(dialogSerial);
ui->stackedWidget->addWidget(dialogPower);
ui->stackedWidget->addWidget(dialogLight);
//ui->stackedWidget->addWidget(dialogAir);

ui->stackedWidget->setCurrentWidget(dialogStatue);
initButtons();
jsonInit();
uiInit();

startTimer(5000);


ui->statusbar->showMessage("warning!");

mCamera->show();
}
Expand All @@ -55,7 +38,6 @@ MainWindow::~MainWindow()




void MainWindow::initButtons() {
// 把按钮放在一个list里为了方便管理
buttons.append(ui->toolButton_1);
Expand Down Expand Up @@ -110,6 +92,22 @@ void MainWindow::on_toolButton_4_clicked()

void MainWindow::on_pushButtonTTS1_clicked()
{
client->connectToHost(QHostAddress("192.168.1.1"), 8383);
if (client->isOpen())
{
qDebug() << "connected";
}
if (client->isOpen())
{
QJsonDocument document;
document.setArray(json);
QByteArray byte_array = document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
qDebug() << json_str;

client->write(json_str.toLatin1().trimmed() + "\n");
client->close();
}

}

Expand All @@ -134,3 +132,116 @@ void MainWindow::widgeIndexChanged(int pageChange)
ui->stackedWidget->setCurrentIndex(ui->stackedWidget->currentIndex() + 1);
}
}

void MainWindow::jsonInit()
{
json.insert(0, "");
json.insert(1, "");
json.insert(2, "");
json.insert(3, "");
}


void MainWindow::timerEvent(QTimerEvent *)
{
client->connectToHost(QHostAddress("192.168.1.1"), 8383);
if (client->isOpen())
{
qDebug() << "connected";
}
if (client->isOpen())
{
QJsonDocument document;
document.setArray(json);
QByteArray byte_array = document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
qDebug() << json_str;

client->write(json_str.toLatin1().trimmed() + "\n");
client->close();
}
}



void MainWindow::serialDataProcess()
{

if (!serialReceiveData.isEmpty())
{

if ("elec" == serialReceiveData.at(0))
{

if ("p" == serialReceiveData.at(1))
{
qDebug() << serialReceiveData;


json.replace(0, serialReceiveData.at(2).toDouble());


}
if ("w" == serialReceiveData.at(1))
{
qDebug() << serialReceiveData;

//ui->lcdNumber_2->display(serialReceiveData.at(2).toDouble());
}
}

if ("fans" == serialReceiveData.at(0))
{
qDebug() << serialReceiveData;

//yFans.append(serialReceiveData.at(1).toDouble());
}

if ("temp" == serialReceiveData.at(0))
{
qDebug() << serialReceiveData;

json.replace(1, serialReceiveData.at(1).toDouble());
json.replace(2, serialReceiveData.at(2).toDouble());

}
if ("fans" == serialReceiveData.at(0))
{
json.replace(3, serialReceiveData.at(1).toDouble());
}
}

}

void MainWindow::uiInit()
{

Time_Dialog *time_dialog = new Time_Dialog(this);
// ui->verticalLayout->addWidget(time_dialog);

//状态栏添加时钟
ui->statusbar->addPermanentWidget(time_dialog);


status_dialog *dialogStatue = new status_dialog;
LightWindowDialog *dialogLight = new LightWindowDialog();
DialogSerial *dialogSerial = new DialogSerial();
qDebug() << "serial initial completed!";
PowerDialog *dialogPower = new PowerDialog(this);
qDebug() << "power initial completed";
//air_Dialog *dialogAir = new air_Dialog();


ui->stackedWidget->addWidget(dialogStatue);
ui->stackedWidget->addWidget(dialogSerial);
ui->stackedWidget->addWidget(dialogPower);
ui->stackedWidget->addWidget(dialogLight);
//ui->stackedWidget->addWidget(dialogAir);

ui->stackedWidget->setCurrentWidget(dialogStatue);



ui->statusbar->showMessage("warning!");

}
17 changes: 14 additions & 3 deletions SmartHome/MainWindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QList>

#include <QTimer>
#include <QEvent>
#include <QTcpSocket>

#include "SerialPort/dialogserial.h"
#include "misc/timer/time_dialog.h"
Expand All @@ -17,12 +19,14 @@
#include "StatueDialog/status_dialog.h"
#include "misc/Camera/cameraget.h"

class QToolButton;

#include "SerialPort/serial_data.h"

extern SerialPort *mSerialPort;
extern QList<QByteArray> serialReceiveData;
extern QStringList serialSendData;


class QToolButton;
namespace Ui {
class MainWindow;
}
Expand All @@ -45,6 +49,7 @@ private slots:

void on_toolButton_1_clicked();

void serialDataProcess();
void on_toolButton_4_clicked();

void on_pushButtonTTS1_clicked();
Expand All @@ -58,8 +63,14 @@ private slots:
cameraGet *mCamera;
// TTSDialog mTTSDialog;
QList<QToolButton *> buttons;
QJsonArray json;
QTcpSocket *client;


void jsonInit();
void timerEvent(QTimerEvent *);
void initButtons();
void uiInit();
};

#endif // MAINWINDOW_H
12 changes: 7 additions & 5 deletions SmartHome/PowerDialog/power_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PowerDialog::PowerDialog(QWidget *parent) :
// connect(mTimer,SIGNAL(timeout()),this,SLOT(plotRefrash()));
// mTimer->start(200);

startTimer(50);
startTimer(500);
qDebug() << "power initial completed";
// cameraGet *mCamera = new cameraGet();

Expand Down Expand Up @@ -162,8 +162,10 @@ void PowerDialog::curveTempRefrsh()

ui->qwtPlot_3->setAxisScale(QwtPlot::xBottom, xTime.last() - 300, xTime.last());

yTemp2.append(rand()%10);
yTemp1.append(rand()%10);
yTemp1.append(yTemp1.last());
yTemp2.append(yTemp2.last());
//yTemp2.append(rand()%10);
//yTemp1.append(rand()%10);
//重新加载数据
curveTemp1.setSamples(xTime, yTemp1);
curveTemp2.setSamples(xTime, yTemp2);
Expand Down Expand Up @@ -196,8 +198,8 @@ void PowerDialog::curveFansRefrash()


//最后一位为新数据(这里为随机数模拟)
//yFans.append(yFans.last());
yFans.append(rand()%10);
yFans.append(yFans.last());
//yFans.append(rand()%10);


//重新加载数据
Expand Down
4 changes: 3 additions & 1 deletion SmartHome/SerialPort/serial_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void SerialPort::slotReadData()
byteArray.append(tmp); //对收到的数据进行缓冲
qDebug() << "buff is "<< byteArray;

if(!byteArray.contains('#') || !byteArray.contains('\n')){ //检验是否为完整数据包
if(!byteArray.contains('#') || !byteArray.contains('\n') || !byteArray.contains('*')){ //检验是否为完整数据包
waitForReadyRead(10); //不是就等待,并返回
return;
}
Expand All @@ -50,7 +50,9 @@ void SerialPort::slotReadData()
QList<QByteArray> list = byteArray.trimmed().split('*');// 去掉最后作为结束符的\n
//分割*
qDebug() << "finally" << byteArray.trimmed();

serialReceiveData = list;
serialReceiveData.append(0);
byteArray.clear();
emit readFinish();
}
Expand Down
15 changes: 15 additions & 0 deletions SmartHome/StatueDialog/status_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,24 @@ void status_dialog::serialDataProcess()
}

}
if ("siren" == serialReceiveData.at(0))
{
if (serialReceiveData.at(1) == "1")
{
ui->label_3->setText(" 安全.");
}
else
{
ui->label_3->setText(" 不安全.");

}

}
}
}



void status_dialog::writeWindData(QString on_off)
{
serialSendData.clear();
Expand Down
Loading

0 comments on commit 071103d

Please sign in to comment.