How can I get/fetch a response from invoked (launch() ) command?

I used launch() to invoke a batch script. The batch script would than launch Arduino-cli to build and upload an arduino program to a board.

That was this fine working line.
launch("rundll32 SHELL32.DLL,ShellExec_RunDLL " + sketchPath("") +"\\flashArduino.bat") ;

In order to get the correct COM port as well as board model. I would like to fetch the output generated by arduino-cli. This allow me to use the board list command to figure out which COM port I should use. The native processing's Serial.list() is not satisfactory:

[0] "CNCA2"
[1] "CNCB2"
[2] "COM4" 
[3] "COM7" 
[4] "COM8" 
[5] "COM9" 

So my question, how can I launch arduino-cli and fetch it’s respons from within a processing sketch?

launch("arduino-cli upload arduinoProgram -b arduino:avr:uno -p COM4") ;

I tried the above line for an upload, but the upload does not happen. The line itself works fine when running from a batch terminal.

Kind regards,

Bas

I learned that I need to use the BufferReader class in combination with the InputStreamReader class. One must import the appropiate modules doe this.

void flashProgram()
{
    String myPath = sketchPath() ;
    String boardName ;
    String COM_PORT ;
    String command ;
    String line ;
    String jsonData = "";

    String arduinoCliPath = myPath + "\\Arduino-cli\\arduino-cli.exe " ;
    String sketchPath     = myPath + "arduinoProgram" ;
    String listCommand    = "board list --format json" ;

    try
    {
        command = arduinoCliPath + listCommand ;
        Process p = launch(command);
        BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream()));

This succesfully launches arduino-cli and fetches the data that arduino-cli produces.

2 Likes

@bask185, Very nice and useful. Was the code in post 2 meant to be a complete working sketch? I think it’s truncated.

No the code was just the relevant snippet. I use several files and my main is often big so posting complete programs in something other than a zip file is not really doable

1 Like