user3741064
user3741064

Reputation: 43

What is the correct syntax to extract serial data from QSerialPort to a double in Qt Creator?

I am trying to stream data in real time from an arduino. Below is how I set up the serial port :

QSerialPort serial;
QByteArray value0;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    serial.setPortName("COM3");
    serial.setBaudRate(QSerialPort::Baud9600);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    serial.open(QIODevice::ReadWrite);
}
....

This is how I have tried to change the serial data into a double :

serial.readData();

Upvotes: 1

Views: 763

Answers (1)

Nejat
Nejat

Reputation: 32645

You should convert the raw byte array to double according to your device logic. But in case you have sent the data on the other side by streaming it, you can read it by :

QByteArray data = serial->readAll();
QDataStream stream(data);

double value;
stream>>value

Upvotes: 1

Related Questions