Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
- To: mathgroup at smc.vnet.net
- Subject: [mg86920] Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
- From: "Szabolcs HorvÃt" <szhorvat at gmail.com>
- Date: Wed, 26 Mar 2008 04:53:40 -0500 (EST)
- References: <fs4rmb$p3s$1@smc.vnet.net> <47E8DDE8.9050406@gmail.com>
On Tue, Mar 25, 2008 at 6:22 PM, Karen Bindash <karenbindash at googlemail.com> wrote: > Hi, > Thank you for emailing me directly - it is faster than using the newsgroup. > > Unfortunately, I don't fully understand your question, but I hope the > following example of how I would use the function in C will help. I'll > call the function write_to_hardware and another read_from_hardware. I > do in fact want to use both functions in Mathematica, although I have > only asked about one so far. > > main() > { > void *huminity_value; I think that variable names are messed up a bit. I suppose that this should be void *result, right? > double humidity, temperature; > int days_since_calibration; > int error_code; > > humidity=malloc(5000); /* all data received is under 5000 bytes */ And this is result = malloc(5000); Is this correct? > error_code=write_to_hardware("RESET",5); > error_code=write_to_hardware("HUMIDITY",9); /* take a measurement of > humidity */ > error_code=read_from_hardware(result,5000); /* read humidity */ > humidity=atof(result); > > error_code=write_to_hardware("TEMPERATURE",11); /* take a measurement > of temperature */ > error_code=read_from_hardware(result,5000); /*read tempeature */ > temperature=atof(result); > > printf("humidity=%lf temperature=%lf \ n",humidity, temperature); > > } > > > > The C functions definitions are > > int write_to_hardware( const void *data,long length_of_command_in_bytes); > int read_from_hardware(void *data, long size_of_buffer_to_store_results); If you always work with character strings, why don't you use char * instead of void *? > > > In Mathematica I'd like to do something like > > WriteToHardware("RESET", 5) > WriteToHardware("HUMIDITY", 9); > humidity=ReadFromHardware(); > (humidity would be a Mathematica string in this case) This is not correct Mathematica syntax. In Mathematica functions are called with f[], not f(). This Mathematica interface looks OK except that it is not necessary to pass the string length to WriteToHardware. A Mathematica string is visible as a char * and and int (its length) on the C side (see the reverse example). > > But the point to note is that whilst in these examples the data sent > was text, and the data received was text, this might not always be the > case. Potentially the data sent might be a binary file which > calibrates the thermometer. You still need to represent the data in some way in Mathematica. You can represent it as strings, or you can represent it as a list of integers (bytes). Probably the former will be more useful. ToCharacterCode and FromCharacterCode can convert between the two representations. Did you take a look at the MathLink example programs? There is one for working with strings (reverse) and one for working with integer arrays (sumalist). You could use these examples as starting points. All you have to do is create C wrapper functions that call read_from_hardware() and write_to_hardware(), and make these wrapper functions accessible from Mathematica. > > Does that make it any clearer? >