views:

300

answers:

2

I use the excellent FileHelpers library when I work with text data. It allows me to very easily dump text fields from a file or in-memory string into a class that represents the data.

In working with a big endian microcontroller-based system I need to read a serial data stream. In order to save space on the very limited microcontroller platform I need to write raw binary data which contains field of various multi-byte types (essentially just dumping a struct variable out the serial port).

I like the architecture of FileHelpers. I create a class that represents the data and tag it with attributes that tell the engine how to put data into the class. I can feed the engine a string representing a single record and get an deserialized representation of the data. However, this is different from object serialization in that the raw data is not delimited in any way, it's a simple binary fixed record format.

FileHelpers is probably not suitable for reading such binary data as it cannot handle the nulls that show up and* I suspect that there might be unicode issues (the engine takes input as a string, so I have to read bytes from the serial port and translate them into a unicode string before they go to my data converter classes). As an experiment I have set it up to read the binary stream and as long as I'm careful to not send nulls it works quite well so far. It is easy to set up new converters that read the raw data and account for endian foratting issues and such. It currently fails on nulls and cannot process multiple records (it expect a CRLF between records).

What I want to know is if anyone knows of an open-source library that works similarly to FileHelpers but that is designed to handle binary data.

I'm considering deriving something from FileHelpers to handle this task, but it seems like there ought to be something already available to do this.

*It turns out that it does not complain about nulls in the input stream. I had an unrelated bug in my test program that came up where I expected a problem with the nulls. Should have investigated a little deeper first!

A: 

I haven't used filehelpers, so I can't do a direct comparison; however, if you have an object-model that represents your objects, you could try protobuf-net; it is a binary serialization engine for .NET using Google's compact "protocol buffers" wire format. Much more efficient than things like xml, but without the need to write all your own serialization code.

Note that "protocol buffers" does include some very terse markers between fields (typically one byte); this adds a little padding, but greatly improves version tolerance. For "packed" data (i.e. blocks of ints, say, from an array) this can be omitted if desired.

So: if you just want a compact output, it might be good. If you need a specific output, probably less so.

Disclosure: I'm the author, so I'm biased; but it is free.

Marc Gravell
This looks like it could be an ideal solution, if the code for generating the data that is very light-weight.I'm working with the Arduino, an AVR microcontroller based platform with only a few kilobytes of storage, so space is a primary concern. Currently I'm just dumping a struct out the serial port with no formatting at all, so the overhead is minimal.Any idea now big the C++ Protocol Buffers libraries are and if they can be compiled to an 8 bit environment?
At the moment I believe the recommendation is to use the C version for space-limited scenarios, but a new slimmer version is on the way: http://groups.google.com/group/protobuf/browse_thread/thread/5520b05194d73d76#. It might still be too big for that, though; I honestly don't know. And 8 bit is... challenging. I'm suspecting "no", unfortunately. Sorry.
Marc Gravell
A: 

When I am fiddling with GPS data in the SIRFstarIII binary mode, I use the Python interactive prompt with the serial module to fetch the stream from the USB/serial port and the struct module to convert the bytes as needed (per some format defined by SIRF). Using the interactive prompt is very flexible because I can read the string to a variable, process it, view the results and try again if needed. After the prototyping stage is finished, I have the data format strings that I need to put into the final program.

Your question doesn't mention anything about why you have a C# tag. I understand FileHelpers is a C# library, but I that doesn't tell me what environment you are working in. There is an implementation of Python for .NET called IronPython.

I realize this answer might mean you have to learn a new language, but having an interactive prompt is a very powerful tool for any programmer.

dwhall