Results 1 to 2 of 2

Thread: Read data from serial port

  1. #1
    Join Date
    Sep 2017
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Read data from serial port

    hello everyone, i'm newbie in qt and please excuse me for my English . I'm writing code to receive data from se i have 5 byte data from serial device and i want to save each in a value then print it. example my device sends a string "fet" and it prints "fet" on window but i want print one character 'f' or 'e' ... thank you
    ter.PNG
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtSerialPort>
    4. #include <QtSerialPort/qserialport.h>
    5. #include <QDebug>
    6.  
    7. QSerialPort *serialPort;
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13.  
    14. ui->setupUi(this);
    15. serialPort = new QSerialPort(this);
    16. serialPort->setPortName("com1");
    17. serialPort->setBaudRate(QSerialPort::Baud9600);
    18. serialPort->setDataBits(QSerialPort::Data8);
    19. serialPort->setParity(QSerialPort::NoParity);
    20. serialPort->setStopBits(QSerialPort::OneStop);
    21. serialPort->open(QIODevice::ReadWrite);
    22. connect(serialPort,SIGNAL(readyRead()),this,SLOT(PutData()));
    23. }
    24.  
    25. MainWindow::~MainWindow()
    26. {
    27. delete ui;
    28. serialPort->close();
    29. }
    30.  
    31. void MainWindow::on_Send_clicked()
    32. {
    33. serialPort->write(ui->SendEdit->text().toStdString().data());
    34.  
    35. }
    36.  
    37. void MainWindow::PutData()
    38. {
    39. //ui->Receive->clear();
    40. QByteArray data;
    41. data=serialPort->readAll();
    42. //sleep(1);
    43. ui->Receive->insertPlainText(data);
    44. }
    45.  
    46. void MainWindow::on_Clear_clicked()
    47. {
    48. ui->Receive->clear();
    49. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by hoangnguyen; 5th September 2017 at 07:30.

  2. #2
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read data from serial port


Similar Threads

  1. Replies: 0
    Last Post: 4th February 2016, 11:48
  2. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 09:49
  3. read data from serial port and display it
    By vanduongbk in forum Newbie
    Replies: 1
    Last Post: 30th June 2013, 04:46
  4. Replies: 4
    Last Post: 2nd June 2012, 08:04
  5. read data from serial port
    By amitpatel22 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 20th July 2011, 18:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.