Created
February 10, 2013 18:21
-
-
Save NIA/4750471 to your computer and use it in GitHub Desktop.
Example client/server using QextSerialPort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "widget.h" | |
#include "ui_widget.h" | |
#include "qextserialport.h" | |
#include <QInputDialog> | |
namespace { | |
const QString DEFAULT_PORT = "COM6"; | |
} | |
Widget::Widget(QWidget *parent) : | |
QWidget(parent), | |
ui(new Ui::Widget), port(NULL) | |
{ | |
ui->setupUi(this); | |
write(tr("Asking for port name")); | |
QString portName = | |
QInputDialog::getText(this, tr("Specify port"), tr("Serial port name:"), | |
QLineEdit::Normal, DEFAULT_PORT ); | |
write(tr("Opening port %1").arg(portName)); | |
port = new QextSerialPort(portName); | |
connect(port, &QextSerialPort::readyRead, [=](){ | |
QByteArray data = port->readAll(); | |
write(tr(">> ") + data.toHex()); | |
}); | |
if(port->open(QextSerialPort::ReadWrite)) { | |
write(tr("Opened!")); | |
} else { | |
write(tr("Failed :(")); | |
} | |
connect(ui->send, &QPushButton::clicked, [=](){ | |
QByteArray data = ui->dataInput->toPlainText().toLocal8Bit(); | |
write("<< " + data); | |
port->write(QByteArray::fromHex(data)); | |
port->flush(); | |
}); | |
} | |
void Widget::write(QString text) { | |
ui->console->append(text); | |
} | |
Widget::~Widget() | |
{ | |
delete ui; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment