Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialport send binary data
Forum Updated to NodeBB v4.3 + New Features

QSerialport send binary data

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMJJ001
    wrote on 17 May 2018, 08:19 last edited by
    #1

    Dear all,

    I'm setting up a communication between my PC and embedded system. My PC needs to send certain data to the embedded system.

    Now when I send for example "START" with QSerialport it sends ASCII code. So there is a lot of traffic on the bus to say start to bus.

    To reduce this (unnessecairy) traffic, I would like to send the raw data. As start signal I would for example send 00010001 and nothing more.

    Is this possible? If yes how should I do this?
    Below is the code I use at the moment.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort/QSerialPort>
    #include <QDebug>
    
    QSerialPort *serial;
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    
    {
        ui->setupUi(this);
        QWidget::setWindowTitle("SCOT - TSP -- Terex Sensor Programmer");
        QWidget::setWindowIcon(QIcon(":/new/rsc/img/TSP_ICON.ico"));
    
        QPixmap logPix(":/new/rsc/img/Sarens.png");
        ui->SarLogo_Label->setPixmap(logPix);
    
        serial = new QSerialPort(this);
        serial->setPortName("com6");
        serial->setBaudRate(QSerialPort::Baud38400);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }
    MainWindow::~MainWindow()
    {
        delete ui;
        serial->close();
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        serial->write("START \n");
        qDebug()<<"START";
    }
    
    void MainWindow::serialReceived()
    {
        QByteArray ba;
        qDebug()<<ba;
        ba=serial->readAll();
        ui->Terminal_textedit->appendPlainText(ba);
    
    }
    
    void MainWindow::on_actionConnect_triggered()
    {
    
    }
    
    
    J 1 Reply Last reply 17 May 2018, 08:25
    0
    • T TMJJ001
      17 May 2018, 08:19

      Dear all,

      I'm setting up a communication between my PC and embedded system. My PC needs to send certain data to the embedded system.

      Now when I send for example "START" with QSerialport it sends ASCII code. So there is a lot of traffic on the bus to say start to bus.

      To reduce this (unnessecairy) traffic, I would like to send the raw data. As start signal I would for example send 00010001 and nothing more.

      Is this possible? If yes how should I do this?
      Below is the code I use at the moment.

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QtSerialPort/QSerialPort>
      #include <QDebug>
      
      QSerialPort *serial;
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      
      {
          ui->setupUi(this);
          QWidget::setWindowTitle("SCOT - TSP -- Terex Sensor Programmer");
          QWidget::setWindowIcon(QIcon(":/new/rsc/img/TSP_ICON.ico"));
      
          QPixmap logPix(":/new/rsc/img/Sarens.png");
          ui->SarLogo_Label->setPixmap(logPix);
      
          serial = new QSerialPort(this);
          serial->setPortName("com6");
          serial->setBaudRate(QSerialPort::Baud38400);
          serial->setDataBits(QSerialPort::Data8);
          serial->setParity(QSerialPort::NoParity);
          serial->setStopBits(QSerialPort::OneStop);
          serial->setFlowControl(QSerialPort::NoFlowControl);
          serial->open(QIODevice::ReadWrite);
          connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
      }
      MainWindow::~MainWindow()
      {
          delete ui;
          serial->close();
      }
      
      
      void MainWindow::on_pushButton_clicked()
      {
          serial->write("START \n");
          qDebug()<<"START";
      }
      
      void MainWindow::serialReceived()
      {
          QByteArray ba;
          qDebug()<<ba;
          ba=serial->readAll();
          ui->Terminal_textedit->appendPlainText(ba);
      
      }
      
      void MainWindow::on_actionConnect_triggered()
      {
      
      }
      
      
      J Offline
      J Offline
      JonB
      wrote on 17 May 2018, 08:25 last edited by
      #2

      @TMJJ001 said in QSerialport send binary data:

      qint64 QIODevice::write(const QByteArray &byteArray) (http://doc.qt.io/qt-5/qiodevice.html#write-2), for example, writes arbitrary binary bytes.

      1 Reply Last reply
      7
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on 17 May 2018, 09:20 last edited by kuzulis
        #3

        Is this possible? If yes how should I do this?

        The '00010001' it is '0x11' in hexadecimal, so just write:

        serial.write(QByteArray::fromHex("11"));
        

        [offtop] I do not understand, what is hard for you? How do you program without knowing the basic knowledge, that goes in school? Why do you ask such questions without trying to find an answer in Google or in Qt documentation?[/offtop]

        1 Reply Last reply
        2

        1/3

        17 May 2018, 08:19

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved