Skip to content

Commit

Permalink
The unpacking program of medium capacity data transfer protocol was s…
Browse files Browse the repository at this point in the history
…uccessfully transplanted to monitor tool.
  • Loading branch information
ZhuYanzhen1 committed Sep 9, 2021
1 parent 21fd211 commit f26872f
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 54 deletions.
17 changes: 2 additions & 15 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Widgets SerialPort REQUIRED)

if(ANDROID)
add_library(monitor SHARED
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
else()
add_executable(monitor
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
endif()
add_executable(monitor main.cpp mainwindow.cpp mainwindow.h mainwindow.ui
mdtp/decrypt.cpp mdtp/encrypt.cpp)

target_link_libraries(monitor PRIVATE Qt5::Widgets Qt5::SerialPort)
30 changes: 15 additions & 15 deletions tools/CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2021-09-10T00:30:15. -->
<!-- Written by QtCreator 4.11.1, 2021-09-10T01:16:40. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{5a1bca82-26f7-43e4-95dc-5fa02d3dc7ae}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">1</value>
<value type="int">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
Expand Down Expand Up @@ -81,9 +81,9 @@
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win32_mingw73_kit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win64_mingw73_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
Expand Down Expand Up @@ -138,7 +138,7 @@
<value type="QString">CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}</value>
<value type="QString">QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}</value>
</valuelist>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_32_bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_64_bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
Expand Down Expand Up @@ -181,7 +181,7 @@
<value type="QString">CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}</value>
<value type="QString">QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}</value>
</valuelist>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_32_bit-RelWithDebInfo</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_64_bit-RelWithDebInfo</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
Expand Down Expand Up @@ -224,7 +224,7 @@
<value type="QString">CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}</value>
<value type="QString">QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}</value>
</valuelist>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_32_bit-MinSizeRel</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_64_bit-MinSizeRel</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
Expand Down Expand Up @@ -353,9 +353,9 @@
<data>
<variable>ProjectExplorer.Project.Target.1</variable>
<valuemap type="QVariantMap">
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win64_mingw73_kit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 32-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win32_mingw73_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
Expand Down Expand Up @@ -410,7 +410,7 @@
<value type="QString">CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}</value>
<value type="QString">QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}</value>
</valuelist>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_64_bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_32_bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
Expand Down Expand Up @@ -453,7 +453,7 @@
<value type="QString">CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}</value>
<value type="QString">QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}</value>
</valuelist>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_64_bit-RelWithDebInfo</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_32_bit-RelWithDebInfo</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
Expand Down Expand Up @@ -496,7 +496,7 @@
<value type="QString">CMAKE_PREFIX_PATH:STRING=%{Qt:QT_INSTALL_PREFIX}</value>
<value type="QString">QT_QMAKE_EXECUTABLE:STRING=%{Qt:qmakeExecutable}</value>
</valuelist>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_64_bit-MinSizeRel</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">D:/Desktop/Files/miniFOC/tools/build-monitor-Desktop_Qt_5_14_2_MinGW_32_bit-MinSizeRel</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="QString" key="CMakeProjectManager.MakeStep.AdditionalArguments"></value>
Expand Down Expand Up @@ -617,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">D:/Desktop/Files/miniFOC/tools/build</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default"></value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
62 changes: 40 additions & 22 deletions tools/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,73 @@

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
, ui(new Ui::MainWindow){
serial = new QSerialPort();
ui->setupUi(this);
refresh_serial_port();
ui->serial_baudrate_txt->setText("115200");
}

MainWindow::~MainWindow()
{
MainWindow::~MainWindow(){
if (serial->isOpen())
serial->close();
delete serial;
delete ui;
}

bool MainWindow::set_serial_badurate(){
if(ui->serial_port_cb->currentText() == NULL || ui->serial_baudrate_txt->toPlainText() == NULL)
{
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());
} else{
serial->setPortName(ui->serial_port_cb->currentText());
serial->setBaudRate(ui->serial_baudrate_txt->toPlainText().toInt());
serial->setDataBits(QSerialPort::Data8);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
return true;
}
}

