I'm working on writing a datalogging program. I'm using a PhidgetSBC3 but can't seem to get my logic in right. What I'm doing right now is incrementing a counter as spatial packets come in through the SpatialDataHandler function. I then check that counter against a number in an if. If that counter is equal to the number then it goes into another function in which the file is written. For some reason, the program never reaches the write_file function. Can anyone help? A lot of this stuff is over my head.

EDIT: If someone would also be able to tell me how to change the file that I open when I write data, I'd greatly appreciate that. For instance, if I want to write 10 files at 10kb a piece, I can't give them the same name. However fopen wants a const char*. Any advice? I'm also afraid packets are coming in faster than a file can be written. How can I prevent the arrays that are storing data from being over-written during write_file, but store the packets that are coming in elsewhere?

//callback that will run if the Spatial is attached to the computerint CCONV AttachHandler(CPhidgetHandle Accel1, void *userptr){ int serialNo; CPhidget_getSerialNumber(Accel1, &serialNo); printf("%i Attached\n", serialNo);

return 0;}

//callback that will run if the Spatial is detached from the computerint CCONV DetachHandler(CPhidgetHandle Accel1, void *userptr){ int serialNo; CPhidget_getSerialNumber(Accel1, &serialNo); printf("%i Detached, Closing Object!\n", serialNo);

I'm amazed your compiler actually made an executable of this. But when it did, did you actually try to run it in the debugger? What your program currently does is calling phidget_spatial() to set up the handlers and then return and immediately block in the getchar() function. Basically, your program pauses until you press a key, at which it exits..

(Unless you have a very slow storage media you should not worry about writing data as they arrive. But you should really worry about the code. If you compile using g++, add the "-Wall -Werror" flags to locate the most serious flaws)

frodegill, do you mean setting -x in the code? I'm not familiar at all with Linux and barely have a handle on C, let alone C++. Either way, I didn't run it in the debugger initially. I was trying to use printf statements in carefully located positions to understand exactly what was going on. Clearly that wasn't working either.

My problem is I need to write a program that will log the data (not hard when using the SpatialDataHandler) from multiple Phidgets (starting to get harder, multiple handles, writing multiple sets of data to file, etc) and then be able to divide up my files in chosen sizes for analysis later on. Any advice on a good way to do that? I'm already working on rewriting my code so it logs to one file from one Phidget, but eventually I want to do multiple Phidgets into multiple files, based on integers fed through scanf() when the program opens.

EDIT: Added new program that gets me a CSV log from one spatial. How do I add Phidgets (one spatial would be enough to demonstrate the concept) and how do I make the file size configurable/create multiple files as a result of a "Full" file.

//callback that will run if the Spatial is attached to the computerint CCONV AttachHandler(CPhidgetHandle Accel1, void *userptr){ int serialNo; CPhidget_getSerialNumber(Accel1, &serialNo); printf("%i Attached\n", serialNo);

return 0;}

//callback that will run if the Spatial is detached from the computerint CCONV DetachHandler(CPhidgetHandle Accel1, void *userptr){ int serialNo; CPhidget_getSerialNumber(Accel1, &serialNo); printf("%i Detached, Closing Object!\n", serialNo);

//increment index_counter to write to next spot in array so information isn't overwritten when the packet is delivered from the spatial index_counter++; //check value of index_counter to allow writing into a secondary array during file writing }

My bad on getchar(), phidget open() forks.. (althought I would very much advice against relying on a getchar() to quit)

First tip: Always check the return value of the open() call.

KUWill wrote:My problem is I need to write a program that will log the data (not hard when using the SpatialDataHandler) from multiple Phidgets (starting to get harder, multiple handles, writing multiple sets of data to file, etc)

If you want to use a separate FILE for each Phidget, you could very well send the FILE as userptr (or, even better, a struct including FILE and whatever else you might need per Phidget)

and then be able to divide up my files in chosen sizes for analysis later on.

fprintf returns how many bytes written, if you want to split on filesize. If you want to split on lines written, this is easy to count yourself.

eventually I want to do multiple Phidgets into multiple files, based on integers fed through scanf() when the program opens.

Not sure what you want to do with scanf. You have all info you need in argv[], don't you?But use sprintf and a counter to generate filenames, and you should have no problem handling multiple files and multiple Phidgets.

Added new program that gets me a CSV log from one spatial. How do I add Phidgets (one spatial would be enough to demonstrate the concept) and how do I make the file size configurable/create multiple files as a result of a "Full" file.

The reason why your 1st program doesn't ever write to a log is because index_counter is an instance variable, so it's value is not saved across calls to SpatialDataHandler() - you need to make it either static or global, and make sure you initialize to it 0 as well.

I'm trying to generate multiple handles because I have several Spatials connected. I'm not exactly sure what the function of the "= 0" section is. Can you explain it? Additionally, is there a way I can create multiple handles without explicitly calling them out? I'm thinking of somehow using the PhidgetManager to count the number of Spatials and then create an iterating name for assigning handles. For example, if I have 3 Spatials connected, the handles would be spatial1, spatial2, spatial3. I'm basically trying to figure out how to generate unique handles from the PhidgetManager data if I don't know what Phidgets are plugged in.

Additionally, is there a way I can create multiple handles without explicitly calling them out? I'm thinking of somehow using the PhidgetManager to count the number of Spatials and then create an iterating name for assigning handles.

This is possible, and the preferred way if you do not know how many Phidgets you have or their searial numbers.

For example, if I have 3 Spatials connected, the handles would be spatial1, spatial2, spatial3.

KUWill wrote:I'd like them to write with spatial 1 data in the first 4 columns, spatial 2 data in the next 4, spatial 3 data in the next 4, then a new line.

Phidgets guys will have to confirm this, but I GUESS that when you have 3 spatials connected, and you use CPhidgetSpatial_set_OnSpatialData_Handler, you are NOT guaranteed that the handlers are called in order.If you need data logged like "<spatial1>,<spatial2>,<spatial3>\n", I would much rather have a timed loop and poll for the values myself (instead of relying on the data handler).

If this is not good enough for your task, I fear you will have to housekeep the values in the data handler yourself, and log them Tetris-style (dump a line when you have data for all 3 spatials);

Can you confirm that this handler DOES allow the passing of one of the values in an array since an array would still have the CPhidgetSpatialHandle type?

In userptr, you mean? Userptr is a void*. You set this to whatever value you want when you set the data handler callback, and you get this very same value in all data handler callbacks. No magic involved.

frodegill is absolutely correct. The SpatialData events will be coming in at a set interval, but with no guarantee of order. Also, they are on separate threads, so it's not safe to access your file handle from them without any locking.

Depending on your data rate, you may be able to log polled data, but it's much better to log the event data as this way you get the corresponding timestamps and guarantee that all the data is from the same time interval.