Control USB ports

雨燕双飞 提交于 2019-12-18 07:09:31

问题


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:


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.



来源:https://stackoverflow.com/questions/1652698/control-usb-ports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!