<rect>
<x>0</x>
<y>0</y>
- <width>295</width>
- <height>255</height>
+ <width>335</width>
+ <height>356</height>
</rect>
</property>
<property name="windowTitle">
- <string>Simple port info application</string>
+ <string>Serial port setting</string>
</property>
- <layout class="QGridLayout" name="gridLayout_4">
- <item row="0" column="0">
+ <layout class="QVBoxLayout" name="verticalLayout_2">
+ <item>
<widget class="QGroupBox" name="portsGroupBox">
<property name="title">
- <string>Available ports:</string>
+ <string>Port to communicate</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="portNameLabel">
<property name="text">
- <string>Current port name:</string>
+ <string>Port name:</string>
</property>
</widget>
</item>
<widget class="QComboBox" name="portsComboBox"/>
</item>
<item>
- <widget class="QPushButton" name="updateButton">
- <property name="text">
- <string>Update</string>
- </property>
- </widget>
- </item>
- <item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
+ <item>
+ <widget class="QPushButton" name="updateButton">
+ <property name="text">
+ <string>Update available ports</string>
+ </property>
+ </widget>
+ </item>
</layout>
</widget>
</item>
- <item row="1" column="0">
+ <item>
<widget class="QGroupBox" name="infoGroupBox">
<property name="title">
<string>Current port info:</string>
</item>
</layout>
</item>
- <item row="1" column="0">
- <layout class="QGridLayout" name="gridLayout_2">
+ </layout>
+ </widget>
+ </item>
+ <item>
+ <widget class="QGroupBox" name="groupBox">
+ <property name="enabled">
+ <bool>true</bool>
+ </property>
+ <property name="title">
+ <string>Port settings</string>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <layout class="QGridLayout" name="gridLayout_5" rowstretch="0,0,0,0,0">
<item row="0" column="0">
- <widget class="QPushButton" name="validButton">
+ <widget class="QLabel" name="baudRateLabel">
<property name="text">
- <string>Check valid</string>
+ <string>Baud rate</string>
</property>
</widget>
</item>
<item row="0" column="1">
- <widget class="QLabel" name="validLabel">
- <property name="text">
- <string>***</string>
+ <widget class="QComboBox" name="ratesComboBox">
+ <property name="currentIndex">
+ <number>3</number>
</property>
+ <item>
+ <property name="text">
+ <string>1200</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>2400</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>4800</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>9600</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>19200</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>38400</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>57600</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>115200</string>
+ </property>
+ </item>
</widget>
</item>
<item row="1" column="0">
- <widget class="QPushButton" name="busyButton">
+ <widget class="QLabel" name="dataBitsLabel">
<property name="text">
- <string>Check busy</string>
+ <string>Data bits</string>
</property>
</widget>
</item>
<item row="1" column="1">
- <widget class="QLabel" name="busyLabel">
- <property name="text">
- <string>***</string>
+ <widget class="QComboBox" name="dataBitsComboBox">
+ <property name="currentIndex">
+ <number>3</number>
</property>
+ <item>
+ <property name="text">
+ <string>5</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>6</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>7</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>8</string>
+ </property>
+ </item>
</widget>
</item>
<item row="2" column="0">
- <widget class="QPushButton" name="ratesButton">
+ <widget class="QLabel" name="parityLabel">
<property name="text">
- <string>Get rates</string>
+ <string>Parity</string>
</property>
</widget>
</item>
<item row="2" column="1">
- <widget class="QComboBox" name="ratesComboBox"/>
+ <widget class="QComboBox" name="parityComboBox">
+ <item>
+ <property name="text">
+ <string>No parity</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Even parity</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Odd parity</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Space parity</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Mark parity</string>
+ </property>
+ </item>
+ </widget>
+ </item>
+ <item row="3" column="0">
+ <widget class="QLabel" name="stopBitsLabel">
+ <property name="text">
+ <string>Stop bits</string>
+ </property>
+ </widget>
+ </item>
+ <item row="3" column="1">
+ <widget class="QComboBox" name="stopBitsComboBox">
+ <item>
+ <property name="text">
+ <string>one stop</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>one and half stop</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>two stop</string>
+ </property>
+ </item>
+ </widget>
+ </item>
+ <item row="4" column="0">
+ <widget class="QLabel" name="flowControlLabel">
+ <property name="text">
+ <string>Flow control</string>
+ </property>
+ </widget>
+ </item>
+ <item row="4" column="1">
+ <widget class="QComboBox" name="flowControlComboBox">
+ <item>
+ <property name="text">
+ <string>No flow control</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Hardware</string>
+ </property>
+ </item>
+ <item>
+ <property name="text">
+ <string>Software</string>
+ </property>
+ </item>
+ </widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <spacer name="horizontalSpacer_2">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ <item>
+ <widget class="QPushButton" name="okPushButton">
+ <property name="enabled">
+ <bool>false</bool>
+ </property>
+ <property name="text">
+ <string>OK</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="cancelPushButton">
+ <property name="text">
+ <string>Cancel</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
bool QtDNSerialPortImpl::setup()
{
- //TODO
if (!MainWindow::instance)
return false;
QtDNSerialPortSettingDialog dialog(MainWindow::instance);
dialog.exec();
- mPortName = dialog.getSelectedPortName();
- mBaudRate = (SerialPort::Rate) dialog.getSelectedRate();
-
- return true;
+ if (dialog.isOK())
+ {
+ mPortName = dialog.getSelectedPortName();
+ mBaudRate = (SerialPort::Rate) dialog.getSelectedRate();
+ mDataBits = dialog.getSelectedDataBits();
+ mParity = dialog.getSelectedParity();
+ mStopBits = dialog.getSelectedStopBits();
+ mFlowControl = dialog.getSelectedFlowControl();
+
+ return true;
+ }
+ return false;
}
return;
}
- bool r = port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
- if (r)
+ bool r = true;
+ int retry = 6;
+ do
{
- if (!port->setRate(impl->mBaudRate))
+ r = port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
+ if (r)
{
- r = false;
+ if (!port->setRate(impl->mBaudRate))
+ {
+ r = false;
+ }
}
- }
- if (r)
- {
- if (!port->setDataBits(impl->mDataBits))
+ if (r)
{
- r = false;
+ if (!port->setDataBits(impl->mDataBits))
+ {
+ r = false;
+ }
}
- }
- if (r)
- {
- if (!port->setParity(impl->mParity))
+ if (r)
{
- r = false;
+ if (!port->setParity(impl->mParity))
+ {
+ r = false;
+ }
}
- }
- if (r)
- {
- if (!port->setStopBits(impl->mStopBits))
+ if (r)
+ {
+ if (!port->setStopBits(impl->mStopBits))
+ {
+ r = false;
+ }
+ }
+ if (r)
{
- r = false;
+ if (!port->setFlowControl(impl->mFlowControl))
+ {
+ r = false;
+ }
}
- }
- if (r)
- {
- if (!port->setFlowControl(impl->mFlowControl))
+
+ if (!r)
{
- r = false;
+ SleeperThread::msleep(500);
+ port->close();
+ retry--;
+ TKLog::printf("Failed to open the serial port. retrying...");
}
- }
+ } while (!r && retry >= 0);
+
if (!r)
{
QtDNSerialPortSettingDialog::QtDNSerialPortSettingDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::QtDNSerialPortSettingDialog)
+ , mIsOK(false)
{
ui->setupUi(this);
procUpdateAvailablePorts();
connect(ui->updateButton, SIGNAL(clicked()), this, SLOT(procUpdateAvailablePorts()));
connect(ui->portsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(procItemPortChanged(int)));
- connect(ui->busyButton, SIGNAL(clicked()), this, SLOT(procBusyButtonClick()));
- connect(ui->validButton, SIGNAL(clicked()), this, SLOT(procValidButtonClick()));
- connect(ui->ratesButton, SIGNAL(clicked()), this, SLOT(procRatesButtonClick()));
+ connect(ui->okPushButton, SIGNAL(clicked()), this, SLOT(procOKButtonClick()));
+ connect(ui->cancelPushButton, SIGNAL(clicked()), this, SLOT(procCancelButtonClick()));
}
QtDNSerialPortSettingDialog::~QtDNSerialPortSettingDialog()
return ui->ratesComboBox->currentText().toInt();
}
+SerialPort::DataBits QtDNSerialPortSettingDialog::getSelectedDataBits()
+{
+ SerialPort::DataBits r = SerialPort::UnknownDataBits;
+
+ switch(ui->dataBitsComboBox->currentIndex())
+ {
+ case 0:
+ r = SerialPort::Data5;
+ break;
+ case 1:
+ r = SerialPort::Data6;
+ break;
+ case 2:
+ r = SerialPort::Data7;
+ break;
+ case 3:
+ r = SerialPort::Data8;
+ break;
+ }
+
+ return r;
+}
+
+SerialPort::Parity QtDNSerialPortSettingDialog::getSelectedParity()
+{
+ SerialPort::Parity r = SerialPort::UnknownParity;
+
+ switch(ui->parityComboBox->currentIndex())
+ {
+ case 0:
+ r = SerialPort::NoParity;
+ break;
+ case 1:
+ r = SerialPort::EvenParity;
+ break;
+ case 2:
+ r = SerialPort::OddParity;
+ break;
+ case 3:
+ r = SerialPort::SpaceParity;
+ break;
+ case 4:
+ r = SerialPort::MarkParity;
+ break;
+ }
+
+ return r;
+}
+
+SerialPort::StopBits QtDNSerialPortSettingDialog::getSelectedStopBits()
+{
+ SerialPort::StopBits r = SerialPort::UnknownStopBits;
+
+ switch(ui->stopBitsComboBox->currentIndex())
+ {
+ case 0:
+ r = SerialPort::OneStop;
+ break;
+ case 1:
+ r = SerialPort::OneAndHalfStop;
+ break;
+ case 2:
+ r = SerialPort::TwoStop;
+ break;
+ }
+
+ return r;
+}
+
+SerialPort::FlowControl QtDNSerialPortSettingDialog::getSelectedFlowControl()
+{
+ SerialPort::FlowControl r = SerialPort::UnknownFlowControl;
+
+ switch(ui->flowControlComboBox->currentIndex())
+ {
+ case 0:
+ r = SerialPort::NoFlowControl;
+ break;
+ case 1:
+ r = SerialPort::HardwareControl;
+ break;
+ case 2:
+ r = SerialPort::SoftwareControl;
+ break;
+ }
+
+ return r;
+}
+
+
void QtDNSerialPortSettingDialog::procUpdateAvailablePorts()
{
ui->portsComboBox->clear();
v.setValue(info);
ui->portsComboBox->addItem(info.portName(), v);
}
+
+ ui->okPushButton->setEnabled(ui->portsComboBox->count() > 0);
}
void QtDNSerialPortSettingDialog::procItemPortChanged(int idx)
ui->locationValueLabel->setText(info.systemLocation());
ui->descriptionValueLabel->setText(info.description());
ui->manufacturerValueLabel->setText(info.manufacturer());
-
- ui->busyLabel->setText("***");
- ui->validLabel->setText("***");
- ui->ratesComboBox->clear();
}
}
-void QtDNSerialPortSettingDialog::procBusyButtonClick()
+void QtDNSerialPortSettingDialog::procOKButtonClick()
{
- int idx = ui->portsComboBox->currentIndex();
- if (idx >= 0) {
- QVariant v = ui->portsComboBox->itemData(idx);
- if (v.isValid()) {
- SerialPortInfo info = v.value<SerialPortInfo>();
- ui->busyLabel->setText(info.isBusy() ? tr("Busy") : tr("Free"));
- }
- }
+ mIsOK = true;
+ done(1);
}
-void QtDNSerialPortSettingDialog::procValidButtonClick()
+void QtDNSerialPortSettingDialog::procCancelButtonClick()
{
- int idx = ui->portsComboBox->currentIndex();
- if (idx >= 0) {
- QVariant v = ui->portsComboBox->itemData(idx);
- if (v.isValid()) {
- SerialPortInfo info = v.value<SerialPortInfo>();
- ui->validLabel->setText(info.isValid() ? tr("Valid") : tr("Invalid"));
- }
- }
+ mIsOK = false;
+ done(0);
}
-void QtDNSerialPortSettingDialog::procRatesButtonClick()
+bool QtDNSerialPortSettingDialog::isOK()
{
- ui->ratesComboBox->clear();
- int idx = ui->portsComboBox->currentIndex();
- if (idx >= 0) {
- QVariant v = ui->portsComboBox->itemData(idx);
- if (v.isValid()) {
- SerialPortInfo info = v.value<SerialPortInfo>();
-
- foreach (qint32 rate, info.standardRates())
- ui->ratesComboBox->addItem(QString::number(rate));
- }
- }
+ return mIsOK;
}