//********************************************************************************************************************** SimpleTerminal::SimpleTerminal(QSerialPort *port, QObject *parent) : QObject(parent), _statusText(QString()), _port(port), _som(""), _eom("\r"), _inputHistory(), _inputHistoryIdx(-1), _is_msg_open(false), _cmdParser(nullptr) { Q_CHECK_PTR(_port); _cmdParser = new CommandParser(*this); // Restore settings restoreSettings(); refreshStatusText(); QObject::connect(_port, SIGNAL(readyRead()), this, SLOT(read())); QObject::connect(_port, SIGNAL(baudRateChanged(qint32,QSerialPort::Directions)), this, SLOT(settingsChanged())); QObject::connect(_port, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(settingsChanged())); QObject::connect(_port, SIGNAL(flowControlChanged(QSerialPort::FlowControl)), this, SLOT(settingsChanged())); QObject::connect(_port, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(settingsChanged())); QObject::connect(_port, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(settingsChanged())); QObject::connect(this, SIGNAL(somChanged()), this, SLOT(settingsChanged())); QObject::connect(this, SIGNAL(eomChanged()), this, SLOT(settingsChanged())); }
/*! \property QSerialPort::dataBits \brief the data bits in a frame If the setting is successful, returns true; otherwise returns false and sets an error code which can be obtained by accessing the value of the QSerialPort::error property. */ bool QSerialPort::setDataBits(DataBits dataBits) { Q_D(QSerialPort); if (d->setDataBits(dataBits)) { if (d->dataBits != dataBits) { d->dataBits = dataBits; emit dataBitsChanged(d->dataBits); } return true; } return false; }
SerialLogging::SerialLogging(MainWindow *mainWindow, QSerialPortInfo serialPortDevice, int baudRate, QSerialPort::DataBits dataBits, QSerialPort::Parity parity, QSerialPort::StopBits stopBits) { serialPort = new QSerialPort(serialPortDevice); // Open int ret = serialPort->open(QIODevice::ReadWrite); if (!ret) { qDebug() << "Serial port failed to open"; delete serialPort; Q_ASSERT(0); } else { // Open file fileName = mainWindow->getFileName(); if (fileName != "") { file.setFileName(fileName); ret = file.open(QIODevice::WriteOnly); if (!ret) { qDebug() << "File failed to open"; delete serialPort; Q_ASSERT(0); } } // Set start time startTime = QDateTime::currentDateTimeUtc(); // Configure serial port serialPort->setBaudRate(baudRate); serialPort->setDataBits(dataBits); serialPort->setParity(parity); serialPort->setStopBits(stopBits); connect(this, SIGNAL(serialPortOpened()), mainWindow, SLOT(serialPortConnected())); connect(this, SIGNAL(newDataReady(QByteArray, quint64)), mainWindow, SLOT(receivedData(QByteArray, quint64))); connect(this, SIGNAL(newTimeStamp(qint64)), mainWindow, SLOT(updateTimer(qint64))); emit serialPortOpened(); } // Connect signals connect(serialPort, SIGNAL(readyRead()), this, SLOT(readData())); connect(mainWindow, SIGNAL(baudRateChanged(int)), this, SLOT(on_baudRateUpdated(int))); connect(mainWindow, SIGNAL(dataBitsChanged(QSerialPort::DataBits)), this, SLOT(on_dataBitsUpdated(QSerialPort::DataBits))); connect(mainWindow, SIGNAL(parityChanged(QSerialPort::Parity)), this, SLOT(on_parityUpdated(QSerialPort::Parity))); connect(mainWindow, SIGNAL(stopBitsChanged(QSerialPort::StopBits)), this, SLOT(on_stopBitsUpdated(QSerialPort::StopBits))); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //вывод ошибок в log connect(&port,SIGNAL(error(QString)),this,SLOT(printErrorMessage(QString))); connect(ui->send_pushButton,SIGNAL(clicked()),this,SLOT(sendMessage())); connect(ui->run_button,SIGNAL(clicked()),this,SLOT(runScript())); connect(ui->comPort_comboBox,SIGNAL(currentTextChanged(QString)),&port,SLOT(serialPortChanged(QString))); //получение списка всех портов и добавление их в comPort_comboBox foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) ui->comPort_comboBox->addItem(info.portName(),Qt::DisplayRole); //привязка изменения настроек в интерфейсе к сетерам connect(ui->bitsRate_comboBox,SIGNAL(currentTextChanged(QString)),&port,SLOT(baudRateChanged(QString))); connect(ui->parity__comboBox,SIGNAL(currentTextChanged(QString)),&port,SLOT(parityChanged(QString))); connect(ui->dataBits_comboBox,SIGNAL(currentTextChanged(QString)),&port,SLOT(dataBitsChanged(QString))); connect(ui->stopBits_comboBox,SIGNAL(currentTextChanged(QString)),&port,SLOT(stopBitsChanged(QString))); connect(ui->flowControl_comboBox,SIGNAL(currentTextChanged(QString)),&port,SLOT(flowControlChanged(QString))); connect(ui->comPort_comboBox,SIGNAL(currentTextChanged(QString)),this,SLOT(setDefault(QString))); //ввывод ошибок в log connect(&port,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(error(QSerialPort::SerialPortError))); //добавление в всех возможных вариантов настроек в интерфейс ui->bitsRate_comboBox->addItems(comPortSettings::bitsRates); ui->dataBits_comboBox->addItems(comPortSettings::dataBits); ui->parity__comboBox->addItems(comPortSettings::parityBits); ui->stopBits_comboBox->addItems(comPortSettings::stopBits); ui->flowControl_comboBox->addItems(comPortSettings::flowControl); setDefault(""); QFileDialog* fileDialog = new QFileDialog(this,QString("Open Script"),QString(""),QString("*.txt")); connect(ui->open_button, SIGNAL(clicked()), fileDialog, SLOT(open())); //TODO: роскоментить в финальном варианте (а то система тормозит) connect(fileDialog, SIGNAL(fileSelected(QString)), ui->filePath_lineEdit, //поєднання підтверження вибору файлу та завантаження SLOT(setText(QString))); //привязка действий к кнопкам выбора режима connect(ui->hex_radioButton,SIGNAL(clicked()),this,SLOT(setHexMode())); connect(ui->text_radioButton,SIGNAL(clicked()),this,SLOT(setTextMode())); emit ui->hex_radioButton->clicked(); //слушаем порт connect(&port,SIGNAL(readData(QString)),this,SLOT(printReceivedMessage(QString))); loadSettings(); }