Re: ParallelIO (mathlink windows program) Help Please
- To: mathgroup at smc.vnet.net
- Subject: [mg61776] Re: [mg61763] ParallelIO (mathlink windows program) Help Please
- From: Todd Gayley <tgayley at wolfram.com>
- Date: Sat, 29 Oct 2005 01:32:42 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hu,
Rather than address your specific questions about C++, I am going to point
out that there is a _much_ easier way to call inpout32.dll than writing
your own MathLink program. .NET/Link gives Windows users the ability to
call DLL functions directly from Mathematica by simply writing a one-line
function declaration much like you would write in Visual Basic or C#. For
this example:
<<NETLink`
ParallelRead = DefineDLLFunction["Inp32", "d:\\inpout32.dll", "void",
{"short"}];
ParallelWrite = DefineDLLFunction["Out32", "d:\\inpout32.dll", "void",
{"short", "short"}];
Then call the functions as before:
ParallelWrite[888, 1234]
No C++ code, no .tm files, no compilers.
Todd Gayley
Wolfram Research
At 02:26 AM 10/28/2005, Zhe Hu wrote:
>Inspired by SerialIO (a mathlink program accessing the serial port), I tried
>to have Mathematica be able to read/write the parallel port on the Windows
>XP PC.
>
>There is a graceful solution (built upon native device driver) to read/write
>parallel port under Windows XP at http://www.logix4u.net/inpout32.htm. All
>you need is a dll (no need to install or anything), which exports both inp32
>that reads the parallel port and outp32 that writes to the port.
>
>So the rest of the work seemed quite textbooky at first thought. (I use VC++
>6.0 and Mathlink V4r11)
>
>(0) define the template file as:
>
>:Begin:
>:Function: Inp32
>:Pattern: ParallelRead[port_]
>:Arguments: {port}
>:ArgumentTypes: {ShortInteger}
>:ReturnType: ShortInteger
>:End:
>
>:Begin:
>:Function: Out32
>:Pattern: ParallelWrite[port_, data_]
>:Arguments: {port, data}
>:ArgumentTypes: {ShortInteger, ShortInteger}
>:ReturnType: Manual
>:End:
>
>(1) write the entry point as:
>
>int main(int argc, char* argv[])
>{
>return MLMain(argc, argv);
>}
>
>(2) wrap the built exe file in a *.m file as
>BeginPackage["ParallelIO`"];
>
>ParallelRead::usage=""
>
>ParallelWrite::usge=""
>
>Begin["`Private`"];
>
>$Link = Install["ParallelIO"];
>
>End[]; (* `Private` *)
>
>EndPackage[];
>
>=======================================
>
>However things weren't so smooth at all.
>
>(1) if all the files are in C, the VC linker just can't find Inp32 and
>Out32, though I definitely incorporate the lib file. So I have to rename
>each file to *.cpp, including "mprep parallel.tm <http://parallel.tm> -o
>parallel.cpp" which is against the "rule"
>
>(2) The original definition
>short _stdcall Inp32(short PortAddress);
>void _stdcall Out32(short PortAddress, short data);
>
>has to be wrapped into
>
>short mInp32(short Port)
>{
>return Inp32(Port);
>}
>
>void mOut32(short Port, short data)
>{
>Out32(Port, data);
>}
>
>(3) Then MathLink got stuck once I do ParallelWrite[888, (someNumber)] in
>Mathematica. So I have to change to
>int mOut32(short Port, short data)
>{
>Out32(Port, data);
>return 0;
>}
>
>&
>
>:Begin:
>:Function: mOut32
>:Pattern: ParallelWrite[port_, data_]
>:Arguments: {port, data}
>:ArgumentTypes: {ShortInteger, ShortInteger}
>:ReturnType: Integer
>:End:
>
>====================================
>
>The surprise is that after all these twists of trial and error, the thing
>worked.
>
>So my confusion is that:
>(1) How well does MathLink code go with C++? I don't mean to use class, jus=
>t
>work with other C++ files in VC++ compiler. (There is pretty much
>contradictory discussion that can be googled already. Please someone at
>least show some sample VC++ project files)
>(2) If a C/C++ function return type is "void", is that the ":ReturnType:"
>should be "Manual". But then does this function need to send some package
>back or free something at the end, otherwise the link seems to get stuck.
>
>I'd love to send my source code, etc. in a zipped file to anyone who would
>be interested in taking a look.
>
>Thanks a lot.
>
>Sincerely,
>Hu Zhe