Ik wil een LEDin- en uitschakelen met een Java-programma. Ik heb het project in C# in ongeveer 5 minuten gedaan, maar het lijkt iets uitdagender in Java. Ik liet de Arduino wachten tot een 1 of 0 werd geschreven naar de COM-poorten zou de LED op basis daarvan veranderen. De code die ik gebruik voor de Arduino is als volgt.
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);
}
}
Hoe zou ik dit doen met een Java-toepassing?
Antwoord 1, autoriteit 100%
Je kunt de JArduino (Java-Arduino)-bibliotheek gebruiken, die een Java API biedt om je Arduino te besturen via een seriële poort (met een USB-kabel, of draadloze apparaten die zich softwarematig als seriële poorten gedragen), UDP ( via een ethernetschild). Alle code met betrekking tot communicatie tussen Java en Arduino wordt intern beheerd door de bibliotheek.
Hier is een Java-voorbeeld om een LED te laten knipperen:
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 is beschikbaar op: jarduino
Antwoord 2, Autoriteit 50%
Om te communiceren met een COMP-poort in Java, hebt u een implementatie van de Java-communicatie nodig Api . Ik kan attesteren op rxtx , ik heb het eerder gebruikt om te communiceren met een Arduino.
Als u eenmaal uw Java-communicatie-implementatie hebt, wordt het vrij eenvoudig om te communiceren met een 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
}
De RXTX-website heeft ook andere voorbeelden[2] die u misschien nuttig vindt.
Antwoord 3, autoriteit 50%
Je kunt eenvoudig Arduino-programma’s in Java bouwen, dankzij de uitstekende HaikuVM.
Hier is een voorbeeld:
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
}
}