Results 1 to 18 of 18

Thread: Raw data I/O

  1. #1
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Raw data I/O

    Hi
    I have developed a connector with Qt that send instruction to equipement on my site.
    This equipement located by it IP give an answer in three times
    the instruction is 2 byte 0801
    the response will be
    - 4 bytes 08010d01.
    - 32 bytes 00000..... (just zeros).
    - 237 bytes 00000..0100000000000001010101010101000000000000000 000000. ( the first byte with 01 is the 16th one and the series of next 01
    bytes are in position 101th.
    but data that i recieved is:
    - 4 bytes 08010d01.
    - 32 bytes 0e1f134ffe..
    - 237 bytes 000ffffffff0001deae8546796.......... (not expected data)

    this is an extract of my code:
    Qt Code:
    1. connect(socketts,SIGNAL(readyRead()),this,SLOT(recievedata()));
    2. void M10::recievedata()
    3.  
    4. {
    5. QDataStream in(socketts);
    6. taillemessage=socketts->bytesAvailable();
    7. ui->taillebuffer->append (tr("La taille de la socket est : ")+QString::number(taillemessage));
    8. int row = ui->Notification->rowCount();
    9. QByteArray bab,buffer;
    10. bab.resize(taillemessage);
    11. buffer.resize(500);
    12. int sizebab = in.readRawData(bab.data(),bab.size());
    13.  
    14. {
    15. buffer=bab.data();
    16. buffer.resize(taillemessage);
    17. if(buffer.size()>2)
    18. {
    19. ui->textBrowser_2->append(buffer.toHex());
    20. QStringList fields1;
    21. ui->Notification->setRowCount(row+1);
    22. fields1 <<buffer.toHex()<<QString::number(buffer.size());
    23. for(int i=0;i<fields1.count();++i)
    24. ui->Notification->setItem(row,i,new QTableWidgetItem(fields1[i]));
    25.  
    26. taillemessage=0;
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    can you explain me why the data is not serialized correctly?
    Last edited by anda_skoa; 5th March 2016 at 11:56. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,792
    Thanks
    263
    Thanked 776 Times in 766 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Raw data I/O

    Your device has a different "endianess" from your PC for binary data? You also resize "bab" once and "buffer" twice, including after assigning bab.data() to it. Why?

  3. #3
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    Thanks for reply
    ths taillemessage is defined by socket.bytesavailable, i try to use buffers to stock data incoming.
    can you give an element of response it seem working when i have one packet recieved but for other packets the data displayed is different to data that i have on wireshark.

  4. #4
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    It seem s work with first packet but when other packets becomes i compare. Packets displayed and data packets on wireshark and they are completly different

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Raw data I/O

    How do you store the data until you have reached the necessary amount for interpretation?

    Or rather how do you plan on doing this, currently you seem to accumulate everything as text, which is likely not what you will end up using.

    Cheers,
    _

  6. #6
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    I just recieve data becoming from the equipement who send me response..
    but response displayed doesnt match the data on the network.
    The data stored in bytearray is simply displayed on textbrowser just to make sure that i have recieved the data that i expected to ricieve.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Raw data I/O

    You wrote that "the first packet" works, but since TCP does not work with packets I assume you are referring to reponses.
    What your code currently doesn't show how you separate responses, i.e. where you determine that one response has endded and the next one has started.

    Cheers,
    _

  8. #8
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    i dont have specific endword in my response from the controller that i m asking
    but if i send instruction 0x0801 automatically the controller will send the respnse in three times
    4 bytes 0X08010d01
    32 bytes 0000..... (Only zeros)
    237 bytes 000000010000000010101010101....01010000000000000.. .....
    i know exactly 01 positions but i dont know how can i get those two other packets because i only recieve the first packet authentic.
    The others are something like random packets .
    How can i get this issu resolved??


    Added after 8 minutes:


    More explanations
    If i recieve the first 4bytes packet i display it on my textbroxser widget . its ok
    The second other packets i display theire sizes and what are containing
    the size is fine but what contains is alrady something like efffff0000001111ddffffff00000
    and sometime when i refresh i get the right packets
    However the packets transmitted dont contains delimiter of end sending or something like that.
    Last edited by IODR100; 6th March 2016 at 16:30.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Raw data I/O

    Well, if you know that the response has a defined size, then it doesn't need any delimiter.
    You can then check the number of received bytes to determine when the next response starts.

    My suggestion would be to assmble each response completely before attempting to display it.

    Cheers,
    _

  10. #10
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    ok how can i do it , i still start on Qt programming can you give me draft of code that i can use for my example


    Added after 37 minutes:


    can you give me an example to follow
    ??
    Last edited by IODR100; 6th March 2016 at 18:20.

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,690
    Thanks
    13
    Thanked 1,599 Times in 1,527 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Raw data I/O

    At its simplest, assuming a packet does not take long to process:
    Qt Code:
    1. // m10.h
    2. class M10: public QObject
    3. {
    4. Q_OBJECT
    5. public:
    6. ..
    7. private slots:
    8. void handleReadyRead();
    9. private:
    10. void handleCompletePacket(const QByteArray& packet);
    11.  
    12. enum { packetSize = 273 };
    13.  
    14. QTcpSocket m_socket;
    15. QByteArray m_buffer;
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void M10::handleReadyRead() {
    2. m_buffer.append(m_socket.readAll());
    3. while (m_buffer.size() >= packetSize) {
    4. handleCompletePacket(m_buffer.mid(0, packetSize));
    5. m_buffer = m_buffer.mid(packetSize);
    6. }
    7. }
    8.  
    9. void M10::handleCompletePacket(const QByteArray& packet) {
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

    Other notes: You do not need QDataStream for this exercise, and it could get in your way.

  12. #12
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    Chris thanks for reply
    i will show you all my code and give me changes to do/
    Qt Code:
    1. #include "m10.h"
    2. #include "ui_m10.h"
    3.  
    4. M10::M10(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::M10)
    7. {
    8. taillemessage=0;
    9. ui->setupUi(this);
    10. ui->spinBox->setRange(0,99999);
    11. ui->spinBox->setValue(24001);
    12. ui->IP->setText("10.32.3.12");
    13. socketts=new QTcpSocket(this);
    14. QTimer *timer=new QTimer(this);
    15. timer->start(4000);
    16. connect(timer,SIGNAL(timeout()),this,SLOT(sendrequest()));
    17. connect(socketts,SIGNAL(connected()),this,SLOT(sendrequest()));
    18. connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(connecttom10()));
    19. connect(socketts,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(erreurSocket(QAbstractSocket::SocketError)));
    20. connect(socketts,SIGNAL(readyRead()),this,SLOT(recievedata()));
    21. // connect(socketts,SIGNAL(readyRead()),this,SLOT(on_Configuration_clicked()));
    22. ui->PWD->setEchoMode(QLineEdit::NoEcho);
    23. ui->PWD->setText("klm123");
    24. ui->Login->setText("Admin");
    25. }
    26.  
    27. void M10::authentification()
    28. {
    29. ui->test->clear();
    30. ui->textBrowser->clear();
    31. ui->textBrowser_2->clear();
    32. QByteArray block;
    33. block.fill(0,22);
    34. //out.setByteOrder(QDataStream::LittleEndian);
    35.  
    36. if((ui->Login->text())!="Admin"&& ui->PWD->text()!="klm123")
    37. {
    38. ui->textBrowser->append("User not Authorized to login");
    39. }
    40. else
    41. {
    42. int i=1;
    43. ui->textBrowser->append("Authorized");
    44. QDataStream out(&block, QIODevice::WriteOnly);
    45. out <<quint8(i)<<quint8(i)<<(ui->PWD->text()).toUtf8();
    46. block.remove(2,4);
    47. out.device()->seek(0);
    48. socketts->write(block);
    49. ui->Auth->setEnabled(true);
    50. ui->Login->setFocus();
    51. ui->PWD->setFocus();
    52. sendrequest();
    53.  
    54.  
    55. }
    56. }
    57. void M10::sendrequest()
    58. {
    59. QByteArray block;
    60. QDataStream out(&block, QIODevice::WriteOnly);
    61. out << quint8(ping);
    62. socketts->write(block);
    63. }
    64.  
    65.  
    66. void M10::connecttom10()
    67. {
    68.  
    69. socketts->abort();
    70. socketts->connectToHost(ui->IP->text(),ui->spinBox->value());
    71. ui->textBrowser->append("Connected");
    72. }
    73.  
    74.  
    75.  
    76.  
    77. void M10::on_pushButton_clicked()
    78. {
    79. ui->textBrowser->append("Wait for connecting....");
    80. }
    81. void M10::erreurSocket(QAbstractSocket::SocketError erreur)
    82. {
    83.  
    84. switch(erreur) // On affiche un message différent selon l'erreur qu'on nous indique
    85. {
    86.  
    87. case QAbstractSocket::HostNotFoundError:
    88. ui->textBrowser->append(tr("<em>ERREUR : le serveur n'a pas pu être trouvé. Vérifiez l'IP et le port.</em>"));
    89. break;
    90. case QAbstractSocket::ConnectionRefusedError:
    91. ui->textBrowser->append(tr("<em>ERREUR : le serveur a refusé la connexion. Vérifiez si le programme \"serveur\" a bien été lancé. Vérifiez aussi l'IP et le port.</em>"));
    92. break;
    93. case QAbstractSocket::RemoteHostClosedError:
    94. ui->textBrowser->append(tr("<em>ERREUR : le serveur a coupé la connexion.</em>"));
    95. break;
    96. default:
    97. ui->textBrowser->append(tr("<em>ERREUR : ") + socketts->errorString() + tr("</em>"));
    98. }
    99. }
    100.  
    101. void M10::recievedata()
    102.  
    103. {
    104. QDataStream in(socketts);
    105.  
    106.  
    107. taillemessage=socketts->bytesAvailable();
    108.  
    109.  
    110.  
    111. ui->taillebuffer->append (tr("La taille de la socket est : ")+QString::number(taillemessage));
    112. //in.setByteOrder(QDataStream::LittleEndian);
    113.  
    114. int row = ui->Notification->rowCount();
    115. QByteArray bab,buffer;
    116. buffer.resize(taillemessage);
    117. buffer=socketts->readAll();
    118. ui->textBrowser_2->append(buffer.toHex());
    119. //QStringList fields1;
    120. //ui->Notification->setRowCount(row+1);
    121.  
    122. // fields1 <<buffer.toHex()<<QString::number(buffer.size());
    123. //for(int i=0;i<fields1.count();++i)
    124. // ui->Notification->setItem(row,i,new QTableWidgetItem(fields1[i]));
    125.  
    126. taillemessage=0;
    127.  
    128. }
    129. }
    130.  
    131.  
    132. //
    133.  
    134.  
    135.  
    136.  
    137. M10::~M10()
    138. {
    139. delete ui;
    140. }
    141.  
    142. void M10::on_Auth_clicked()
    143. {
    144. authentification();
    145. }
    146.  
    147.  
    148.  
    149. void M10::on_Contentnode_clicked()
    150. {
    151.  
    152. ui->textBrowser_2->clear();
    153. ui->Notification->setRowCount(0);
    154. QByteArray block;
    155. QDataStream out(&block, QIODevice::WriteOnly);
    156. out.device()->seek(0);
    157. out << quint8(codecnt)<< quint8(node);
    158. socketts->write(block);
    159.  
    160. }
    161.  
    162.  
    163. void M10::on_Configuration_clicked()
    164. {
    165. ui->textBrowser_2->clear();
    166. ui->Notification->setRowCount(0);
    167. QByteArray block;
    168. QDataStream out(&block, QIODevice::WriteOnly);
    169. out << quint8(codegrp)<< quint8(node)<<quint8(0);
    170. socketts->write(block);
    171. // QDataStream in (socketts);
    172. // int row = ui->Notification_2->rowCount();
    173. //quint8 ping;
    174. //in >>ping;
    175. // ui->Notification_2->setRowCount(row+1);
    176. //QStringList fields1;
    177. //fields1 << QString::number(ping,16).toUpper();
    178. //for(int i=0;i<fields1.count();++i)
    179. // ui->Notification_2->setItem(row,i,new QTableWidgetItem(fields1[i]));
    180.  
    181.  
    182. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. namespace Ui {
    2. class M10;
    3. }
    4.  
    5. class M10 : public QMainWindow
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. explicit M10(QWidget *parent = 0);
    11.  
    12. int ping=43;
    13. int codecnt=8;
    14. int codegrp=7;
    15. int node=1;
    16. int grp=6;
    17. ~M10();
    18.  
    19. private slots:
    20. void on_pushButton_clicked();
    21.  
    22. void authentification();
    23. void connecttom10();
    24. void erreurSocket(QAbstractSocket::SocketError erreur);
    25. void sendrequest();
    26. void recievedata();
    27. void on_Auth_clicked();
    28.  
    29.  
    30. void on_Contentnode_clicked();
    31.  
    32. void on_Configuration_clicked();
    33.  
    34. private:
    35. Ui::M10 *ui;
    36. QTcpSocket *socketts;
    37. QTimer *timer;
    38. quint8 taillemessage;
    39. };
    To copy to clipboard, switch view to plain text mode 
    What does this void function contain??
    Qt Code:
    1. void M10::handleCompletePacket(const QByteArray& packet) {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 6th March 2016 at 22:19. Reason: missing [code] tags

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Raw data I/O

    Quote Originally Posted by IODR100 View Post
    What does this void function contain??
    Qt Code:
    1. void M10::handleCompletePacket(const QByteArray& packet) {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 
    This is the function that handles one complete response.
    Here you could add the data to the text browser.

    Cheers,
    _

  14. #14
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    i m beginner on Qt can you give me more explanations

  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Raw data I/O

    Quote Originally Posted by IODR100 View Post
    i m beginner on Qt can you give me more explanations
    Handling your responses is application specific, not Qt specific.

    Cheers,
    _

  16. #16
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Raw data I/O

    dont understood your answer

  17. #17
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Raw data I/O

    Quote Originally Posted by IODR100 View Post
    dont understood your answer
    We don't know what you want to do with the response data.
    This is your application, your use case, you need to have an idea what to do with the data that is sent by the device, no?

    Cheers,
    _

  18. #18
    Join Date
    Mar 2016
    Posts
    10
    Qt products
    Qt5
    Platforms
    Windows

    Cool Re: Raw data I/O

    Ok thanks for answer but i resolve the problem it was just miss undesrtanding.
    However , i want to read the 2 final bytes in little endian order and to convert to Uint16;

    like if i have 2bytes 0D01 in little endian Uint16 the result will be 269;
    i did this line of code but i dont have a good results

    if(buffer.size()==164)
    {
    QDataStream in(buffer);

    in.setByteOrder(QDataStream::LittleEndian);
    buffer=buffer.right(2);
    ui->textBrowser_2->append(buffer.toHex());
    ui->textBrowser_2->append(QString::number(buffer.toUInt()));

    thanks for help


    Added after 1 32 minutes:


    i have data recieved 269 bytes 00000000000000000010000000000000000....01010101010 1010303030300000000000000
    i want to include in tablewidget Sensor names and states (2 columns)
    In the byte array i have 20 of "0x01" 1byte at index from 109 to 128 i want to put a first column Sensor names (Sensor 01 to Sensor 020) and second column
    status Normal if 01 , Status Maintenance if O3 , Status Alarm if 02 etc...
    can i have Help..
    Last edited by IODR100; 7th March 2016 at 22:20.

Similar Threads

  1. Replies: 8
    Last Post: 16th July 2015, 20:44
  2. Replies: 1
    Last Post: 27th February 2015, 07:00
  3. Replies: 0
    Last Post: 25th February 2014, 17:17
  4. Replies: 0
    Last Post: 10th September 2011, 14:38

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.