Guten Abend,
Ich möchte über JavaFX einen String an einen Arduino Uno senden. Dieser String soll dann auf einem 16x2 LCD angezeigt werden.
Beim betätigen des Sendebuttons reagiert der Uno, jedoch bleibt die Anzeige unverändert bei "Hallo, ich warte...".
Verwendet wird die jSSC-Library.
JavaFX-Code:
Arduino Code:
Über ein wenig Hilfe würde ich mich freuen.
Ich möchte über JavaFX einen String an einen Arduino Uno senden. Dieser String soll dann auf einem 16x2 LCD angezeigt werden.
Beim betätigen des Sendebuttons reagiert der Uno, jedoch bleibt die Anzeige unverändert bei "Hallo, ich warte...".
Verwendet wird die jSSC-Library.
JavaFX-Code:
Java:
package sample;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaFX_jSSC extends Application {
ObservableList<String> portList;
private void detectPort(){
portList = FXCollections.observableArrayList();
String[] serialPortNames = SerialPortList.getPortNames();
for(String name: serialPortNames){
System.out.println(name);
portList.add(name);
}
}
@Override
public void start(Stage primaryStage) {
detectPort();
final ComboBox comboBoxPorts = new ComboBox(portList);
final TextField textFieldOut = new TextField();
Button btnSend = new Button("Send");
btnSend.setOnAction( s -> {
if(comboBoxPorts.getValue() != null &&
!comboBoxPorts.getValue().toString().isEmpty()){
String stringOut = textFieldOut.getText();
try {
SerialPort serialPort =
new SerialPort(comboBoxPorts.getValue().toString());
serialPort.openPort();
serialPort.setParams(
SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.writeBytes(stringOut.getBytes());
serialPort.closePort();
} catch ( SerialPortException ex) {
Logger.getLogger(
JavaFX_jSSC.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
System.out.println("No SerialPort selected!");
}
});
VBox vBox = new VBox();
vBox.getChildren().addAll(
comboBoxPorts,
textFieldOut,
btnSend);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("J-A-Kommu");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Arduino Code:
Code:
//LiquidCrystal-Bibliothek einrichten
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String charsIn = "";
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Hallo,");
lcd.setCursor(0, 1);
lcd.print("ich warte...");
}
void loop() {
while(Serial.available()){
char charRead = Serial.read();
charsIn.concat(charRead);
delay(3);
}
if(charsIn != ""){
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print(charsIn);
charsIn = "";
}
}
Über ein wenig Hilfe würde ich mich freuen.