Skip to content

Instantly share code, notes, and snippets.

@NIA
Created February 10, 2013 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NIA/4750471 to your computer and use it in GitHub Desktop.
Save NIA/4750471 to your computer and use it in GitHub Desktop.
Example client/server using QextSerialPort
#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