void MainWindow::refresh_serial_port()
{
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");
void MainWindow::mdtp_callback_handler(unsigned char pid, const unsigned char *data){
qDebug() << "pack:" << pid << " data:" << data[0] << data[1] << data[2] << data[3]
<< data[4] << data[5] << data[6] << data[7];
}

void MainWindow::serial_received(){
QByteArray info = serial->readAll();
char *buffer = info.data();
for (int counter = 0; counter < info.length(); counter++)
mdtp_receive_handler(buffer[counter]);
}

void MainWindow::on_open_btn_clicked(){
if(serial->isOpen() == false){
if(set_serial_badurate() == true){
serial->open(QSerialPort::ReadWrite);
ui->open_btn->setText("Close");
ui->serial_port_cb->setEnabled(false);
ui->serial_baudrate_txt->setEnabled(false);
connect(serial,SIGNAL(readyRead()),this,SLOT(serial_received()));
}
}
else{
serial.close();
serial->close();
ui->open_btn->setText("Open");
ui->serial_port_cb->setEnabled(true);
ui->serial_baudrate_txt->setEnabled(true);
}
}

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

#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
Expand All @@ -20,13 +22,18 @@ class MainWindow : public QMainWindow

private slots:
void on_refresh_btn_clicked();

void on_open_btn_clicked();

public slots:
void serial_received();

private:
Ui::MainWindow *ui;
QSerialPort serial;
QSerialPort *serial;
void refresh_serial_port();
bool set_serial_badurate();
void mdtp_receive_handler(unsigned char data);
void mdtp_data_transmit(unsigned char pid, const unsigned char *data);
void mdtp_callback_handler(unsigned char pid, const unsigned char *data);
};
#endif // MAINWINDOW_H
10 changes: 10 additions & 0 deletions tools/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@
</font>
</property>
</widget>
<widget class="QTextEdit" name="test_txt">
<property name="geometry">
<rect>
<x>40</x>
<y>70</y>
<width>681</width>
<height>451</height>
</rect>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
Expand Down
88 changes: 88 additions & 0 deletions tools/mdtp/decrypt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// Created by Lao·Zhu on 2021/9/4.
//

#include "mainwindow.h"
#include <string>

volatile static unsigned char mdtp_receive_status = 0;
volatile static unsigned char mdtp_receive_number_counter = 0;
static unsigned char mdtp_receive_data_buffer[10] = {0};

/*!
\brief medium capacity data transmission protocol unpacking handler
\param[in] data: data received from general receive function
\param[out] none
\retval none
*/
void MainWindow::mdtp_receive_handler(unsigned char data) {
/* data receiving finite state machine */
switch (mdtp_receive_status) {
case 0:
/* judge whether the packet header is received */
if (data == 0xff) {
/* enter the receive state */
mdtp_receive_status = 1;
/* clear receive array counter */
mdtp_receive_number_counter = 0;
/* clear the value in the buffer array */
memset(mdtp_receive_data_buffer, 0x00, sizeof(mdtp_receive_data_buffer));
}
break;
case 1:
/* judge whether the end of the packet is mistakenly recognized as the header */
if (data == 0xff && mdtp_receive_number_counter != 0) {
/* an unexpected data had been received */
/* reset to receive start of package state */
mdtp_receive_status = 0;
/* clear receive array counter */
mdtp_receive_number_counter = 0;
/* clear the value in the buffer array */
memset(mdtp_receive_data_buffer, 0x00, sizeof(mdtp_receive_data_buffer));
} else if (data != 0xff) {
/* judge whether the reception is completed or the error data is received */
if (mdtp_receive_number_counter != 10) {
/* receive the data into the array in turn */
mdtp_receive_data_buffer[mdtp_receive_number_counter] = data;
mdtp_receive_number_counter = mdtp_receive_number_counter + 1;
if (mdtp_receive_number_counter == 10)
mdtp_receive_status = 2;
}
}
break;
case 2:
if (data == 0xff) {
/* ready to receive the next packet */
mdtp_receive_status = 0;
/* verify whether the pid byte is correct*/
if ((mdtp_receive_data_buffer[0] >> 4) == (~mdtp_receive_data_buffer[0] & 0x0f)) {
unsigned char tmp_rcv_buffer[8], counter = 0;
/* judge whether the package content is all 0xff */
if (mdtp_receive_data_buffer[1] == 0xa5 && mdtp_receive_data_buffer[9] == 0xa5)
/* fill all bytes with 0xff */
memset(tmp_rcv_buffer, 0xff, sizeof(tmp_rcv_buffer));
else {
/* traverse the data byte to be adjusted */
for (; counter < 8; ++counter)
/* judge whether the adjustment bit is 1 */
if (((mdtp_receive_data_buffer[9] >> counter) & 0x01) == 0x01)
/* fill the data byte with 0xff */
tmp_rcv_buffer[counter] = 0xff;
else
/* copy data directly to the receiving array */
tmp_rcv_buffer[counter] = mdtp_receive_data_buffer[counter + 1];
}
/* call user callback function to complete the next step */
mdtp_callback_handler(mdtp_receive_data_buffer[0] >> 4, tmp_rcv_buffer);
}
} else { /* an unexpected data had been received */
/* reset to receive start of package state */
mdtp_receive_status = 0;
/* clear receive array counter */
mdtp_receive_number_counter = 0;
/* clear the value in the buffer array */
memset(mdtp_receive_data_buffer, 0x00, sizeof(mdtp_receive_data_buffer));
}
break;
}
}
Loading

0 comments on commit f26872f

Please sign in to comment.