views:

419

answers:

1
+2  Q: 

Control USB ports

can i send specific signals to USB ports using Ruby or C++, also the operating system is Windows, so this is like totally new 4 me (to program for windows), so i'm trying to do it as a DLL file, can this DLL contain Ruby code ??

by the way this is just a training project, so it dosn't matter that much, i'm just practicing on those stuff under windows.

+1  A: 

I see two separate questions here. So I'll try to anwser them separatly.

How to control USB Devices from your code

Yes you can control USB Devices from your own code. With libusb you can do everything ordinary usb drivers do. Be aware you don't address them with the port they are on, but with their manufacterer and device ID. Under linux this work for any usb device. Under windows your have to somehow install libusb as a driver for the device you wan't to control. See more about libusb at http://www.libusb.org/. The libusb for win32 is hosted at http://libusb-win32.sourceforge.net/.

If you wan't to use libusb from inside ruby you have to use some kind of c bindings. You could ether use http://www.a-k-r.org/ruby-usb/ or write your own bindings.

How to compile ruby Code

This is not my field of expertise, but as you asked both questions as one. I will try to anwser it.

There are actually ways of compiling ruby.

  • You could write a C program which includes the ruby interpreter and eval's a string of ruby code.
  • The ruby2c project can translate some ruby code (not all) into c. This c code could be compiled. See more at http://rubyforge.org/projects/ruby2c/.

You should also think about, whether it makes sense in your case to compile the ruby code. If you are writing a mixed c ruby program, you could make all c parts c extensions for ruby. This way your ruby could does not need to be compiled. Do you wan't to write a library for c in ruby? This would probably mean a huge performance impact for the c programs using this library.

johannes