c++arduinowindows-consoleavrdude

C++ System(); function not working as expected / Windows console commands in C++


Hello friendly people of stack overflow!

I am currently working on a project using an Arduino Uno. Because i create all my files and sketches using a c++ program, i want to eliminate the Arduino IDE from my workflow. For that i can very easily use avrdude (which the IDE uses anyway) and some windows console commands. These are the commands that i am using:

"C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude" "-CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\Users\Jzargo\AppData\Local\Temp\arduino_build_766345/EPaper_TestDither.ino.hex:i

"EPaper_TestDither.ino" is the arduino Sketch i want to compile and upload. When using the console and manually inserting the above commands, everything works as expected.

And here comes the part I am struggeling with:

Because i also dont want the user to manually open the console and type in some gibberish code, i want to integrate this command into my c++ program using the system(); function:

system("\"C:\\Program Files(x86)\\Arduino\\hardware\\tools\\avr/bin/avrdude\" \" - CC:\\Program Files(x86)\\Arduino\\\hardware\\tools\\avr/etc/avrdude.conf\" -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\\Users\\Jzargo\\AppData\\Local\\Temp\\arduino_build_766345/EPaper_TestDither.ino.hex:i");

When executing this function, the command cannot be executed because "Der Befehl "C:\Program" ist entweder falsch geschrieben oder konnte nicht gefunden werden.", which roughly translates to "The Command "C:\Program" is not written correctly or cant be found".

I do not understand why the console accepts the command when manually inserting it, but not when using the system(); function.

I hope you can help me figure this out.

Edit: By using subst H: "C:\Program Files(x86)\Arduino\hardware\tools\avr\bin" and

system("\"H:/avrdude \"-CC:/Program Files (x86)/Arduino/hardware/tools/avr/etc/avrdude.conf\"\" -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:/Users/Jzargo/AppData/Local/Temp/arduino_build_833906/EPaper_TestDither.ino.hex:i");

I was able to upload my sketch. Note the changed Placement of \".

But for some reason, this does not work when using C:\Program Files(x86)\Arduino\hardware\tools\avr\bin instead of H:.

Kindest regards

J'zargo


Solution

  • The command looks messed up with respect to the parameters, although I don't see how exactly that triggers your specific error.

    The beginning is OK. The path is properly quoted (double quotes, protected by backslashes from the C compiler). But why do you have slashes and backslashes mixed? In some online examples I saw that people use forward slashes in Windows paths (C:/whatever...) ; that seems to work and is easier than using double backslashes all the time (but it should not trigger your — or any — error).

    So system("\"C:\\Program Files(x86)\\Arduino\\hardware\\tools\\avr/bin/avrdude\" ... should call the right executable. Why don't you try that on its own (without parameters) to see whether the error persists?

    I suspect that \" - CC:\\Program Files(x86)\\ ... is not correct though. avrdude expects a parameter -C<path>, not - C<path> (note the badly placed spaces before and after the dash).

    As an aside, it may not hurt to quote parameters that contain funny characters like colons which may have special meanings.

    The general advice for this kind of trouble:

    Edit: I'm not sure this example is valid because I used the msys2 development environment and ran the example in a bash shell; I'm not even sure cmd is called as the system shell by the syste call!

    In order to check the system call semantics I wrote the following minimal example (which uses mixed slashes/backslashes as a test). The current directory has a sub directory called "some dir" containing a minimal program showargs which simply writes its command line parameters to stdout:

    $ ls -l "some dir" && echo && cat cmdline.c && echo && gcc -o cmdline cmdline.c && ./cmdline.exe
    total 56
    -rwxr-xr-x 1 Peter None 56097 Apr 16 17:23 showargs.exe
    
    #include <stdlib.h>
    int main(int argc, char **argv)
    {
            system("\".\\some dir/showargs\" 1 2 3");
    }
    
    ->.\some dir/showargs<-
    ->1<-
    ->2<-
    ->3<-