【问题标题】:Control an Arduino with Java使用 Java 控制 Arduino
【发布时间】:2012-08-14 22:56:12
【问题描述】:

我希望使用 Java 程序打开和关闭 LED。我在大约 5 分钟内用 C# 完成了这个项目,但在 Java 中似乎更具挑战性。我让 Arduino 等待将 1 或 0 写入COM port,然后根据此更改 LED。我用于 Arduino 的代码如下。

int LedPin = 13;
char data;

void setup()
{
    Serial.begin(9600);
    pinMode( LedPin , OUTPUT );
}

void loop()
{
    data = Serial.read();
    if (Serial.available() > 0)
    {
        if(data == '1' )
        {
            digitalWrite(LedPin,HIGH);
        }
        else if(data == '0' )
        {
            digitalWrite(LedPin,LOW);
        }
    }
    else
        if (Serial.available()<0)
        {
            digitalWrite(LedPin,HIGH);
            delay(500);
            digitalWrite(LedPin,LOW);
            delay(500);
        }
}

我将如何使用 Java 应用程序做到这一点?

【问题讨论】:

  • @Jon Ardunio 语言肯定不是 Java。 Arduino 语言基于Wiring,并在 C/C++ 中实现。你可能会想到Processing
  • @Jeffrey,你是对的。抱歉……

标签: java eclipse arduino jarduino


【解决方案1】:

您可以使用 JArduino (Java-Arduino) 库,它提供了一个 Java API 来使用串行端口(使用 USB 电缆,或从软件角度表现为串行端口的无线设备)、UDP(通过以太网屏蔽)。所有与Java和Arduino通信相关的代码都由库内部管理。

Here is a Java sample to blink an LED:

public class Blink extends JArduino {

public Blink(String port) {
    super(port);
}

@Override
protected void setup() {
    // initialize the digital pin as an output.
    // Pin 13 has an LED connected on most Arduino boards:
    pinMode(DigitalPin.PIN_12, PinMode.OUTPUT);
}

@Override
protected void loop() {
    // set the LED on
    digitalWrite(DigitalPin.PIN_12, DigitalState.HIGH);
    delay(1000); // wait for a second
    // set the LED off
    digitalWrite(DigitalPin.PIN_12, DigitalState.LOW);
    delay(1000); // wait for a second
}

public static void main(String[] args) {
    String serialPort;
    if (args.length == 1) {
        serialPort = args[0];
    } else {
        serialPort = Serial4JArduino.selectSerialPort();
    }
    JArduino arduino = new Blink(serialPort);
    arduino.runArduinoProcess();
}
}

JArduino 位于:JArduino

【讨论】:

【解决方案2】:

为了与 Java 中的通信端口进行通信,您需要一些 Java Communications API 的实现。我可以证明RXTX,我以前用它来与Arduino通信。

一旦您实现了 Java 通信,与 Arduino 通信就变得相当简单:

CommPort arduino = getArduinoPort();
arduino.getOutputStream().write(1);

public CommPort getArduinoPort() {
    Enumeration ports = CommPortIdentifier.getPortIdentifiers();
    while(ports.hasMoreElements()) {
        CommPortIdentifier identifier = (CommPortIdentifier) ports.nextElement();
        if(isArduino(identifier)) {
            return identifier.open(getClass().getName(), 2000); // 2 second timeout
        }
    }
    return null;
}

public boolean isArduino(CommPortIdentifier identifier) {
    // if you know the name of the port ahead of time you can
    // compare it here with identifier.getName(), otherwise
    // you can interface with the user like the Arduino IDE's
    // serial monitor
}

RXTX 网站还有其他 examples [2] 可能对您有用。

【讨论】:

  • 令牌“getOutputStream”的语法错误,标识符应在此令牌之后
  • 另外我似乎得到了很多“gnu.io.rxtx.properties 没有被检测到。”
  • @user1116969 您应该将前两行放在方法体中。我没有收到那个错误,我使用的是 Ubuntu 12.04 x86_64
  • Code 我的代码...它输出的内容Output
  • @user1116969 RXTX 似乎不支持您的操作系统。您使用的是什么操作系统?
【解决方案3】:

感谢优秀的HaikuVM,您可以轻松地用 Java 构建 Arduino 程序。

这是一个例子:

import static processing.hardware.arduino.cores.arduino.Arduino.*;

public class Blink {
    static byte ledPin = 13;            // LED connected to digital pin 13

    public static void setup() {
        pinMode(ledPin, OUTPUT);        // sets the digital pin as output
    }

    public static void loop()           // run over and over again
    {
        digitalWrite(ledPin, HIGH);     // sets the LED on
        delay(500);                    // waits for a second
        digitalWrite(ledPin, LOW);      // sets the LED off
        delay(500);                    // waits for a second
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2013-03-12
    • 2014-05-21
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多