Untitled
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.io.OutputStream; import java.io.InputStream; import java.io.IOException; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; public class BLHeliUpdater extends Application { private static final byte[] firmwareData = new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // Добавете останалите байтове на фърмуера тук }; private TextField portField; private Button updateButton; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("BLHeli 32.10 Updater"); portField = new TextField(); portField.setPromptText("Serial Port"); updateButton = new Button("Update ESC"); updateButton.setOnAction(e -> updateFirmware()); VBox vbox = new VBox(10, new Label("Serial Port:"), portField, updateButton); vbox.setPadding(new Insets(20)); Scene scene = new Scene(vbox, 300, 150); primaryStage.setScene(scene); primaryStage.show(); } private void updateFirmware() { String portName = portField.getText(); if (portName.isEmpty()) { showAlert(Alert.AlertType.ERROR, "Input Error", "Please enter a serial port."); return; } SerialPort serialPort = connectToESC(portName); if (serialPort == null) { return; } String response = sendCommand(serialPort, "BOOTLOADER"); if (response.contains("READY")) { uploadFirmware(serialPort); } else { showAlert(Alert.AlertType.ERROR, "Error", "ESC is not ready for update."); } serialPort.close(); } private SerialPort connectToESC(String portName) { try { CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName); SerialPort serialPort = (SerialPort) portId.open("BLHeliUpdater", 2000); serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); return serialPort; } catch (Exception e) { showAlert(Alert.AlertType.ERROR, "Connection Error", "Failed to connect to " + portName + ": " + e.getMessage()); return null; } } private void uploadFirmware(SerialPort serialPort) { try { OutputStream out = serialPort.getOutputStream(); showAlert(Alert.AlertType.INFORMATION, "Uploading", "Firmware upload started..."); int blockSize = 64; for (int i = 0; i < firmwareData.length; i += blockSize) { byte[] block = new byte[Math.min(blockSize, firmwareData.length - i)]; System.arraycopy(firmwareData, i, block, 0, block.length); out.write(block); Thread.sleep(50); } showAlert(Alert.AlertType.INFORMATION, "Success", "Firmware uploaded successfully!"); } catch (Exception e) { showAlert(Alert.AlertType.ERROR, "Upload Error", "Failed to upload firmware: " + e.getMessage()); } } private String sendCommand(SerialPort serialPort, String command) { try { OutputStream out = serialPort.getOutputStream(); InputStream in = serialPort.getInputStream(); out.write(command.getBytes()); Thread.sleep(100); byte[] buffer = new byte[in.available()]; in.read(buffer); return new String(buffer); } catch (IOException | InterruptedException e) { showAlert(Alert.AlertType.ERROR, "Communication Error", "Failed to send command: " + e.getMessage()); return ""; } } private void showAlert(Alert.AlertType alertType, String title, String message) { Alert alert = new Alert(alertType); alert.setTitle(title); alert.setContentText(message); alert.showAndWait(); } }
Leave a Comment