Results 1 to 3 of 3

Thread: Problem in reading data from the serial port to byte array

  1. #1
    Join Date
    Feb 2014
    Posts
    31
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Post Problem in reading data from the serial port to byte array

    Hello all,
    I am new to Qt and please excuse me for my English, am writing code to controll the finger print reader(R305 module) with serial communication. I am writing the data to device successfully but i am unable to get the data to a bytearray and failed to verify received data with my data.

    The below is the code for reading and writing data...

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3.  
    4. const char enable[12]={0xEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X03,0X01,0X00,0X05};
    5.  
    6. QByteArray data = QByteArray::fromRawData(enable, sizeof(enable));
    7.  
    8. textEdit->setText(data);
    9. qDebug()<<"\n enabletext123:"<<textEdit->toPlainText();
    10.  
    11. writeData(data);
    12.  
    13. }
    14.  
    15. void MainWindow::on_pushIdentity_clicked()
    16. {
    17. const char identify[17]={0xef,0x01,0xff,0xff,0xff,0xff,0x01,0x00,0x08,0x1b,0x01,0x00,0x00,0x01,0x01,0x00,0x27};
    18.  
    19. QByteArray data1 = QByteArray::fromRawData(identify, sizeof(identify));
    20. // port->write(data);
    21.  
    22. textEdit->setText(data1);
    23. qDebug()<<"\n identytext1234:"<<textEdit->toPlainText();
    24.  
    25. writeData(data1);
    26. }
    27.  
    28. void MainWindow::writeData(const QByteArray &data)
    29. {
    30.  
    31. qDebug()<< port->write(data);
    32. qDebug()<<"write data:"<<data;
    33. for(int i=0;i<=5000;i++);
    34. }
    35.  
    36.  
    37. void MainWindow::readData()
    38. {
    39. port->flush();
    40. QByteArray response=port->readAll();
    41. int count=response.size();
    42. qDebug()<<"\n no. of bytes read"<<response.count();
    43.  
    44. textEdit->append(response)
    45.  
    46. qDebug()<<"read data:"<<response;
    47. }
    To copy to clipboard, switch view to plain text mode 

    My debug output is showing like this...and the textEdit not displaying this garbage data...The problem is, i am not getting any idea that what is happing..but the reader is blinking when i press the enable button..

    Starting /home/hari/Videos/qt_projects2/Fingerprint-build-Desktop-Debug/Fingerprint...
    Number of serial ports: 1
    Name: "ttyS0"
    true ok
    Baud rate: 9600
    Data bits: 8
    Stop bits: 1
    Parity: 0
    Flow control: 0
    Read buffer size: 0

    enabletext123: "�����"
    12
    write data: "�����
    no. of bytes read 8
    read data: "�����
    no. of bytes read 4
    read data: "
    identytext1234: "�����"
    17
    write data: "�����
    no. of bytes read 8
    read data: "�����
    no. of bytes read 8
    read data: " U&
    /home/hari/Videos/qt_projects2/Fingerprint-build-Desktop-Debug/Fingerprint exited with code 0


    Please suggest some modifications to get the data understandable for me...ASAP...
    Thanks in advance..
    Last edited by anda_skoa; 15th March 2014 at 15:37. Reason: missing [code] tags

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem in reading data from the serial port to byte array

    Qt Code:
    1. QByteArray data = QByteArray::fromRawData(enable, sizeof(enable));
    2.  
    3. textEdit->setText(data);
    To copy to clipboard, switch view to plain text mode 

    The data will contain a pointer to enable. No conversion to a human-readable string here. The setText() will use a QString ctor QString( const QByteArray& ) most likely. Do not expect any conversion, too. Just expanding bytes to shorts. That's why you get the output you are getting.

    You need to convert yourself. For example:
    Qt Code:
    1. QByteArray data = QByteArray::fromRawData(enable, sizeof(enable));
    2. QString aux;
    3.  
    4. for( int i = 0; i < data.size(); i++ )
    5. {
    6. aux.append(QChar(0x20));
    7. aux.append(QString::number(data.constData[i],16);
    8. }
    9.  
    10. textEdit->setPlainText(aux);
    11. qDebug()<<"\n enabletext123:"<<textEdit->toPlainText();
    12.  
    13. writeData(data);
    To copy to clipboard, switch view to plain text mode 

    and so on.

  3. The following user says thank you to Radek for this useful post:

    prasad1001 (15th March 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    31
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem in reading data from the serial port to byte array

    Thanks for the quick reply..i was followed your code and ...it works for me....

    Thank you very much..

Similar Threads

  1. Replies: 4
    Last Post: 21st January 2013, 12:12
  2. Reading/writing a serial port through USB
    By jvwlong in forum Newbie
    Replies: 2
    Last Post: 28th June 2012, 11:09
  3. 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
  4. Serial Port Reading problem
    By sfabel in forum Qt Programming
    Replies: 12
    Last Post: 18th February 2010, 14:59
  5. Replies: 1
    Last Post: 1st July 2009, 00:37

Tags for this Thread

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.