1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include "dialog.h" #include <QLabel> #include <QPushButton> #include <QComboBox> #include <QLineEdit> #include <QGridLayout> #include <QSerialPortInfo> #include <QByteArray> #include <QDebug>
Dialog::Dialog(QWidget *parent) : QDialog(parent) , portLabel(new QLabel("Port:")) , portComboBox(new QComboBox()) , connectButton(new QPushButton("Connect")) , dataLabel(new QLabel("Data:")) , dataLineEdit(new QLineEdit()) , sendButton(new QPushButton("Send")) , statusLabel(new QLabel("Status:")) , status(new QLabel()) , replyLabel(new QLabel("Reply:")) , reply(new QLabel())
{ QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts(); for(const QSerialPortInfo &info : infos){ portComboBox->addItem(info.portName()); }
QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(portLabel, 0, 0); mainLayout->addWidget(portComboBox, 0, 1); mainLayout->addWidget(connectButton, 0, 2); mainLayout->addWidget(dataLabel, 1, 0); mainLayout->addWidget(dataLineEdit, 1, 1, 1, 2); mainLayout->addWidget(sendButton, 2, 1, 1, 1); mainLayout->addWidget(statusLabel, 3, 0); mainLayout->addWidget(status, 3, 1, 1, 2); mainLayout->addWidget(replyLabel, 4, 0); mainLayout->addWidget(reply, 4, 1, 1, 2); setLayout(mainLayout);
QObject::connect(connectButton, SIGNAL(clicked()), this, SLOT(connect())); QObject::connect(sendButton, SIGNAL(clicked()), this, SLOT(send())); QObject::connect(this, SIGNAL(portError(const QString)), this, SLOT(handlePortError(const QString))); QObject::connect(&serialPort, SIGNAL(readyRead()), this, SLOT(handleResponse())); }
void Dialog::connect(){ if(serialPort.isOpen()){ serialPort.close(); connectButton->setText("Connect"); return; }
serialPort.setPortName(portComboBox->currentText()); serialPort.setBaudRate(QSerialPort::Baud9600);
if(!serialPort.open(QIODevice::ReadWrite)){ emit portError(QString("Error: %1").arg(serialPort.errorString())); return; }
status->setText(QString("Connected to %1.").arg(serialPort.portName()));
connectButton->setText("Disconnect"); return; }
void Dialog::send(){ QByteArray ba = dataLineEdit->text().toLocal8Bit();
if(ba.isEmpty()){ status->setText(QString("No data!")); return; }
if(serialPort.isOpen() && serialPort.isWritable()){ qint64 bytesWritten = serialPort.write(ba); serialPort.flush();
if(bytesWritten == -1){ emit portError(QString("Write failed! %1").arg(serialPort.errorString())); return; } else if(bytesWritten != ba.size()){ emit portError(QString("Can't write all data! %1").arg(serialPort.errorString())); return; } status->setText(QString("%1 bytes written!").arg(bytesWritten)); }
return; }
void Dialog::handlePortError(const QString &error){ status->setText(error); }
void Dialog::handleResponse(){ if(serialPort.isOpen() && serialPort.isReadable()){ QByteArray readData = serialPort.readAll(); while(serialPort.waitForReadyRead(5000)){ readData += serialPort.readAll(); }
reply->setText(readData); return; }
emit portError(QString("Port isn't open or not readable")); }
|