LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Efficiency Advice - Reading Buffer and storing data

I have a USB connection and every second I am going to pull all data from the buffer of my USB device.

The data coming in will be a packet in the form of three different variables. Two unsigned ints (id and uint8 size) and a uint8 data array.  One of the first two ints will tell me the size of the uint8 array but i know the max is 4096.

 

So the issue is, I can only read one packet at a time.  However, if I'm looking for two different id's I need a way to save the incoming data.

 

So the question is:

Efficiently/Optimally, would it be better to:

Create a hidden table or list and populate this with the entire buffer then search for my two different id's?  I know that this way, memory allocation is done automatically, but seems cumbersome to search each different part.

 

OR

 

create a struct pointer and dynamically allocate memory (or pick some arbitrary size that is large enough)

for eg. 

typedef struct {

uint16 id;

uint8 size;

uint8 data[4096]; (or uint8 *data)

} myData;

myData *listOfData; (or listOfData[20];)

 

Side Notes: Every Second I plan to empty/read all data from buffer, so that is my time constraint.

I know I could technically read each packet from the buffer one at a time and see if I'm looking for it, but at this time, that is a last resort as it doesn't work out well for the rest of my program.

 

Thanks

 

 

0 Kudos
Message 1 of 3
(2,907 Views)

Are you using the USB as mapped to a virtual com port that you can use the NI rs-232 library on?

 

You could have a thread dedicated to reading the USB, parsing every packet as it comes in, then allocating memory when you see the packet of interest.  That way your main thread is free to run the GUI or whatever else it needs to do.

 

You could use the List type (in CVI programmer's troolbox I think it is) and when you get a packet you want, allocate memory then add it to the list.  Then you could have your main thread read the list, the main thread would know that the list has only packets of interest, it can then probe the list to fetch the item and delete it from the list when its done, also destroy (free) the memory for the packet.   You could list the pointers to the packets, I think the List type is restricted to items of the same size.  Or, if your packets are of some limited number of types, make a separate list for each type.  Packets would appear in the list in the order received.

 

Menchar

0 Kudos
Message 2 of 3
(2,894 Views)

This will be done on its own thread, however, its not exactly like a com port as I have to "ask" for data.  I just wasn't sure what would be faster or better for my program, just adding everything I get to a list or table then searching through that to find a specific item OR to create a multiple list of my defined struct and parse through that.  the latter requires me to free memory if its done dynamically while the list is done by CVI (assuming I delete the item).

 

Nick

0 Kudos
Message 3 of 3
(2,874 Views)