1 #include "serialmainwindow.h" 2 #include "ui_serialmainwindow.h" 10 SerialMainWindow::SerialMainWindow(QWidget *parent) :
16 ui->_serialTableView->setModel(m_model);
17 ui->_serialTableView->setSelectionMode(QTableView::SingleSelection);
18 ui->_serialTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
21 SerialMainWindow::~SerialMainWindow()
26 void SerialMainWindow::refreshModel()
28 m_model->refreshModel();
31 void SerialMainWindow::on__refreshPushButton_clicked()
34 ui->_serialTableView->reset();
37 void SerialMainWindow::on__portPushButton_clicked()
39 QModelIndex in = ui->_serialTableView->currentIndex();
42 m_port = in.data(SerialPortModel::PORT).value<QSerialPort*>();
43 m_port->open(QIODevice::ReadWrite);
44 connect(m_port, SIGNAL(readyRead()),
this, SLOT(changeText()));
47 void SerialMainWindow::on__writePushButton_clicked()
49 if(m_port->isWritable() && m_port->isOpen())
52 m_port->write(ui->_dataLineEdit->text().toLocal8Bit().data());
57 void SerialMainWindow::changeText()
59 if(m_port->isReadable() && m_port->isOpen()){
61 if(ui->_formatTimeCheckBox->isChecked()){
63 string.append(QTime::currentTime().toString(
"hh:mm:ss:zzz"));
64 string.append(
") :: ");
66 QByteArray arr = m_port->readAll();
68 ui->_outTextEdit->append(
string);
72 void SerialMainWindow::on__attachProcessPushButton_2_clicked()
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);
81 void SerialMainWindow::redirecteInput()
83 m_childProcess->write(m_port->readAll());
86 void SerialMainWindow::redirecteOutput()
88 m_port->write(m_childProcess->readAllStandardOutput());
91 void SerialMainWindow::childFinished(
int i)
93 ui->_processStateLabel->setText(QString(
"finsihed with code %1").arg(i));
96 void SerialMainWindow::childStatus(QProcess::ProcessState state)
98 auto en = QMetaEnum::fromType<QProcess::ProcessState>();
99 ui->_processStateLabel->setText(en.valueToKey(state));
102 QProcess *SerialMainWindow::childProcess()
const 104 return m_childProcess;
107 void SerialMainWindow::setChildProcess(QProcess *childProcess)
110 m_childProcess->disconnect();
111 delete m_childProcess;
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)));