Results 1 to 6 of 6

Thread: Sending a "struct" through a serial (with QIODevice::write())

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Sending a "struct" through a serial (with QIODevice::write())

    Hello!

    I have to create a software that communicates with a serial port sending, instead of any chars, a structure. A software that sends bytes I already have, so no problem. The problem is that the function that sends data to the serial port uses the qint64 QIODevice::write ( const QByteArray & byteArray ) in a similar way to this:

    Qt Code:
    1. { //.h
    2. #include <Serial/abstractserial.h>
    3. AbstractSerial *port1;
    4. }
    5.  
    6. {
    7. port1 = new AbstractSerial();
    8. }
    9.  
    10. bool command::Write(QByteArray com)
    11. {
    12. if(!port1->isOpen())
    13. {
    14. emit sendSBMessage("Port closed");
    15. return false;
    16. }
    17. else
    18. {
    19. port1->write(com + "\r\0");
    20. return true;
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    Now what I have to do is to create a function that instead of sending that QByteArray, it rather sends a structure. But the problem is that the function used by AbstractSerial port1 is the QIODevice::write, and this function is not ready for sending structures as it seems.


    So how do I do this? I have no idea.


    Thanks,


    Momergil

  2. #2
    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: Sending a "struct" through a serial (with QIODevice::write())

    You must create a function that performs serialization structure to QByteArray. And the recipient must perform the inverse operation.

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending a "struct" through a serial (with QIODevice::write())

    Quote Originally Posted by Lesiok View Post
    You must create a function that performs serialization structure to QByteArray. And the recipient must perform the inverse operation.
    Hmmm, not sure that I understood. You mean that I must create some sort of protocol that catch the data from the structure, put it into a QByteArray variable and sends it normally through the serial port, and than the other side, knowing that protocol, will "translate" it?


    Momergil

  4. #4
    Join Date
    Jul 2008
    Location
    Norway
    Posts
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Sending a "struct" through a serial (with QIODevice::write())

    You want to read about the QDataStream class and check out this example.
    Declare the << and >> operator overloads as friend to get access to private data in your class.
    !sirius

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sending a "struct" through a serial (with QIODevice::write())

    Lesiok has the general process, and sirius has a good approach for Qt-to-Qt program communications (although it can do other things). Your requirement is too vague to allow much more specific answers.

    What is at the other end of the transmission? Another Qt program or something else? Do you understand/have intrinsic variable/byte order/character encoding differences? Is the "structure" a simple C++ struct using only simple types, a list, a tree or other graph...? If it is a graph, does it have cycles?

  6. #6
    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: Sending a "struct" through a serial (with QIODevice::write())

    Quote Originally Posted by Momergil View Post
    Hmmm, not sure that I understood. You mean that I must create some sort of protocol that catch the data from the structure, put it into a QByteArray variable and sends it normally through the serial port, and than the other side, knowing that protocol, will "translate" it?
    Momergil
    Yes. In addition, data transfer without packing in a protocol is sensitive to transmission errors.

Similar Threads

  1. Replies: 4
    Last Post: 2nd June 2012, 08:04
  2. Replies: 2
    Last Post: 21st August 2011, 08:57
  3. Replies: 1
    Last Post: 16th June 2009, 10:09
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

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.