LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

ANN: NI-DAQmx Base library wrapper for Ruby language

[this was also posted to Multifunction DAQ discussion board; apologies if you've already seen it before or see it again in the near future]

The National Instruments NI-DAQmx Base C API for data acquisition supports most National Instruments data acquisition hardware. Although it's more limited than the full (i.e. non-Base) API, it also is more portable across operating systems.

I have written a library wrapper for the NI-DAQmx Base library. This lets you write applications in a scripting language (currently only Ruby) that talk to the library. I'm using it for general purpose work with my USB-6009 devices, but it should work with any supported device. The initial focus of this project is support for the NI USB-6008/6009 devices using the Ruby programming language. So some of the API that doesn't apply to the USB-6009 may not be wrapped yet (though I think I covered the whole API).

Library errors that are reported as error codes in C throw exceptions (complete with descriptive strings) in Ruby. You can use Ruby threads to make your life easier; for instance, I have one program at work where I use one thread for continuous analog input, another for analog output, and another for digital I/O. These are synchronized together. There is another thread accepting user input from the command line and performing various actions, and another thread doing data logging.

For an example of the flavor of a program using this wrapper, I have attached the source for an example program that continuously displays analog input voltages and lets you type in values for analog outputs. (Unfortunately, this board doesn't let me use the .rb extension, so I had to use .txt)

I have provided this work as Open Source (under the Apache LIcense 2.0).

I have written the wrapper using the SWIG wrapper generator; SWIG supports a number of languages besides Ruby. I'd love to extend the support to other scripting languages, but this is my first SWIG project and I don't know all the ins and outs of the language yet. As a result, my code is Ruby-specific in places (though it is easy to support other languages). If you'd prefer to have it support (say) Perl, you're welcome to help. (I moved from Perl to Ruby several years ago and haven't looked back.)

The NI-DAQmx Base library is available for Windows, Mac OS X, and Linux, as well as certain PDAs. I have tested the code in its current version with NI-DAQmx Base version 2.20f under Mac OS/X and Windows XP, as well as in an earlier version with Linux.

The code is hosted at Google Code. You can get the source at: http://code.google.com/p/daqmxbase-swig/

For Subversion checkout, do this:

svn co http://daqmxbase-swig.googlecode.com/svn/daqmx

Volunteers are welcome, as (of course) is feedback and bug reporting.

Ned Konz
Message 1 of 11
(7,900 Views)

Welcome to the forums. It's fairly rare to see someone sign up and immediately start writing long high quality replies, so thanks for that. It's always good to have such additions to the community.

I suggest you also post this to the LAVA forums, as there might be people there interested in this. Personally, I don't work with Ruby, so I can't help you there.

You might also wish to check out this blog post which demos using the DAQmx API with Python.

P.S. For some fun, check out the BreakPoint.


___________________
Try to take over the world!
0 Kudos
Message 2 of 11
(7,888 Views)
Thanks for the welcome.

And thanks for the pointer. I wish I'd known that someone else had written a SWIG wrapper for the library; it would have saved me some time!

Anyhow, since I used SWIG, it would be relatively easy to support any of these languages that SWIG supports already:
  • Tcl 8.0 and newer versions.
  • Python 1.5 and newer.
  • Perl 5.003 or newer.
  • Guile 1.3.4 and newer.
  • Java JDK 1.1 and newer.
  • Ruby.
  • Mzscheme.
  • PHP
  • Objective Caml (Ocaml)
  • Pike
  • C#
  • the Chicken scheme compiler
  • Allegro CL
  • Modula-3
  • Lua
  • CLISP
  • Common Lisp with UFFI
  • Common Lisp with CFFI
  • R
  • JavaScript using SwigJS
  • D using swig4d
  • Eiffel using SWIGEiffel

0 Kudos
Message 3 of 11
(7,867 Views)
Please consider posting this to the LAVA Code Repository here:

http://forums.lavag.org/downloads.html

We'd love to see it over there.


Message Edited by Michael Aivaliotis on 02-02-2008 08:07 PM


Michael Aivaliotis
VI Shots LLC
0 Kudos
Message 4 of 11
(7,859 Views)


@Michael Aivaliotis wrote:
Please consider posting this to the LAVA Code Repository here:

http://forums.lavag.org/downloads.html

We'd love to see it over there.



What category would you prefer?
0 Kudos
Message 5 of 11
(7,853 Views)
There is now a hardware category.


Michael Aivaliotis
VI Shots LLC
0 Kudos
Message 6 of 11
(7,843 Views)

Ned,

Whilst I'd normally condemn cross-posting, it might be nice to re-post your announcement in one of the CVI/C++/C# forums as I'm sure a lot of the people there might be very interested in a scripting interface to NI-DAQ.

Great job by  the way!

Paul

0 Kudos
Message 7 of 11
(7,736 Views)

Hi ned-

Nice work!  I'll have to give your libraries a try when I get some downtime- this is something I have been interested in trying for a while.

On a side note, I notice that you worked around the 'DAQmxReadBinaryI32' bug in the DAQmx Base header file.  FYI- I fixed that "bug" for the DAQmx Base 3.1 release which is now available for Mac, Linux, and Windows/PDA.

Tom W
National Instruments
0 Kudos
Message 8 of 11
(7,731 Views)
Thanks for sharing, Ned!
Amanda Howard
Americas Services and Support Recruiting Manager
National Instruments
0 Kudos
Message 9 of 11
(7,693 Views)
Hi Ned,

I've just tested your library. It took me a while to understand  how to do it, as I never used SWIG before.
Anyway this is what I did:

0. setup a NI USB-6008 with some AA battery connected to AI0 (see attached photo)
1. installed Cygwin (I work on Win2000)
2. setup .paths in the "vimrc" file.
3. run "make -f Makefile.swig "
4. run "ruby test.rb"

It worked, but didn't show correct values. I changed settings inside the file, but still wrong numbers.
I then tried to run other examples and I couldn't run them (I guess the fault is on my side in ruby).
However I managed to run acquireNscans.rb , and it worked perfect.

It took me quite long time today to do. I tried to compile it without installing cygwin, then I compiled it in cygwin, but SWIG seems to be out-of-date, etc.etc. but at the end of the day I've got some results.

$ ruby acquireNscans.rb
[1.38554652487558, 1.37531881852636, 1.37531881852636, ...

read 10, total time: 0.907, rate: 11.0253583241455
Aborted (core dumped)


Is there any forum/topic where others discuss issues or work on the code similar to yours ?
What I am interested in is a library that would allow me to use Ruby or Php to communicate with devices through GPIB, Ethernet , serial port, etc.

Cheers,
Jakub



Message Edited by jcz on 02-15-2008 03:31 PM
0 Kudos
Message 10 of 11
(7,117 Views)