12-03-2011 01:51 AM
Hi,
In my application, I am polling data from a stack on a module (COTS). And I am tring to create data pakets which contaain 100ms worth of data. Each packet should have a data header which contain data length (how much data in a packet). I am having difficulty creating packets.
right now I am reading the stack (which puts data in a structure) at the same time i am writing to the disk. In this method i can't calculate data lenght because i write the header first and then the data. Is there way i can accomplish this task? Is there way to buffer data for 100ms in some kind of buffer then calculate data length and then write the whole packet (header and data) to the disk? what i am trying to do is yo follow a standard called IRIG 106 Chapter 10. Any help will be greatly appreciated.
Thanks
Solved! Go to Solution.
12-03-2011 12:47 PM
You could append incoming data to a string instead of writing it to a file; every 100 ms you can calculate string lenght and create your packet to write to disk. Beware that if your data are recorded in bynary format normal string operator like strlen or strcat / strcpy cannot be used as they would stop at the first nul byte found. You will need to use memcpy or similar operators and keep a separate lenght indicator calculated on your own based on the real amount of data transferred.
12-07-2011 09:22 AM
Thanks for the reply Mr Bozzolo.
I have few questions. I am recording data in binary format therefore i have to use memcpy functions. Does this need strings.h header file? The data i am recording are integers ( members of a structure). In order for me use memcpy i have frist convert integers to strings and them use memcpy. and also i have couple of structures data types i am wiriting to the file as whole structure not as individual members. is there a way i can write these structure to a string as a whole not as a individual members?
Thanks very much for all the help.
12-08-2011 03:00 PM
You can append structure to the strings using memcpy provided you cast the structure to char * type: this is an example of how it works
typedef struct { int a; int b; int c; } myStruct; static myStruct s; static char msg[512]; static int z; memset (msg, 0, 512); strcpy (msg, "Test: "); z = strlen (msg); s.a = 3; s.b = 123456; s.c = -37599; memcpy (msg + z, (char *)&s, sizeof (myStruct)); z += sizeof (myStruct);
Run the code in the interactive window and examine 'msg' variable after run: selecting Options >> Display entire buffer and Format >> Decimal in String display window you'll see the content of the string including the structure. Note that I'm keeping a separated variable 'z' with string lenght to avoid overwriting string content.
01-11-2012 05:28 PM
Hi Roberto,
Thanks for your reply. What you have posted helped me very much appending stuctures to a string.
In my application, i have few other variables to append (integers and doubles) to string. I am having little trouble doing this. I get a compiler error message saying "too small of argument for one of the array pointers".
Could you please point me in the right direction?
This is what i have
chat packet[300];
int bytes=0;
double timestamp;
memcpy (packet+bytes, (char *) ×tamp, 😎
01-12-2012 01:52 AM
I cannot recognize the error message you posted and the snippet of code is conceptually ok: the problem may be related to the rest of the code you have not shown us. Can you also report the exact error message text?