1

I'm trying to upload a hex file from Java/Processing but running into some issues. Here is my code so far, based on the Arduino IDE's upload verbose output:

void setup(){
    //C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avrdude -CC:\Program Files (x86)\Arduino/hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -PCOM96 -b115200 -D -Uflash:w:C:\Users\HM\AppData\Local\Temp\build5909267154049588263.tmp/sketch_aug13a.cpp.hex:i
    String ArduinoPath = "C:\\Program Files (x86)\\Arduino";
    String hexPath = "C:\\Users\\HM\\AppData\\Local\\Temp\\build1023547366107161384.tmp/sketch_aug13a.cpp.hex";
    String port = "COM96";
    runCommand(new String[]{ArduinoPath+"/hardware/tools/avr/bin/avrdude", "-C"+ArduinoPath+"/hardware/tools/avr/etc/avrdude.conf","-v","-v","-v","-v","-patmega328p","-carduino","-P"+port,"-b115200","-D","-Uflash:w:"+hexPath+":i"});
  }
  void runCommand(String[] cmd){

    String s = null;

    try {

      Process p = Runtime.getRuntime().exec(cmd);
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
      BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

      // read the output from the command
      System.out.println("command out:\n");
      while ( (s = stdInput.readLine ()) != null) System.out.println(s);
      System.out.println("errors (if any):\n");
      while ( (s = stdError.readLine ()) != null) System.out.println(s);

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

  }

If I run this from command line it uploads without any issues, but from the code above I always get this output:

avrdude: Version 5.11, compiled on Sep  2 2011 at 19:38:36
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardw
are\tools\avr\etc\avrdude.conf"

         Using Port                    : COM96
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: ser_open(): can't open device "\\.\COM96": Access is denied.


avrdude done.  Thank you.
| improve this question | | | | |
  • What output do you get? You forgot to post it in your question! – jfpoilpret Aug 14 '14 at 16:20
  • 3
    Is "COM96" really the correct device? If it is, is this sketch being run in the same way as your other test, or it is perhaps running a less privileged mode? Do you still have the Arduino IDE (perhaps its serial monitor) open and claiming the port? – Chris Stratton Aug 14 '14 at 17:00
  • I've disconnected the port to see if I get a different message from avrdude and I do. COM96 is correct, otherwise I get The system can't find the file specified. If I use the Arduino IDE or command prompt to do the upload, the upload seems to work, it's when trying to replicate it from Java this problem arises. Does the Arduino IDE do anything clever after uploading calling avrdude to release the port ? – George Profenza Aug 14 '14 at 18:09
  • Have you tried hard coding the value of port in your command string. try "-PCOM96" instead of "-P"+port. Other than that, it says access is denied, try running it as administrator – Doowybbob Aug 14 '14 at 18:33
  • oooh...haven't tried that as administrator yet. That kind of rings a bell because Arduino and avrdude are in Program Files so would have admin privileges – George Profenza Aug 14 '14 at 18:47
1

I'd first would try to find out the right command when running on command line (cmd.exe).

When you launch an external command from Java which generates output, you should/must read the output (p.getInputStream(), p.getErrorStream()) using separate threads. Otherwise your application might hang for no obvious reason.

| improve this answer | | | | |
  • I've made two separate thread as advised and it worked! – George Profenza Sep 4 '14 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.