Hallo liebes Forum,
ich versuche momentan einen String über die serielle Schnittstelle vom JavaFX-Programm an den Arduino Uno zu schicken, um ihn dann auf einem 16x2 LCD anzuzeigen. Leider flackert nach dem betätigen des Sendebuttons die LCD nur kurz auf und zeigt den gleichen Text, der beim Setup voreingestellt wurde.
Über Hilfe würde ich mich sehr freuen.
JavaFX:
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 | InterruptedException 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:
//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();
Serial.print(charRead);
charsIn.concat(charRead);
delay(3);
}
if(charsIn != ""){
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print(charsIn);
charsIn = "";
}
}