Hey,
What I am looking to is this:
Receive 32 bytes of data from serial port into an array. ( This I have done already)
Now based on say the second byte in the first 32 bytes, i will know the size of the entire packet I need to receive, say for example its 128 bytes.
So I need to now receive the remaining 3 packets of 32 bytes each and as I get them, combine them all into one big array of 128 bytes to transmit them elsewhere.


I am trying to think about the best way to do this. Any suggestions ? Thanks for all your help.

I did something similar to this before and I found the best way to do it was to create a packet structure... something like:

struct packet_structure {
  char preamble;
  char length;
  char from;
  char to;
  // ... etc
};

I found it a lot more accessable that way. Do you have to have it as an array? It's a little less... nice to manage that way.

What're you having trouble with? Getting the length? Or using the length in defining the length of the big array and then combining everything into this?

No, I do not have to store it as an array. I jus found it easier to store the input data in a 32 byte array as thats the max size of packets I can receive.
What I need to do is get in the whole 128 bytes for example ( and this size is variable but there's only 3 possible sizes 128,512,1024), accumulate it locally and then send it over to another array ( here the array is fixed).
Thanks for your help.

using the length in defining the length of the big array and then combining everything into this?

thats the troublesome part :)

The first thing you should read about is dynamic memory allocation in C. malloc is the function you're gonna be looking for (and free afterwards). When you understand this write a simple program that does something like this:

  1. Prompt the user for a number.
  2. Dynamically allocate an array of this size.
  3. Copy some 'dud' information into this.
  4. Free the array.

This is basically what you need to do. I still recommend creating your own structure, but that's just me. That way it's easier to get specific information... saves you having to #define the length location etc. You can just use something like packet.length. Anyway, after you've written that program integrate it into the other code.

The first thing you should read about is dynamic memory allocation in C. malloc is the function you're gonna be looking for (and free afterwards). When you understand this write a simple program that does something like this:

  1. Prompt the user for a number.
  2. Dynamically allocate an array of this size.
  3. Copy some 'dud' information into this.
  4. Free the array.

This is basically what you need to do. I still recommend creating your own structure, but that's just me. That way it's easier to get specific information... saves you having to #define the length location etc. You can just use something like packet.length. Anyway, after you've written that program integrate it into the other code.

Here's where I am getting stuck at.
So I get 32 bytes and store them in an array. Now when I get the next 32 bytes, I need to append to this existing array. I am thinking I can use a linked list to do this but its not very clear to me on how to.
Thanks for all your help anyways.

Yeah. Linked list might be of use. So you can continuously grab packets without worrying too much about where they go. Have you the dynamic size thing working? That the length parameter determines the size of the packet?

Yeah. Linked list might be of use. So you can continuously grab packets without worrying too much about where they go. Have you the dynamic size thing working? That the length parameter determines the size of the packet?

What I did was this eventually:

-Get 32 bytes at a time.

-Create another array and initialise a pointer to point to its start.
-Copy 32 bytes into this new array using pointers
eg: for (x=0;x<=32;x++)
{
*ptr++ = *oldptr++;
}
ptr points to new array and oldprt points to the buffer in which i have 32 bytes.

So now i have ptr pointing at the end of my array. Stick this in a loop and data gets appended to the array.
SO lets say I have 200 bytes, it all gets appended correctly.

If your question was how I allocate 200 bytes, well in my case I have a fixed packet length, so I am just using a fixed array size for now.
But you could use malloc() to dynamically create an array of any size right ?

>> I have a fixed packet length
I thought the length was a parameter of the packet... guess not anymore.

>> But you could use malloc() to dynamically create an array of any size right ?
Course. It's no different to any other dynamic memory allocation program. Consider this simple exmple (from cplusplus.com):

/* malloc example: string generator*/
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='\0';

  printf ("Random string: %s\n",buffer);
  free (buffer);

  return 0;
}

If you were to modify that slightly to something like:

#include <stdio.h>
#include <stdlib.h>

int main ()
{
  /* Record all packets via this variable, then save it via 
     malloc to a memory location that's the right size */
  char packet_buffer[MAX_PACKET_SIZE];
  char *packet;
  int packet_length;

  /* I'm assuming you have a function like thse */
  read_in_packet( &packet_buffer );
  packet_length = get_packet_length( &packet_buffer );

  packet= (char*) malloc (packet_length+1);
  if (packet==NULL) exit (1);

  memcpy( packet, packet_buffer, packet_length );

  free ( packet );

  return 0;
}

I haven't tested the above code so it mightn't work. Now, what you could do is simply save the recorded packets to a file. But what would be neat would be to make packet a char **, and dynamically allocate a 2d array of memory, one dimension for the current packet, and the other for packets over time. Allocate a block of packets at a time and realloc when you need more space.

I have a fixed packet length

I thought the length was a parameter of the packet... guess not anymore.

int main ()
{
  /* Record all packets via this variable, then save it via 
     malloc to a memory location that's the right size */
  char packet_buffer[MAX_PACKET_SIZE];
  char *packet;
  int packet_length;

  /* I'm assuming you have a function like thse */
  read_in_packet( &packet_buffer );
  packet_length = get_packet_length( &packet_buffer );

  packet= (char*) malloc (packet_length+1);
  if (packet==NULL) exit (1);

  memcpy( packet, packet_buffer, packet_length );

  free ( packet );

  return 0;
}

I haven't tested the above code so it mightn't work. Now, what you could do is simply save the recorded packets to a file. But what would be neat would be to make packet a char **, and dynamically allocate a 2d array of memory, one dimension for the current packet, and the other for packets over time. Allocate a block of packets at a time and realloc when you need more space.

interesting. Need to think more into it. I liked the second method of accepting packets though.

You might find it easier to read the first packet into a 32 byte allocation (via malloc). Read the size via an overlay of the struct defined way at the top (has the preamble, size and end points).
Realloc the current allocation to the size that you need and set a pointer at the end of the first packet (location 32). Read the next packet to the new pointer, increment pointer 1 packet length and repeat until you have all of the packets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.