Object Detection  5.0
Image Processing Using Qt and Opencv
serialmainwindow.cpp
1 #include "serialmainwindow.h"
2 #include "ui_serialmainwindow.h"
3 
4 #include <QTime>
5 #include <QDebug>
6 #include <iostream>
7 #include <QFileDialog>
8 #include <QProcess>
9 #include <QMetaEnum>
10 SerialMainWindow::SerialMainWindow(QWidget *parent) :
11  QMainWindow(parent),
12  ui(new Ui::SerialMainWindow)
13 {
14  ui->setupUi(this);
15  m_model = new SerialPortModel(this);
16  ui->_serialTableView->setModel(m_model);
17  ui->_serialTableView->setSelectionMode(QTableView::SingleSelection);
18  ui->_serialTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
19 }
20 
21 SerialMainWindow::~SerialMainWindow()
22 {
23  delete ui;
24 }
25 
26 void SerialMainWindow::refreshModel()
27 {
28  m_model->refreshModel();
29 }
30 
31 void SerialMainWindow::on__refreshPushButton_clicked()
32 {
33  refreshModel();
34  ui->_serialTableView->reset();
35 }
36 
37 void SerialMainWindow::on__portPushButton_clicked()
38 {
39  QModelIndex in = ui->_serialTableView->currentIndex();
40  if(!in.isValid())
41  return;
42  m_port = in.data(SerialPortModel::PORT).value<QSerialPort*>();
43  m_port->open(QIODevice::ReadWrite);
44  connect(m_port, SIGNAL(readyRead()), this, SLOT(changeText()));
45 }
46 
47 void SerialMainWindow::on__writePushButton_clicked()
48 {
49  if(m_port->isWritable() && m_port->isOpen())
50  {
51  m_port->flush();
52  m_port->write(ui->_dataLineEdit->text().toLocal8Bit().data());
53 
54  }
55 }
56 
57 void SerialMainWindow::changeText()
58 {
59  if(m_port->isReadable() && m_port->isOpen()){
60  QString string("");
61  if(ui->_formatTimeCheckBox->isChecked()){
62  string.append("(");
63  string.append(QTime::currentTime().toString("hh:mm:ss:zzz"));
64  string.append(") :: ");
65  }
66  QByteArray arr = m_port->readAll();
67  string.append(arr);
68  ui->_outTextEdit->append(string);
69  }
70 }
71 
72 void SerialMainWindow::on__attachProcessPushButton_2_clicked()
73 {
74  QString exect = QFileDialog::getOpenFileName(this, "Process", QDir::currentPath(), tr("executable (*.exe);; All Files (*.*)"));
75  setChildProcess(new QProcess(this));
76  QStringList args = ui->lineEdit->text().split(" ");
77  childProcess()->start(exect, args);
78 
79 }
80 
81 void SerialMainWindow::redirecteInput()
82 {
83  m_childProcess->write(m_port->readAll());
84 }
85 
86 void SerialMainWindow::redirecteOutput()
87 {
88  m_port->write(m_childProcess->readAllStandardOutput());
89 }
90 
91 void SerialMainWindow::childFinished(int i)
92 {
93  ui->_processStateLabel->setText(QString("finsihed with code %1").arg(i));
94 }
95 
96 void SerialMainWindow::childStatus(QProcess::ProcessState state)
97 {
98  auto en = QMetaEnum::fromType<QProcess::ProcessState>();
99  ui->_processStateLabel->setText(en.valueToKey(state));
100 }
101 
102 QProcess *SerialMainWindow::childProcess() const
103 {
104  return m_childProcess;
105 }
106 
107 void SerialMainWindow::setChildProcess(QProcess *childProcess)
108 {
109  if(m_childProcess){
110  m_childProcess->disconnect();
111  delete m_childProcess;
112  }
113  m_childProcess = childProcess;
114  connect(m_childProcess, SIGNAL(finished(int)), this, SLOT(childFinished(int)));
115  connect(m_childProcess, SIGNAL(finished(int)), m_childProcess, SLOT(deleteLater()));
116  connect(m_childProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(redirecteOutput()));
117  connect(m_port, SIGNAL(readyRead()), this, SLOT(redirecteInput()));
118  connect(m_childProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(childStatus(QProcess::ProcessState)));
119 
120 }