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

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Can your library upload as well hex code for the Esp32 family?
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...

3 Comments

Thank you for the response. I watch about VisualMicro. that's seem to be hard for me to use it ! Thank you guy
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
VisualMicro is a licensed plugin with just a 45 days trial period
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();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.