Important: Please read the Qt Code of Conduct -

How to create defined message structures in code.

  • I want do develop a simulator that sends defined messages to a subsystem that I am testing.
    The interface is defined as different data messages. Each request message, sent from my simulator, has a predefined header and a defined data contents and the reply from the subsystem is also defined as different messages with header and data.

    The simulator is kind of simple. It shall send a message and wait for a acknowledge from the subsystem.

    My problem is how to create these different messages in code? What is the best way to create the message structure? The byte order is important and the message structure must be exactly the way they are specified in the interface specification. e.g:
    unit8 messageId
    unit8 messageType
    unit16 totalMessageLength
    uint32 Id
    uint32 name
    unit16 textLength
    unit8(16) text

    What is the best way to create a data structure that represents my message above?

  • Hi,
    Just like your text says, use structures ;-)
    make it a typedef and register it with Q_REGISTER_METATYPE so it may be used in Signal/slots connections.
    Or generate a MessageClass containing public member like you described. Also below this class do the register metatyp for Signal/Slots.
    Add member function to fulfill some extra options you need.
    class MessageClass
    -- your list here --
    void data(unsigned char *); // Just an example of a member function, no idea what you need for it

    Q_REGISTER_METATYPE(MessageClass); // Enable MOC compiler for Signal/Slots


    typedef struct
    -- Your list here!

  • Ok that's interesting. A class is convenient but how do I ensure that the actual data gets sent in the right order with no gaps between the data elements?
    From Ada95 i remember that I used a compiler instruction called
    @pragma pack@

    this ensured that the compiler always allocated the data without gaps in memory.
    Thanks Jeroentje, now I can start to test the concept:)

Log in to reply