2

Is there a way to compile and upload an C program generated from a C# program into an arduino board.

Basically i've builded a little C# application which generate an C code depending somes parameters selected into the window. For now you have to copy and paste the code inside your ardunio interface to compile, and upload the program.

I want to automate this task and send directly the C code générated inside the arduino board without use a arduino program or anything else. only my window interface. Is it possible ?

Program picture : enter image description here

user2305302
  • 31
  • 1
  • 3

4 Answers4

7

As datafiddler points out in his answer, you'll need to call avr-gcc first (in order to compile your program).

As an alternative for the second step (the upload process) you could use ArduinoSketchUploader, a native C# library to upload the binary HEX file to the Arduino through it's bootloader. This way, you don't have to ship / wrap avrdude with your code.

Disclaimer: this is a library I have personally written.

Once the nuget package ArduinoUploader is referenced, the resulting code would look like this:

var uploader = new ArduinoSketchUploader(
    new ArduinoSketchUploaderOptions()
    {
        FileName = @"C:\MyHexFiles\UnoHexFile.ino.hex",
        PortName = "COM3",
        ArduinoModel = ArduinoModel.UnoR3
    });
uploader.UploadSketch();
Paul Efford
  • 261
  • 4
  • 12
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • ChristopheR, that could be a solution, so i have to find a solution to create the .ino.hex before. I never explored this part of C# but i think is a acceptable way for me. Thank you. – user2305302 Nov 12 '16 at 00:20
  • Can your library upload as well hex code for the Esp32 family? – Paul Efford Mar 21 '21 at 18:54
1

You do not upload C code, but you use avr-gcc to compile it to machine code. Additionally, the Arduino IDE does some preparation to produce a .cpp file from the .ino file (generate function prototypes, add include files) Then the utility avrdude is used to upload the resulting .hex file

Look at extended output while compiling and uploading.

In general it is possible, but the way you ask I doubt you will be successful. As you are working with c#, look for a VisualStudio plugin to compile and upload to an Arduino from within VisualStudio ( search for VisualMicro ). Perhaps that's interesting for you...

datafiddler
  • 1,755
  • 3
  • 17
  • 30
  • Thank you for the response. I watch about VisualMicro. that's seem to be hard for me to use it ! Thank you guy – user2305302 Nov 08 '16 at 17:42
  • You can start with the IDE then import a sketch to a VS project. Within VS you can edit (much better intellisense support), compile and upload. Of course that's a completely different approach than your question/idea, but it's as easy as using the Arduino IDE – datafiddler Nov 09 '16 at 20:44
  • VisualMicro is a licensed plugin with just a 45 days trial period – Paul Efford Mar 21 '21 at 18:37
1
Code used to compile and install arduino in c# :

DosyaYolu is a arduino .ino folder path
`myPath` is a file path where arduinon is installed.

string myPath = @"C:\Program Files (x86)\Arduino\arduino"; 
System.Diagnostics.Process p = new System.Diagnostics.Process(); 
p.StartInfo.FileName = myPath;
p.StartInfo.Arguments = @"--upload " + DosyaYolu; 
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
0

You can download the Arduino software for non admin install as a folder and use this a resource for your C# program. Then you can just invoke the compiler in the background with the CMD properties set to cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.StartInfo.CreateNoWindow = true;

Then do the same for the uploader.

This way everything is being done automatically in the background without the user of the program noticing.