Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:00:56+00:00 2026-06-13T14:00:56+00:00

In an embedded application I am working on, I frequently need to packetize data

  • 0

In an embedded application I am working on, I frequently need to packetize data from several different objects to be sent out over a serial port. Similarly, data comes in on the serial port that needs to be written to several different objects.

For compatibility reasons, the ordering of the data in the packets does not allow for all the data associated with one object to be placed consecutively in the packet. So I can not easily vectorize, etc. data from each object and then put all that in a packet.

I can achieve proper functionality using a very large number of getters/mutators that the packet class uses to create/decode transmitted/received packets. But this does not seem very elegant.

I could make the packet class a friend of the classes it pulls/writes data from/to, but I was always told to avoid using friends as that sort of violates object-oriented principles.

Ideally the classes that handle the actual application would not know anything about the packet class (and not have to provide getters/mutators exclusively for it) and they simply would have new data if a packet came in between updates.

I thought that maybe I could pass references or pointers to the relevant member variables to the packet class at startup, but that is also tricky because all the members are not the same size. Perhaps I could also pass the size information as well? Would there be a way to vectorize a list of void pointer to member and size of member pairs so that the packet class does not need to take a gigantic amount of arguments to its constructor?

I am not sure how well I have described this problem, but I can certainly provide clarifications and more information if that would help. Thanks very much for any ideas.

Abbreviated example of tx packet class currently:

class PacketTx {
private:
  uint8_t buffer[MAX_PACKET_SIZE];   // MAX_PACKET_SIZE is 200
public:
  PacketTx(object1 *firstObject,
           object2 *secondObject,
           object3 *thirdObject
           // ...etc...
           )

  void sendPacket(void);
};

void PacketTx::sendPacket(void) {
  uint32_t i = 0;

  int16_t tempVar1 = firstObject->getVar1();
  uint32_t tempVar2 = secondObject->getVar2();
  uint8_t tempVar3 = firstObject->getVar3();
  int32_t tempVar4 = thirdObject->getVar4();
  // ...etc...

  memcpy(buffer + i, &tempVar1, sizeof(tempVar1));
  i += sizeof(tempVar1);

  memcpy(buffer + i, &tempVar2, sizeof(tempVar2));
  i += sizeof(tempVar2);

  memcpy(buffer + i, &tempVar3, sizeof(tempVar3));
  i += sizeof(tempVar3);

  memcpy(buffer + i), &tempVar4, sizeof(tempVar4));
  i += sizeof(tempVar4);

  // ...etc...

  for(uint32_t j = 0; j < i; ++j)
    putc(static_cast<char>(buffer[i]));
}

This example doesn’t include header, checksum, etc., but it should give a basic idea of what is causing me a headache.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:00:57+00:00Added an answer on June 13, 2026 at 2:00 pm

    So If I understand you have Some class C which has an arbitrary number of members which need to be written out into a buffer in an arbitrary order. Then the same mechanism occurs in reverse.

    Serial -> Packet.buffer() -> Object (and reverse)

    My initial thought was using boost::tuple and having the types correspond to the order of the data in the packet. (But with possibly 50+ members/entries in tuple that could be a big compile time hit)

    e.g

    tuple<float, int, std::string> a(1.0f,  2, std::string("Some Words");
    ostr << a;
    

    That would also require your classes to take a tuple as an argument and fill in the entries from data. So rather than write to a buffer there could be an enumeration of the fields held in the tuple so that “nice” code could be written:

    enum Fields {Price,Amount,Reason};
    typedef boost::tuple<...> MyTuple;
    
    void MyClass::getData( MyTuple& t )
    {
        t<Price>() = mPrice;
        t<Amount>() = mAmount;
        t<Reason>() = mSomeReason;
    }
    

    Note Would work in reverse as well.

    Not sure if this fits completely with what you were trying to do but it may trigger some new thoughts?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a embedded application where I need to declare an array of
The code working fine when run out side the CRM2011 application. When I embedded
I'm working on an application with an embedded UIWebView. The user has the ability
I am working a java application using H2 Database in embedded mode. My Application
I have an embedded application where an image scanner sends out a stream of
I want to copy some functions from OpenCV library to my embedded application. Rewriting
I'm working on swing application that relies on an embedded H2 database. Because I
I'm currently working on an embedded device application (in C). What optimization flag should
I'm working on a Silverlight 4 application. I have a TM symbol embedded in
I am working on a windows application that has a shockwave flash player embedded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.