Jump to content
Cat

Converting C structures into LabVIEW

Recommended Posts

I'm guessing the answer to this is "no", but thought I'd ask anyway, just in case...

I'm (probably going to be) tasked to develop Yet Another data collector/analyzer. I'll be reading (via TCP) and parsing data from many different platforms. The main collection and analysis algorithms will be the same for all the platforms, but the "headers", to use the C term, that define the message formats will be different for each platform. There are around a hundred of these message types and the header files for them are already written in C. Is there any way, other than slogging thru each of them by hand, to turn the structures in C header files into clusters in LabVIEW (or whatever else might be appropriate)?

I haven't played with scripting any, but might I be able to use that to generate this header code? It would probably take me longer to write the scripting code than to actually do the conversion by hand, but it would be a lot more fun. :)

If the answer's "no" all around, then converting header files by hand might be a good project for one of my cow-orkers who's dying to take on some LV programming... [insert evil laugh] :P

Share this post


Link to post
Share on other sites

I haven't played with scripting any, but might I be able to use that to generate this header code? It would probably take me longer to write the scripting code than to actually do the conversion by hand, but it would be a lot more fun. :)

I have zero scripting experience, so can't comment on that. But I think you hit it with the time comment, anything you develop will take longer than drudging through the headers by hand. Of course then there's maintenance and validation to account for, but, well, you know.

I do have some code that dynamically creates cluster types at will using variants, where each contained element is another properly typed variant. Works recursively and everything (clusters can contain other clusters, arrays, or other "container" types). It does the job by playing with the binary type strings that you get when you flatten a variant. By manipulating those you can pretty much create anything. The rub being of course, that to convert it back to a native LabVIEW data, you need the LabVIEW type, which means you wouldn't need to go through the exercise to begin with. Damned if you do, damned if you don't really.

But variants suck, so you don't want to do any of that anyways. Stupid variants

Share this post


Link to post
Share on other sites

Can you do an intermediate step and convert the structs to XML, for instance?

Share this post


Link to post
Share on other sites

Here's a little startup:

StructToVariant.vi

This VI will try to read a Typedef and return it's contents as a cluster with the right elements.

Together with the Variant to Control (with which I would like to integrate this) you can convert a typedef-string into a (typedef) control file).

I have tested the converter with the following typedef string:

typedef struct {  uint32 header_sz;  uint32 width;  uint32 height;  uint16 nplanes;  uint16 bitspp;  uint32 compress_type;  uint32 bmp_bytesz;  uint32 hres;  uint32 vres;  uint32 ncolors;  char nimpcolors;} bmp_dib_v3_header_t;

and the following Enum struct:

typedef enum {  BI_RGB = 0,  BI_RLE8 =3,  BI_RLE4,  BI_BITFIELDS,  BI_JPEG,  BI_PNG,} bmp_compression_method_t;

It supports sparse Enum's for instance.

It however does not support nested structs (reference to another), that might be something to add.

If you make any changes I would like to see them here.

But variants suck, so you don't want to do any of that anyways. Stupid variants

As said earlier, have you looked at the Variant To Control tool-VI?

Ton

  • Like 2

Share this post


Link to post
Share on other sites

Now that's pretty cool. I hadn't seen that before.

As for XML intermediates, the code I was talking about actually was part of a generic XML reader/writer...so yes! I haven't released the code though, not sure if I can...

Share this post


Link to post
Share on other sites

I haven't played with scripting any, but might I be able to use that to generate this header code? It would probably take me longer to write the scripting code than to actually do the conversion by hand, but it would be a lot more fun. :)

Fun indeed. If you have the time I would take some of it to learn scripting. Half of my fun with LV is finding when I can turn something tedious into a coding challenge.

  • Like 1

Share this post


Link to post
Share on other sites

There are around a hundred of these message types and the header files for them are already written in C. Is there any way, other than slogging thru each of them by hand, to turn the structures in C header files into clusters in LabVIEW (or whatever else might be appropriate)?

Can you (or an associate) create a 'dummy' dll with functions that use these structures as inputs?

If so, you might be able to use the Import Shared Library Wizard to create VIs for you that contain the clusters. No LabVIEW coding required... shifty.gif

(the only thing that sucks worse than variants is pointers!)

Share this post


Link to post
Share on other sites

I think you hit it with the time comment, anything you develop will take longer than drudging through the headers by hand. Of course then there's maintenance and validation to account for, but, well, you know.

The up side of spending the time is that there will be more platforms added in the future. So if I take the time now, it will be automated in the future. If it's possible to do in the first place.

Here's a little startup:

Thanks, Ton! Yes, I will need to have to deal with deeply nested structures. If I go forward with this, I'll post those additions.

Even if I can do this with scripting, you've started the parsing part for me. That will be a big help.

Can you (or an associate) create a 'dummy' dll with functions that use these structures as inputs?

That was my first thought when this was proposed to me. I know how to deal with dlls. But when I asked my C programming cow-orkers if they could make a dll for me, I just got back a blank stare. Of course, then I had to say that in LabVIEW, creating dlls is a piece of cake. :)

Share this post


Link to post
Share on other sites

Half of my fun with LV is finding when I can turn something tedious into a coding challenge.

It would definitely be a challenge! But one of the things I got out of NIWeek is that I have to start thinking outside of my cozy LabVIEW 7.1 box. Taking this on would definitely do that.

Share this post


Link to post
Share on other sites

Hi,

 

I'm new here but I am looking to do something similar. I am trying to automate the conversion of structures (in C typedef format) into LabVIEW typedef .ctl files. I need to support nested structures (some elements of the top level structure are themselves structures). I could just as easily export my structures into XML or some other text-based format also (the C typedefs are autogenerated already from Matlab scripts that I can modify). The only data type I need is double.

 

I am starting to look at the StructToVariant.vi tool in combination with VariantToControl, but I think it was already mentioned that it does not support nested structures.

 

Has anyone done any further work on this and would you be willing to share what you have found?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.