diff --git a/HPPA/CommunicationInterfaceBase.cpp b/HPPA/CommunicationInterfaceBase.cpp new file mode 100644 index 0000000..b3c90b2 --- /dev/null +++ b/HPPA/CommunicationInterfaceBase.cpp @@ -0,0 +1,14 @@ +#include "CommunicationInterfaceBase.h" + +using namespace MotorParams; + +CommunicationInterfaceBase::CommunicationInterfaceBase(QObject* parent) + :QObject(parent) +{ + +} + +CommunicationInterfaceBase::~CommunicationInterfaceBase() +{ + +} diff --git a/HPPA/CommunicationInterfaceBase.h b/HPPA/CommunicationInterfaceBase.h new file mode 100644 index 0000000..94a9047 --- /dev/null +++ b/HPPA/CommunicationInterfaceBase.h @@ -0,0 +1,36 @@ +#pragma once +#include +#include +#include "motorParams.h" + +namespace MotorParams { + + struct TCPConnectionParams + { + QString serverIP; + int port; + }; +class CommunicationInterfaceBase :public QObject +{ + Q_OBJECT +public: + CommunicationInterfaceBase(QObject* parent = nullptr); + ~CommunicationInterfaceBase(); + + virtual bool connect2Motor() = 0; + //virtual void disconnect() = 0; + //virtual bool isConnected() const = 0; + + virtual int sendCommand(const QString command) = 0; + //virtual void sendCommandAsync(const QString& command) = 0; + virtual int recvData(QByteArray& dataRecv) = 0; + +signals: + void dataReceived(const QByteArray& data); + + void connected(); // 不管串口还是TCP,都会发这个信号 + void disconnected(); + void errorOccurred(QString msg); +}; + +} // namespace MotorParams diff --git a/HPPA/CommunicationViaTCP.cpp b/HPPA/CommunicationViaTCP.cpp new file mode 100644 index 0000000..6fa5937 --- /dev/null +++ b/HPPA/CommunicationViaTCP.cpp @@ -0,0 +1,95 @@ +锘#include "CommunicationViaTCP.h" + +using namespace MotorParams; + +CommunicationViaTCP::CommunicationViaTCP(MotorParams::TCPConnectionParams connectionParams, QObject* parent) + :MotorParams::CommunicationInterfaceBase(parent) +{ + m_bConnected = false; + m_tcpServer = new QTcpServer(this); + connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection())); + + m_tcpServer->listen(QHostAddress::Any, connectionParams.port); +} + +CommunicationViaTCP::~CommunicationViaTCP() +{ + m_tcpServer->close(); + delete m_tcpServer; + + //杩欎袱琛屼唬鐮佽鎶ラ敊锛屼负鍟ュ憿锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛 + //m_tcpSocket->disconnectFromHost(); + //delete m_tcpSocket; +} + +bool CommunicationViaTCP::connect2Motor() +{ + return true; +} + +void CommunicationViaTCP::onNewConnection() +{ + m_bConnected = true; + m_tcpSocket = m_tcpServer->nextPendingConnection(); + connect(m_tcpSocket, SIGNAL(disconnected()), this, SLOT(onTcpSocketDisconnected())); + + emit connected(); +} + +bool CommunicationViaTCP::isConnected() const +{ + return m_bConnected; +} + +//浠庢嫈鎺夊鎴风鐨勭數婧愶紙瀹㈡埛绔痬_tcpSocket鏂紑杩炴帴锛夊埌杩欎釜鍑芥暟琚皟鐢ㄦ湁寤惰繜锛屾墍浠ヨ繖涓嚱鏁拌皟鐢ㄤ篃鏈夊欢杩燂紝瀵艰嚧鎷旀帀鐢垫簮鍚庣殑涓灏忔鏃堕棿鍑芥暟isConnected()杩樻槸杩斿洖true +void CommunicationViaTCP::onTcpSocketDisconnected() +{ + int a = 1; + m_bConnected = false; + m_tcpSocket->deleteLater(); +} + +int CommunicationViaTCP::sendCommand(const QString cmd) +{ + if (!isConnected()) { + QString error = "No client connected"; + emit commandSendResult(-1, error); + qWarning() << error << "command:" << cmd; + return -1; + } + + qint64 bytesWritten = m_tcpSocket->write(cmd.toUtf8().data()); + m_tcpSocket->waitForBytesWritten(50); + + return bytesWritten; +} + +int CommunicationViaTCP::recvData(QByteArray& dataRecv) +{ + if (!isConnected()) { + QString error = "No client connected"; + return -1; + } + + dataRecv.clear(); + + QByteArray temp; + + temp = m_tcpSocket->readAll(); + dataRecv.append(temp); + + int counter = 0; + while (dataRecv.size() < 21) + { + counter++; + m_tcpSocket->waitForReadyRead(100); + temp = m_tcpSocket->readAll(); + dataRecv.append(temp); + + if (counter >= 5) + break; + } + //qDebug() << "Hex:" << dataRecv.toHex(); + + return dataRecv.size(); +} diff --git a/HPPA/CommunicationViaTCP.h b/HPPA/CommunicationViaTCP.h new file mode 100644 index 0000000..305d961 --- /dev/null +++ b/HPPA/CommunicationViaTCP.h @@ -0,0 +1,45 @@ +锘#pragma once +#include +#include +#include +#include +#include + +#include "CommunicationInterfaceBase.h" + +namespace MotorParams { + + class CommunicationViaTCP : + public CommunicationInterfaceBase + { + Q_OBJECT + + public: + CommunicationViaTCP(TCPConnectionParams connectionParams, QObject* parent = nullptr); + ~CommunicationViaTCP(); + + //缁ф壙鍩虹被 + bool connect2Motor(); + //void disconnect(); + //bool isConnected() const; + + int sendCommand(const QString cmd); + //void sendCommandAsync(const QString& command); + int recvData(QByteArray& dataRecv); + + private: + QTcpServer* m_tcpServer; + QTcpSocket* m_tcpSocket; + int m_iCommunicationProtocol; + + bool m_bConnected; + bool isConnected() const; + + public Q_SLOTS: + void onNewConnection(); + void onTcpSocketDisconnected(); + + signals: + void commandSendResult(int bytesWritten, const QString& error = QString()); + }; +} diff --git a/HPPA/HPPA.cpp b/HPPA/HPPA.cpp index 8233c81..23f0ac0 100644 --- a/HPPA/HPPA.cpp +++ b/HPPA/HPPA.cpp @@ -965,6 +965,11 @@ void HPPA::initControlTabwidget() m_pc->setWindowFlags(Qt::Widget); ui.controlTabWidget->addTab(m_pc, QString::fromLocal8Bit("鐢垫簮鎺у埗")); + //鐢垫簮鎺у埗3D + m_pc3D = new PowerControl3D(); + m_pc3D->setWindowFlags(Qt::Widget); + ui.controlTabWidget->addTab(m_pc3D, QString::fromLocal8Bit("鐢垫簮鎺у埗3D")); + //鏈烘鑷傛帶鍒 m_rac = new RobotArmControl(); connect(m_rac->robotController, SIGNAL(hsiRecordSignal(int)), this, SLOT(recordFromRobotArm(int))); @@ -1514,7 +1519,8 @@ void HPPA::create3DPlantPhenotypeScenario() m_tabManager->showTab(m_singleLensReflexCameraWindow); //m_tabManager->showTab(m_rgbCameraControlWindow); m_tabManager->showTab(m_adt); - m_tabManager->showTab(m_pc); + //m_tabManager->showTab(m_pc); + m_tabManager->showTab(m_pc3D); m_tabManager->showTab(m_tmc); m_view3DModelManager->switchScenario(View3DModelManager::ScenarioType::PlantPhenotype); diff --git a/HPPA/HPPA.h b/HPPA/HPPA.h index d5de2ee..19b1f28 100644 --- a/HPPA/HPPA.h +++ b/HPPA/HPPA.h @@ -85,6 +85,8 @@ #include "TimedDataCollection.h" +#include "PowerControl3D.h" + #define PI 3.1415926 QT_CHARTS_USE_NAMESPACE//QChartView 浣跨敤 闇瑕佸姞瀹忥紝 鍚﹀垯鏃犳硶浣跨敤 @@ -290,6 +292,7 @@ private: SingleLensReflexCameraWindow* m_singleLensReflexCameraWindow; adjustTable* m_adt; PowerControl* m_pc; + PowerControl3D* m_pc3D; RobotArmControl* m_rac; OneMotorControl* m_omc; TwoMotorControl* m_tmc; diff --git a/HPPA/HPPA.vcxproj b/HPPA/HPPA.vcxproj index 2da6df4..349d2e9 100644 --- a/HPPA/HPPA.vcxproj +++ b/HPPA/HPPA.vcxproj @@ -112,6 +112,8 @@ + + @@ -140,6 +142,7 @@ + @@ -189,6 +192,7 @@ + @@ -223,6 +227,8 @@ + + @@ -244,6 +250,7 @@ + diff --git a/HPPA/HPPA.vcxproj.filters b/HPPA/HPPA.vcxproj.filters index 87b4009..79bc464 100644 --- a/HPPA/HPPA.vcxproj.filters +++ b/HPPA/HPPA.vcxproj.filters @@ -238,6 +238,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + @@ -387,6 +396,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + @@ -496,6 +514,9 @@ Form Files + + Form Files + diff --git a/HPPA/PowerControl3D.cpp b/HPPA/PowerControl3D.cpp new file mode 100644 index 0000000..ef8b148 --- /dev/null +++ b/HPPA/PowerControl3D.cpp @@ -0,0 +1,64 @@ +#include "PowerControl3D.h" + +using namespace MotorParams; + +PowerControl3D::PowerControl3D(QWidget* parent) + : QDialog(parent) +{ + ui.setupUi(this); + + connect(ui.halogenLampPowerOn_btn, &QPushButton::clicked, this, &PowerControl3D::halogenLampPowerOn); + connect(ui.halogenLampPowerDown_btn, &QPushButton::clicked, this, &PowerControl3D::halogenLampPowerDown); + + connect(ui.d65LampPowerOn_btn, &QPushButton::clicked, this, &PowerControl3D::d65LampPowerOn); + connect(ui.d65LampPowerDown_btn, &QPushButton::clicked, this, &PowerControl3D::d65LampPowerDown); + + connect(ui.slrPowerOn_btn, &QPushButton::clicked, this, &PowerControl3D::slrPowerOn); + connect(ui.slrPowerDown_btn, &QPushButton::clicked, this, &PowerControl3D::slrPowerDown); + + MotorParams::TCPConnectionParams tcpConnectionParams6003; + tcpConnectionParams6003.port = 6003; + tcpConnectionParams6003.serverIP = "192.168.1.2"; + tcpServer6003 = new CommunicationViaTCP(tcpConnectionParams6003, this); + + MotorParams::TCPConnectionParams tcpConnectionParams6004; + tcpConnectionParams6004.port = 6004; + tcpConnectionParams6004.serverIP = "192.168.1.2"; + tcpServer6004 = new CommunicationViaTCP(tcpConnectionParams6004, this); +} + +PowerControl3D::~PowerControl3D() +{ + delete tcpServer6003; + delete tcpServer6004; +} + +void PowerControl3D::halogenLampPowerOn() +{ + tcpServer6003->sendCommand(R"({"key1":1,"type":"event"})"); +} + +void PowerControl3D::halogenLampPowerDown() +{ + tcpServer6003->sendCommand(R"({"key1":0,"type":"event"})"); +} + +void PowerControl3D::d65LampPowerOn() +{ + tcpServer6004->sendCommand(R"({"key1":1,"type":"event"})"); +} + +void PowerControl3D::d65LampPowerDown() +{ + tcpServer6004->sendCommand(R"({"key1":0,"type":"event"})"); +} + +void PowerControl3D::slrPowerOn() +{ + tcpServer6003->sendCommand(R"({"key2":1,"type":"event"})"); +} + +void PowerControl3D::slrPowerDown() +{ + tcpServer6003->sendCommand(R"({"key2":0,"type":"event"})"); +} diff --git a/HPPA/PowerControl3D.h b/HPPA/PowerControl3D.h new file mode 100644 index 0000000..7a3dee9 --- /dev/null +++ b/HPPA/PowerControl3D.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include +#include + +#include "ui_PowerControl3D.h" + +#include "CommunicationViaTCP.h" + +//using namespace MotorParams; + +class PowerControl3D : public QDialog +{ + Q_OBJECT + +public: + PowerControl3D(QWidget* parent = nullptr); + ~PowerControl3D(); + +public Q_SLOTS: + void halogenLampPowerOn(); + void halogenLampPowerDown(); + + void d65LampPowerOn(); + void d65LampPowerDown(); + + void slrPowerOn(); + void slrPowerDown(); + +signals: + //void powerOpened(); + +private: + Ui::PowerControl3D_UI ui; + + MotorParams::CommunicationViaTCP* tcpServer6003; + MotorParams::CommunicationViaTCP* tcpServer6004; +}; diff --git a/HPPA/PowerControl3D.ui b/HPPA/PowerControl3D.ui new file mode 100644 index 0000000..4dc9ae7 --- /dev/null +++ b/HPPA/PowerControl3D.ui @@ -0,0 +1,350 @@ + + + PowerControl3D_UI + + + + 0 + 0 + 493 + 369 + + + + + 0 + 0 + + + + PowerControl3D + + + QGroupBox +{ + border: 12px solid transparent; + /*border-top: 12px solid transparent; + border-right: 0px solid transparent; + border-bottom: 0px solid transparent; + border-left: 0px solid transparent;*/ + color: #ACCDFF; +} + +QPushButton +{ + /*width: 172px; + height: 56px;*/ + font: 19pt "鏂板畫浣"; + background-color: qlineargradient( + spread:pad, + x1:0.5, y1:0, x2:0.5, y2:1, + stop:0 #283D86, + stop:1 #0F1A40 + ); + color: white; + border: none; + padding: 8px 16px; + border-radius: 4px; +} +QPushButton:hover +{ + background-color: qlineargradient( + spread:pad, + x1:0, y1:0, x2:1, y2:0, + stop:0 #3A4875, + stop:1 #5F6B91 + ); +} +/* 鎸変笅鏃剁殑鏁堟灉 */ +QPushButton:pressed +{ + background-color: qlineargradient( + spread:pad, + x1:0, y1:0, x2:1, y2:0, + stop:0 #1A254F, + stop:1 #3A466B + ); + /* 鍙夛細娣诲姞涓嬪帇鏁堟灉 */ + padding-top: 9px; + padding-bottom: 7px; +} + + + + + + Qt::Vertical + + + + 20 + 66 + + + + + + + + Qt::Horizontal + + + + 63 + 20 + + + + + + + + + 0 + 0 + + + + 鍗ょ礌鐏 + + + + 0 + + + 10 + + + 0 + + + 0 + + + 18 + + + + + + 0 + 0 + + + + 鎵 寮 + + + + + + + + 0 + 0 + + + + 鍏 闂 + + + + + + + + + + Qt::Horizontal + + + + 63 + 20 + + + + + + + + Qt::Horizontal + + + + 63 + 20 + + + + + + + + + 0 + 0 + + + + D65鐏 + + + + 0 + + + 10 + + + 0 + + + 0 + + + 18 + + + + + + 0 + 0 + + + + 鎵 寮 + + + + + + + + 0 + 0 + + + + 鍏 闂 + + + + + + + + + + Qt::Horizontal + + + + 63 + 20 + + + + + + + + Qt::Horizontal + + + + 63 + 20 + + + + + + + + + 0 + 0 + + + + 鍗曞弽 + + + + 0 + + + 10 + + + 0 + + + 0 + + + 18 + + + + + + 0 + 0 + + + + 涓 鐢 + + + + + + + + 0 + 0 + + + + 涓 鐢 + + + + + + + + + + Qt::Horizontal + + + + 63 + 20 + + + + + + + + Qt::Vertical + + + + 20 + 65 + + + + + + + + + +