Skip to content

Commit

Permalink
The function of opening and closing serial port is realized in the up…
Browse files Browse the repository at this point in the history
…per computer.
  • Loading branch information
ZhuYanzhen1 committed Sep 9, 2021
1 parent 9216fd7 commit 2123f37
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 20 deletions.
17 changes: 2 additions & 15 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,7 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.

#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()

find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5 COMPONENTS Widgets SerialPort REQUIRED)

if(ANDROID)
add_library(monitor SHARED
Expand All @@ -42,4 +29,4 @@ else()
)
endif()

target_link_libraries(monitor PRIVATE Qt5::Widgets)
target_link_libraries(monitor PRIVATE Qt5::Widgets Qt5::SerialPort)
23 changes: 20 additions & 3 deletions tools/CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2021-09-09T22:08:37. -->
<!-- Written by QtCreator 4.11.1, 2021-09-10T00:30:15. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -55,10 +55,27 @@
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
<value type="bool" key="AutoTest.Framework.Boost">true</value>
<value type="bool" key="AutoTest.Framework.GTest">true</value>
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
<value type="QString">-fno-delayed-template-parsing</value>
</valuelist>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.TidyAndClazy</value>
<value type="int" key="ClangTools.ParallelJobs">8</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
Expand Down Expand Up @@ -328,7 +345,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/LaoZhu/AppData/Local/Temp/QtCreator-bUkvlM/qtc-cmake-snePBMlG</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/Desktop/Files/miniFOC/tools/build</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down Expand Up @@ -600,7 +617,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/LaoZhu/AppData/Local/Temp/QtCreator-bUkvlM/qtc-cmake-yrjZVxUw</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">D:/Desktop/Files/miniFOC/tools/build</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
42 changes: 42 additions & 0 deletions tools/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,52 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
refresh_serial_port();
ui->serial_baudrate_txt->setText("115200");
}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::set_serial_badurate(){
if(ui->serial_port_cb->currentText() == NULL || ui->serial_baudrate_txt->toPlainText() == NULL)
{
QMessageBox::information(NULL, "Error", "Please select port number and baudrate first!");
return false;
}
else
{
serial.setPortName(ui->serial_port_cb->currentText());
serial.setBaudRate(ui->serial_baudrate_txt->toPlainText().toInt());
return true;
}
}

void MainWindow::refresh_serial_port()
{
QList<QSerialPortInfo> serialPortinfo = QSerialPortInfo::availablePorts();
int count = serialPortinfo.count();
ui->serial_port_cb->clear();
for(int i = 0; i < count; i++)
ui->serial_port_cb->addItem(serialPortinfo.at(i).portName());
}

void MainWindow::on_open_btn_clicked()
{
if(serial.isOpen() == false){
if(set_serial_badurate() == true)
serial.open(QSerialPort::ReadWrite);
ui->open_btn->setText("Close");
}
else{
serial.close();
ui->open_btn->setText("Open");
}
}

void MainWindow::on_refresh_btn_clicked()
{
refresh_serial_port();
}
11 changes: 11 additions & 0 deletions tools/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMessageBox>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
Expand All @@ -15,7 +18,15 @@ class MainWindow : public QMainWindow
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_refresh_btn_clicked();

void on_open_btn_clicked();

private:
Ui::MainWindow *ui;
QSerialPort serial;
void refresh_serial_port();
bool set_serial_badurate();
};
#endif // MAINWINDOW_H
112 changes: 110 additions & 2 deletions tools/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,116 @@
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QWidget" name="centralwidget">
<widget class="QComboBox" name="serial_port_cb">
<property name="geometry">
<rect>
<x>90</x>
<y>10</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
</widget>
<widget class="QPushButton" name="refresh_btn">
<property name="geometry">
<rect>
<x>370</x>
<y>10</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Refresh</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>82</width>
<height>23</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>PortNum:</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>190</x>
<y>20</y>
<width>82</width>
<height>23</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Baudrate:</string>
</property>
</widget>
<widget class="QPushButton" name="open_btn">
<property name="geometry">
<rect>
<x>480</x>
<y>10</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Open</string>
</property>
</widget>
<widget class="QTextEdit" name="serial_baudrate_txt">
<property name="geometry">
<rect>
<x>260</x>
<y>10</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>10</pointsize>
</font>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
Expand Down

0 comments on commit 2123f37

Please sign in to comment.