//********************************************************************************************************************** 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::baudRate \brief the data baud rate for the desired direction 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. To set the baud rate, use the enumeration QSerialPort::BaudRate or any positive qint32 value. \warning Only the AllDirections flag is support for setting this property on Windows, Windows CE, and Symbian. \warning Returns equal baud rate in any direction on Windows, Windows CE, and Symbian. */ bool QSerialPort::setBaudRate(qint32 baudRate, Directions dir) { Q_D(QSerialPort); if (d->setBaudRate(baudRate, dir)) { if (dir & QSerialPort::Input) { if (d->inputBaudRate != baudRate) d->inputBaudRate = baudRate; else dir &= ~QSerialPort::Input; } if (dir & QSerialPort::Output) { if (d->outputBaudRate != baudRate) d->outputBaudRate = baudRate; else dir &= ~QSerialPort::Output; } if (dir) emit baudRateChanged(baudRate, dir); return true; } return false; }
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(); }