制作一个 USB 设备,用 Java 控制它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2159253/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:45:15  来源:igfitidea点击:

Make a USB Device, Control It In Java

javaarduinoinput-devices

提问by Dan Rosenstark

I'm thinking about making a physical controller (device?) with knobs, buttons, and LEDs. I'd like to interact with it using Java (respond to the knobs, light up LEDs, etc). The reason I mention Java is two-fold: first, I know Java well1. Second, I've written the rest of the program I need to interface with in Java (though there are ways to talk to the Java program from another language).

我正在考虑制作一个带有旋钮、按钮和 LED 的物理控制器(设备?)。我想使用 Java 与它交互(响应旋钮、点亮 LED 等)。我提到 Java 的原因有两个:首先,我很了解 Java 1。其次,我已经用 Java 编写了我需要与之交互的程序的其余部分(尽管有一些方法可以用另一种语言与 Java 程序对话)。

I would like the device to connect via USB and be (computer-)platform independent.

我希望设备通过 USB 连接并且独立于(计算机)平台。

I haven't the slightest idea of where to start, except to start reading the Arduino website. Is this my best/only option?Is there something better suited for communicating with Java?

除了开始阅读 Arduino 网站之外,我对从哪里开始一无所知。这是我最好/唯一的选择吗?有没有更适合与 Java 通信的东西?

Note:I know that Arduino has something to do with Java (not sure what), but it seems like code must be written in a subset of C.

注意:我知道 Arduino 与 Java 有关(不确定是什么),但似乎代码必须用 C 的子集编写。

How would I get moving on this topic?

我将如何继续讨论这个话题?


1 - No laughter, please.


1 - 请不要笑。

采纳答案by R Samuel Klatchko

The Arduino development environment is written in Java.

Arduino 开发环境是用 Java 编写的。

But the standard language you write a program for the Arduino platform is effectively C++.

但是您为 Arduino 平台编写程序的标准语言实际上是 C++。

The Arduino platform is based on an Atmel AVR chip. There is at least one Java VM for AVR chips. There are other languages available for the AVR such as Forthand BASIC (although I could only find commercial versions, so I'll if you want to find them, search for "AVR BASIC").

Arduino 平台基于 Atmel AVR 芯片。至少有一个用于 AVR 芯片的 Java VM。AVR还有其他可用的语言,例如Forth和BASIC(虽然我只能找到商业版本,所以如果你想找到它们,我会搜索“AVR BASIC”)。

The Arduino uses a virtual COM port to communicate between the host computer and it. A virtual COM port emulates an old style serial line but is done with USB. You can use the Java communication APIto then have a Java program running on the host computer communicate with your physical device.

Arduino 使用虚拟 COM 端口在主机和它之间进行通信。虚拟 COM 端口模拟旧式串行线,但使用 USB 完成。您可以使用Java 通信 API,然后让运行在主机上的 Java 程序与您的物理设备进行通信。

回答by Ben Combee

For some encoders and buttons, you probably want to implement a USB HID device. If you're going to produce more than a couple of them, you'll want to do a custom board. Check out V-USB, an open-source library for making USB HID devices using Atmel microcontrollers. They have a bunch of examples of projects that use this library.

对于某些编码器和按钮,您可能想要实现 USB HID 设备。如果您要生产多个以上的产品,则需要制作定制板。查看V-USB,这是一个使用 Atmel 微控制器制作 USB HID 设备的开源库。他们有一堆使用这个库的项目示例。

You could probably make this look like a HID joystick, using the encoders to produce X/Y axis information and having the buttons act like buttons. They you could use standard Java gaming APIs to read the joystick values.

您可以将其设计为 HID 操纵杆,使用编码器生成 X/Y 轴信息,并使按钮像按钮一样工作。您可以使用标准的 Java 游戏 API 来读取操纵杆值。

回答by Jherico

Can you expand on your need for a custom device? It seems to me that designing hardware has a pretty high barrier to entry and that most applications I can think of would be better resolved by repurposing an existing piece of game controller hardware. If you really need new hardware, then i suggest you start by googling 'USB development kit' or 'USB development board' which will get you links like this, thisand this.

您能否扩展您对定制设备的需求?在我看来,设计硬件的门槛相当高,我能想到的大多数应用程序都可以通过重新利用现有的游戏控制器硬件来更好地解决。如果您真的需要新硬件,那么我建议您首先使用谷歌搜索“USB 开发工具包”或“USB 开发板”,它们将为您提供类似这样这样这样的链接。

As for working with USB hardware from Java, I've played around with the JUSB library a bit and it looks promising, but appears to be limited to Linux only because the Windows version of the native library half of the library hasn't been written. Alternatives can by found by googling 'HID java'.

至于使用来自 Java 的 USB 硬件,我已经使用了 JUSB 库,它看起来很有希望,但似乎仅限于 Linux,因为该库一半的本地库的 Windows 版本尚未编写. 可以通过谷歌搜索“HID java”找到替代方案。

回答by jean88

sample for java usb connection to freescale microcontroller:

java usb 连接到飞思卡尔微控制器的示例:

http://javausbapi.blogspot.com/

http://javausbapi.blogspot.com/

回答by t0mm13b

Ok, computer-platform independant? What platforms are you targetting? That would depend on the driver and support for that said platform, what does the usb device do? Is it a mass storage device...You may have to look around and see if you can find a device driver that can talk to the device...

好的,独立于计算机平台?你的目标是什么平台?这将取决于该平台的驱动程序和支持,USB 设备有什么作用?它是一个大容量存储设备吗……你可能需要四处看看,看看是否能找到一个可以与设备对话的设备驱动程序……

Hope this helps, Best regards, Tom.

希望这会有所帮助,最好的问候,汤姆。

回答by Matt

I know for the serial port there were libraries that existed for interacting with it (rs232 library). Googling for java and USB returned several answers (the first was called jUSB). That would be the first type of thing I would be looking for.

我知道对于串行端口,存在用于与其交互的库(rs232 库)。谷歌搜索 java 和 USB 返回了几个答案(第一个被称为 jUSB)。那将是我要寻找的第一种类型。