Results 1 to 4 of 4

Thread: how to check data package received in serial port is 5 byte and check each byte ?

  1. #1
    Join Date
    Jan 2017
    Posts
    13
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Android

    Default how to check data package received in serial port is 5 byte and check each byte ?

    Hello all,
    i want to process output from a serial device , which sending 5 bytes of data serially with 4800 BR and odd parity. i read data serially and displayed in text edit. Now i want to check the received data package is 5 Byte and 7th bit of 1st byte is high and store second byte in QByte array .



    Qt code :
    Qt Code:
    1. ui->setupUi(this);
    2. QVector<double> data_x(101), data_y(101);
    3. timer.start();
    4. init_port();
    5. init_line_plot();
    6.  
    7. x_position = 0;
    8.  
    9. connect(serial, SIGNAL(readyRead()), this, SLOT(receive()));
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. serial->close();
    16. }
    17. void MainWindow::init_port()
    18. {
    19. QTextStream out(stdout);
    20. QString portname = "/dev/ttyUSB0";
    21. serial = new QSerialPort(portname);
    22. if (!serial->open(QIODevice::ReadWrite))
    23. {
    24. QMessageBox::warning(this, "port error", "cannot open port");
    25. }
    26.  
    27. serial->setBaudRate(QSerialPort::Baud4800);
    28. serial->setFlowControl(QSerialPort::NoFlowControl);
    29. serial->setParity(QSerialPort::OddParity);
    30. serial->setDataBits(QSerialPort::Data8);
    31. serial->setStopBits(QSerialPort::OneStop);
    32. out <<"port opened"<<endl<<endl;
    33. }
    34.  
    35. void MainWindow::init_line_plot()
    36. {
    37. ui->customPlot->addGraph();
    38. ui->customPlot->xAxis->setLabel("t");
    39. ui->customPlot->yAxis->setLabel("V");
    40. }
    41.  
    42. void MainWindow::receive()
    43. {
    44. // recieves data as ASCII string
    45. int datalength = 1000;
    46. char data [1000];
    47. int bytesRead = serial->readLine(data, datalength);
    48. data[bytesRead]='\0';
    49.  
    50. ui->textEdit->append(QString(data));
    51.  
    52. QTextStream out(stdout);
    53. out << data << endl;
    54. addDataPoint(atof(data));
    55. }
    56.  
    57. void MainWindow::addDataPoint(double datapoint)
    58. {
    59. if (x_position>250)data_x.pop_front();
    60. double ms = timer.elapsed();
    61. data_x.push_back((double)ms/1000);
    62. x_position++;
    63. if (x_position>250) data_y.pop_front();
    64. data_y.push_back(datapoint);
    65.  
    66.  
    67. ui->customPlot->graph(0)->setData(data_x,data_y);
    68. ui->customPlot->setBackground(Qt::black);
    69. ui->customPlot->graph(0)->rescaleAxes();
    70. ui->customPlot->replot();
    71.  
    72. }
    To copy to clipboard, switch view to plain text mode 

    Anyone please help me ....
    Last edited by anda_skoa; 15th February 2017 at 08:58. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to check data package received in serial port is 5 byte and check each byte

    Anyone please help me ....
    Gladly, just tell us what the problem or question is, then we might help you...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: how to check data package received in serial port is 5 byte and check each byte

    Not sure if I understand, but you can use static_cast the first byte to uint, and then mask 7th bit.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to check data package received in serial port is 5 byte and check each byte

    First of all : in line 47 (serial->readLine()...) You can get any number of bytes in the range of <1..1000>.
    Before atof() you need to check whether the buffer contains a complete package.

  5. The following user says thank you to Lesiok for this useful post:

    suhairkp (20th February 2017)

Similar Threads

  1. Replies: 2
    Last Post: 15th March 2014, 11:54
  2. Replies: 12
    Last Post: 24th July 2012, 08:19
  3. How to write the BYTE* data to a QFile?
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 23rd November 2011, 04:33
  4. PlEASE HELP: Hot To Use Byte Array, data stream
    By aash_89 in forum Qt Programming
    Replies: 7
    Last Post: 16th July 2010, 08:33
  5. Replies: 3
    Last Post: 19th April 2010, 14:16

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.