|
|
|
|
@ -5,7 +5,6 @@ VisionController::VisionController(QWidget *parent)
|
|
|
|
|
: QMainWindow(parent)
|
|
|
|
|
{
|
|
|
|
|
ui.setupUi(this);
|
|
|
|
|
|
|
|
|
|
m_tcpServerPtr = nullptr;
|
|
|
|
|
m_tcpServerPtr = new TcpServer(this);
|
|
|
|
|
QString path = QApplication::applicationDirPath();
|
|
|
|
|
@ -17,8 +16,8 @@ VisionController::VisionController(QWidget *parent)
|
|
|
|
|
ui.LineEdit_IP->setEnabled(false);
|
|
|
|
|
if (m_tcpServerPtr->startConnect(serverIP, serverPort))
|
|
|
|
|
{
|
|
|
|
|
//QString msg = QString("服务器:1%: 2% 启动成功").arg(serverIp).arg(port);
|
|
|
|
|
QString showMsg = QString("【服务器:%1: %2 启动成功】").arg(serverIP).arg(serverPort);
|
|
|
|
|
qInfo() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
ui.ButtonStart->setEnabled(false);
|
|
|
|
|
ui.ButtonStop->setEnabled(true);
|
|
|
|
|
@ -26,6 +25,7 @@ VisionController::VisionController(QWidget *parent)
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QString showMsg = QString("【服务器:%1: %2 启动失败】").arg(serverIP).arg(serverPort);
|
|
|
|
|
qWarning() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
}
|
|
|
|
|
connect(ui.ButtonStart, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
|
|
|
|
|
@ -36,22 +36,43 @@ VisionController::VisionController(QWidget *parent)
|
|
|
|
|
connect(m_tcpServerPtr, &TcpServer::sgReceived, this, &VisionController::receivedData);
|
|
|
|
|
connect(m_tcpServerPtr, &TcpServer::sgDisconnected, this, &VisionController::onDisconnected);
|
|
|
|
|
m_ioControllerPtr = new IOController();
|
|
|
|
|
unsigned int serialNum = 2;
|
|
|
|
|
if (!m_ioControllerPtr->controllerInitialization(serialNum))
|
|
|
|
|
unsigned int comNum = 2;
|
|
|
|
|
if (!readComSetting(path, comNum))
|
|
|
|
|
{
|
|
|
|
|
QString showMsg = QString("未能读取到Com配置文件");
|
|
|
|
|
qWarning() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
}
|
|
|
|
|
if (!m_ioControllerPtr->controllerInitialization(comNum))
|
|
|
|
|
{
|
|
|
|
|
QString showMsg = QString("串口Com%1初始化失败").arg(serialNum);
|
|
|
|
|
QString showMsg = QString("串口Com%1初始化失败").arg(comNum);
|
|
|
|
|
qWarning() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QString showMsg = QString("串口Com%1初始化成功").arg(serialNum);
|
|
|
|
|
QString showMsg = QString("串口Com%1初始化成功").arg(comNum);
|
|
|
|
|
qInfo() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
}
|
|
|
|
|
qInfo() << "软件开启成功";
|
|
|
|
|
}
|
|
|
|
|
VisionController::~VisionController()
|
|
|
|
|
{
|
|
|
|
|
if (m_ioControllerPtr)
|
|
|
|
|
{
|
|
|
|
|
delete m_ioControllerPtr;
|
|
|
|
|
//m_ioControllerPtr = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_tcpServerPtr)
|
|
|
|
|
{
|
|
|
|
|
delete m_tcpServerPtr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bool VisionController::readServerConfig(QString path, QString& serverIP, quint16& port)
|
|
|
|
|
{
|
|
|
|
|
QString filePath = path + "\\..\\config\\tcpServer.json";
|
|
|
|
|
QString filePath = path + "\\config\\tcpServer.json";
|
|
|
|
|
QFile jsonFile(filePath);
|
|
|
|
|
if (!jsonFile.exists())
|
|
|
|
|
{
|
|
|
|
|
@ -82,7 +103,7 @@ bool VisionController::readServerConfig(QString path, QString& serverIP, quint16
|
|
|
|
|
}
|
|
|
|
|
bool VisionController::writeServerConfig(QString path, QString serverIP, quint16 serverPort)
|
|
|
|
|
{
|
|
|
|
|
QString filePath = path + "\\..\\config\\tcpServer.json";
|
|
|
|
|
QString filePath = path + "\\config\\tcpServer.json";
|
|
|
|
|
QFile jsonFile(filePath);
|
|
|
|
|
if (!jsonFile.open(QIODevice::WriteOnly))
|
|
|
|
|
{
|
|
|
|
|
@ -100,33 +121,49 @@ bool VisionController::writeServerConfig(QString path, QString serverIP, quint16
|
|
|
|
|
jsonFile.close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
VisionController::~VisionController()
|
|
|
|
|
|
|
|
|
|
bool VisionController::readComSetting(const QString &path, unsigned int& comNum)
|
|
|
|
|
{
|
|
|
|
|
if (m_ioControllerPtr)
|
|
|
|
|
QString filePath = path + "\\config\\comSetting.json";
|
|
|
|
|
QFile comSettingFile(filePath);
|
|
|
|
|
if (!comSettingFile.exists())
|
|
|
|
|
{
|
|
|
|
|
delete m_ioControllerPtr;
|
|
|
|
|
//m_ioControllerPtr = nullptr;
|
|
|
|
|
qDebug() << "文件:" << filePath << "不存在!";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_tcpServerPtr)
|
|
|
|
|
if (!comSettingFile.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
|
{
|
|
|
|
|
delete m_tcpServerPtr;
|
|
|
|
|
qDebug() << "文件:" << filePath << "打开失败!";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
QByteArray dataArray = comSettingFile.readAll();
|
|
|
|
|
comSettingFile.close();
|
|
|
|
|
QJsonParseError jsonError;
|
|
|
|
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(dataArray, &jsonError);
|
|
|
|
|
if (jsonError.error != QJsonParseError::NoError)
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "文件:" << filePath << "格式错误!";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
QJsonObject jsonObject = jsonDocument.object();
|
|
|
|
|
comNum = jsonObject.value("Com").toInt();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_SLOT void VisionController::onButtonClicked()
|
|
|
|
|
{
|
|
|
|
|
QString strObj = sender()->objectName();
|
|
|
|
|
QString serverIp = ui.LineEdit_IP->text();
|
|
|
|
|
quint16 port = ui.LineEdit_Port->text().toUInt();
|
|
|
|
|
QString path = QApplication::applicationDirPath();
|
|
|
|
|
if (strObj == "ButtonStart")
|
|
|
|
|
{
|
|
|
|
|
QString serverIp = ui.LineEdit_IP->text();
|
|
|
|
|
quint16 port = ui.LineEdit_Port->text().toUInt();
|
|
|
|
|
QString path = QApplication::applicationDirPath();
|
|
|
|
|
writeServerConfig(path, serverIp, port);
|
|
|
|
|
if (m_tcpServerPtr->startConnect(serverIp, port))
|
|
|
|
|
{
|
|
|
|
|
//QString msg = QString("服务器:1%: 2% 启动成功").arg(serverIp).arg(port);
|
|
|
|
|
QString showMsg = QString("【服务器:%1: %2 启动成功】").arg(serverIp).arg(port);
|
|
|
|
|
qInfo() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
ui.ButtonStart->setEnabled(false);
|
|
|
|
|
ui.ButtonStop->setEnabled(true);
|
|
|
|
|
@ -134,6 +171,7 @@ Q_SLOT void VisionController::onButtonClicked()
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QString showMsg = QString("【服务器:%1: %2 启动失败】").arg(serverIp).arg(port);
|
|
|
|
|
qWarning() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -141,6 +179,7 @@ Q_SLOT void VisionController::onButtonClicked()
|
|
|
|
|
{
|
|
|
|
|
if (m_tcpServerPtr != nullptr)
|
|
|
|
|
{
|
|
|
|
|
qInfo() << QString("【关闭服务器:%1:%2】").arg(serverIp).arg(port);
|
|
|
|
|
m_tcpServerPtr->onCloseConnect();
|
|
|
|
|
ui.ButtonStart->setEnabled(true);
|
|
|
|
|
ui.ButtonStop->setEnabled(false);
|
|
|
|
|
@ -155,12 +194,14 @@ Q_SLOT void VisionController::onButtonClicked()
|
|
|
|
|
Q_SLOT void VisionController::newConnected(QString ip, quint16 port)
|
|
|
|
|
{
|
|
|
|
|
QString showMsg = QString("【客户端上线:%1: %2】").arg(ip).arg(port);
|
|
|
|
|
qInfo() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
}
|
|
|
|
|
Q_SLOT void VisionController::receivedData(QString ip, quint16 port, const QByteArray& dataByte)
|
|
|
|
|
{
|
|
|
|
|
QString showMsg = QString("【客户端:%1: %2】:").arg(ip).arg(port);
|
|
|
|
|
showMsg.append(dataByte);
|
|
|
|
|
qInfo() << showMsg;
|
|
|
|
|
ui.textLog->appendPlainText(showMsg);
|
|
|
|
|
QString msg = QString(dataByte);
|
|
|
|
|
|
|
|
|
|
@ -179,11 +220,13 @@ Q_SLOT void VisionController::receivedData(QString ip, quint16 port, const QByte
|
|
|
|
|
if (state == 0)
|
|
|
|
|
{
|
|
|
|
|
QString errorMsg = QString("【端口Port%1 关闭失败!】").arg(port);
|
|
|
|
|
qWarning() << errorMsg;
|
|
|
|
|
ui.textLog->appendPlainText(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
else if (state == 1)
|
|
|
|
|
{
|
|
|
|
|
QString errorMsg = QString("【端口Port%1 打开失败!】").arg(port);
|
|
|
|
|
qWarning() << errorMsg;
|
|
|
|
|
ui.textLog->appendPlainText(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -192,11 +235,13 @@ Q_SLOT void VisionController::receivedData(QString ip, quint16 port, const QByte
|
|
|
|
|
if (state == 0)
|
|
|
|
|
{
|
|
|
|
|
QString successMsg = QString("【端口port%1 关闭成功!】").arg(port);
|
|
|
|
|
qInfo() << successMsg;
|
|
|
|
|
ui.textLog->appendPlainText(successMsg);
|
|
|
|
|
}
|
|
|
|
|
else if (state == 1)
|
|
|
|
|
{
|
|
|
|
|
QString successMsg = QString("【端口port%1 打开成功!】").arg(port);
|
|
|
|
|
qInfo() << successMsg;
|
|
|
|
|
ui.textLog->appendPlainText(successMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -207,11 +252,13 @@ Q_SLOT void VisionController::receivedData(QString ip, quint16 port, const QByte
|
|
|
|
|
if (m_ioControllerPtr->getPortState(port, portState))
|
|
|
|
|
{
|
|
|
|
|
QString successMsg = QString("【端口port%1的状态为:%2】").arg(port).arg(portState);
|
|
|
|
|
qInfo() << QString("查询端口port%1的状态,端口port%1的状态为:%2").arg(port).arg(portState);
|
|
|
|
|
ui.textLog->appendPlainText(successMsg);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QString errorMsg = QString("【端口port%1状态获取失败!】").arg(port);
|
|
|
|
|
qWarning() << QString("获取端口port%1状态失败!").arg(port);
|
|
|
|
|
ui.textLog->appendPlainText(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -220,12 +267,28 @@ Q_SLOT void VisionController::receivedData(QString ip, quint16 port, const QByte
|
|
|
|
|
Q_SLOT void VisionController::onDisconnected(QString ip, quint16 port)
|
|
|
|
|
{
|
|
|
|
|
QString msg = QString("【客户端:%1:%2断开连接!】").arg(ip).arg(port);
|
|
|
|
|
qInfo() << msg;
|
|
|
|
|
ui.textLog->appendPlainText(msg);
|
|
|
|
|
}
|
|
|
|
|
void VisionController::closeEvent(QCloseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QString serverIp = ui.LineEdit_IP->text();
|
|
|
|
|
quint16 port = ui.LineEdit_Port->text().toUInt();
|
|
|
|
|
QString path = QApplication::applicationDirPath();
|
|
|
|
|
writeServerConfig(path, serverIp, port);
|
|
|
|
|
//QMessageBox msgBox;
|
|
|
|
|
//setWindowFlags(Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);
|
|
|
|
|
//msgBox.setText("提示");
|
|
|
|
|
//msgBox.setInformativeText("确认退出?");
|
|
|
|
|
//msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
|
|
|
|
|
//msgBox.setDefaultButton(QMessageBox::Ok);
|
|
|
|
|
//int ret = msgBox.exec();
|
|
|
|
|
//if (ret == QMessageBox::Ok) {
|
|
|
|
|
// //若用户点击确认,则接收这个事件,当前窗口会关闭
|
|
|
|
|
// QString serverIp = ui.LineEdit_IP->text();
|
|
|
|
|
// quint16 port = ui.LineEdit_Port->text().toUInt();
|
|
|
|
|
// QString path = QApplication::applicationDirPath();
|
|
|
|
|
// writeServerConfig(path, serverIp, port);
|
|
|
|
|
// event->accept();
|
|
|
|
|
//}
|
|
|
|
|
//else {
|
|
|
|
|
// //若用户点击取消,则忽略这个事件,当前窗口不会关闭
|
|
|
|
|
// event->ignore();
|
|
|
|
|
//}
|
|
|
|
|
}
|