34.QT- Make a serial assistant (and dynamically detected the online serial port, with source code)

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowTitle(("Simple Serial Port Tool"));
ui->recvEdit- >setReadOnly(true);
initBtn();
initComboBoxs();
initSerial();
qApp->setStyleSheet("QComboBox::item{text-align: center ; }");
}

void Widget::initBtn() //Initialize button
{
Change_btn_isOn(false);
}

void Widget::initComboBoxs() //Initialize the drop-down list box
{
BaudRateType RateTypes[12]={
BAUD1200,BAUD2400 ,BAUD4800,BAUD9600 ,
BAUD14400,BAUD19200,BAUD38400,BAUD56000,
BAUD57600,BAUD115200,BAUD128000, BAUD256000};
DataBitsType BitsTypes[4]={DATA_5,DATA_6, DATA_7, DATA_8};
for(int i =0;i<12;i++)
{
ui->serial_baud->addItem(QString("%1").arg((int)RateTypes[i]),RateTypes[i]) ;
}

for(int i=0;i<4;i++)
{
ui->serial_data->addItem(QString("%1").arg((int)BitsTypes[i]),BitsTypes [i]);
}

ui->serial_parity->addItem("None",PAR_NONE);
ui->serial_parity->addItem("odd parity" ,PAR_ODD);
ui->serial_parity->addItem("even parity",PAR_EVEN);

ui->serial_stop->addItem("1",STOP_1);
ui->serial_stop->addItem("1.5",STOP_1_5);
ui->serial_stop->addItem("2",STOP_2);
}

void Widget::initSerial() //Initialize the serial port
{
onPortAddedOrRemoved();
enumerator =new QextSerialEnumerator();
enumerator->setUpNotifications();
< br /> connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)),this, SLOT(onPortAddedOrRemoved())); //A serial port was found
connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), this, SLOT( onPortAddedOrRemoved())); //Found that there is no serial port

port =new QextSerialPort(QextSerialPort::EventDriven,this);
connect(port, SIGNAL(readyRead( )), this,SLOT(readLineData())); //Connect signal
}

void Widget::on_btn_send_clicked() //Send data
{
if (port->isOpen() && !ui->sendEdit->toPlainText().isEmpty())
{
QString data =ui->sendEdit->toPlainText();
data+=" ";
}
}

void Widget::on_betn_clear_clicked()//Clear the received data
{
ui- >recvEdit->clear();
}

void Widget::on_btn_switch_clicked()//Serial port switch
{if(!port->isOpen()) //Current Not open
{
Change_btn_isOn(true);
port->setPortName(ui->serial_name->itemData(ui->serial_name->currentIndex()).toString());< br /> port->setBaudRate((BaudRateType)ui->serial_baud->itemData(ui->serial_baud->currentIndex()).toInt());
port->setDataBits((DataBitsType)ui-> serial_data->itemData(ui->serial_data->currentIndex()).toInt());
port->setParity((ParityType)ui->serial_parity->itemData(ui->serial_parity->currentIndex() ).toInt());
port->setSt opBits((StopBitsType)ui->serial_stop->itemData(ui->serial_stop->currentIndex()).toInt());
port->open(QIODevice::ReadWrite);
}< br /> else
{
Change_btn_isOn(false);
port->close();
}
}

void Widget: :closeEvent(QCloseEvent *)
{
if(port->isOpen())
port->close();
}

void Widget ::readLineData() //Read data
{
while(port->canReadLine()) {
ui->recvEdit->moveCursor(QTextCursor::End);
ui->recvEdit->insertPlainText(QString::fromLocal8Bit(port->readLine()));
}
}

void Widget::onPortAddedOrRemoved() //Refresh Serial number
{
QString current =ui->serial_name->currentText();
ui->serial_name->blockSignals(true); //Blocking signals
ui-> serial_name->clear();

foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
{
QString friendname = inf o.friendName;
int end=friendname.lastIndexOf(" ");
if(end!=-1)
{
ui->serial_name->addItem(QString( "%1:%2").arg(info.portName).arg(info.friendName.left(end)),info.portName);
}
else
{
ui->serial_name->addItem(QString("%1:%2").arg(info.portName).arg(info.friendName),info.portName);
}
}

ui->serial_name->setCurrentIndex(ui->serial_name->findText(current));
if(ui->serial_name->currentIndex()==-1)
ui->serial_name->setCurrentIndex(0);
ui->serial_name->blockSignals(false); //Turn off blocking
}

void Widget::Change_btn_isOn (bool ison)
{
if(!ison)
{
ui->btn_switch->setStyleSheet("color:blue;border: 1px solid blue");
ui->btn_switch->setText("Open the serial port");
}
else
{
ui->btn_switch->setStyleSheet("color:red;border: 1px solid red");
ui->btn_switch->s etText("Close serial port");
}
}

Widget::~Widget()
{
delete ui;
}

void Widget::on_serial_name_currentIndexChanged(int index)
{
if (port->isOpen()) {//If it is open, close the serial port
port->close();
Change_btn_isOn(false);
}
}

Article source: https://www.cnblogs.com/lifexy /p/9273352.html

Leave a Comment

Your email address will not be published.