windows 控制 USB 端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1652698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Control USB ports
提问by Raafat
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 ??
我可以使用 Ruby 或 C++ 将特定信号发送到 USB 端口吗,操作系统也是 Windows,所以这就像全新的 4 me(为 Windows 编程),所以我试图将它作为 DLL 文件来做,可以吗? DLL 包含 Ruby 代码 ??
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.
顺便说一下,这只是一个培训项目,所以没关系,我只是在 windows 下练习那些东西。
回答by johannes
I see two separate questions here. So I'll try to anwser them separatly.
我在这里看到两个不同的问题。所以我会尝试分别对它们进行分析。
How to control USB Devices from your code
如何通过代码控制 USB 设备
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/.
是的,您可以通过自己的代码控制 USB 设备。使用 libusb,您可以完成普通 USB 驱动程序所做的一切。请注意,您不是使用它们所在的端口来寻址它们,而是使用它们的制造商和设备 ID。在 linux 下,这适用于任何 USB 设备。在 Windows 下,您必须以某种方式安装 libusb 作为您不想控制的设备的驱动程序。在http://www.libusb.org/ 上查看有关 libusb 的更多信息。win32 的 libusb 托管在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.
如果您不想从 ruby 内部使用 libusb,则必须使用某种 c 绑定。您可以使用http://www.akr.org/ruby-usb/或编写自己的绑定。
How to compile ruby Code
如何编译 ruby 代码
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.
实际上有编译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/.
- 您可以编写一个包含 ruby 解释器和 eval 的 ruby 代码字符串的 C 程序。
- ruby2c 项目可以将一些 ruby 代码(不是全部)翻译成 c。这个c代码可以编译。在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.
您还应该考虑编译 ruby 代码在您的情况下是否有意义。如果您正在编写一个混合 c ruby 程序,您可以为 ruby 制作所有 c 部分 c 扩展。这样你的 ruby 就不需要编译了。你不想用 ruby 为 c 编写一个库吗?这可能意味着使用这个库的 c 程序会产生巨大的性能影响。