2

I am working on an Project in which i have to upload hex file from java to arduino uno i wrote the code in java its working but only when i call it in main method. But when i call it when button is pressed it doesn't upload the hex.

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/main.fxml"));
    Parent root = loader.load();
    MainController controller = (MainController) loader.getController();
    Scene scene = new Scene(root,485,355);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Hello World!");
    primaryStage.show();

    controller.Init(primaryStage);
    controller.searchForPorts();
    controller.disableControls();
}

public static void main(String[] args) {
     Avr avr = new Avr();
    avr.start();
    launch(args);
}

}

Working fine like this but when called on button pressed event doesn't work.

public class Avr extends Thread {
String avrdude;
String avrconf;
String hex ;

MainController main;
void setup() throws IOException{
    Path avrpath = Paths.get("avrdude.exe");
    Path hexpath = Paths.get("sketch_may13b.ino.hex");
    Path avrconfpath = Paths.get("avrdude.conf");
    Path realavrpath = avrpath.toRealPath(LinkOption.NOFOLLOW_LINKS);
    Path realhexpath = hexpath.toAbsolutePath();
    Path realavrconfpath = avrconfpath.toAbsolutePath();

    avrdude = realavrpath.toString();
    avrconf = realavrconfpath.toString();
    hex  = realhexpath.toString();

    avrdude = avrdude.replace("\\","\\\\");
    avrconf = avrconf.replace("\\","\\\\");
    hex = hex.replace("\\","\\\\");
    main = new MainController();

    String port ="COM3";
        runCommand(new String[]{avrdude, "-C"+avrconf,"-v","-v","-v","-v","-patmega328p","-carduino","-P"+port,"-b115200","-D","-Uflash:w:"+hex+":i"});

  }
  void runCommand(String[] cmd){

try {

Runtime.getRuntime().exec(cmd);

} catch (IOException e) {

      System.out.println("something went wrong: \n");
      e.printStackTrace();
  }

  }  

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        setup();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

0 Answers0