如何从C#应用程序编译并上传到Arduino

时间:2016-11-08 13:29:30

标签: c# upload arduino atmel

有没有办法将C#程序生成的C程序编译并上传到arduino板。

基本上我已经构建了一个C#应用程序,它根据窗口中选择的某些参数生成C代码。现在你必须在ardunio界面中复制并粘贴代码进行编译,然后上传程序。

我想自动执行此任务,并直接发送arduino板内的C代码générated,而不使用arduino程序或其他任何东西。只有我的窗口界面。可能吗 ?

节目图片: enter image description here

4 个答案:

答案 0 :(得分:2)

您不上传C代码,但使用avr-gcc将其编译为机器代码。 此外,Arduino IDE做了一些准备从.ino文件生成.cpp文件(生成函数原型,添加包含文件) 然后使用实用程序avrdude上传生成的.hex文件

在编译和上传时查看扩展输出。

总的来说,这是可能的,但你问的方式我怀疑你会成功。 在使用c#时,查找VisualStudio插件以编译并从VisualStudio中上传到Arduino(搜索VisualMicro)。 也许这对你很有意思......

答案 1 :(得分:2)

正如datafiddler在他的回答中指出的那样,你需要先调用avr-gcc(为了编译你的程序)。

作为第二步(上传过程)的替代方法,您可以使用ArduinoSketchUploader,一个本机C#库,通过它的引导加载程序将二进制HEX文件上传到Arduino。这样,您就不必使用代码发送/包装avrdude

免责声明:这是我亲自撰写的图书馆。

引用nuget包后,生成的代码如下所示:

var uploader = new ArduinoSketchUploader(
    new ArduinoSketchUploaderOptions()
    {
        FileName = @"C:\MyHexFiles\UnoHexFile.ino.hex",
        PortName = "COM3",
        ArduinoModel = ArduinoModel.UnoR3
    });
uploader.UploadSketch();

答案 2 :(得分: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();

答案 3 :(得分:0)

您可以将非管理员安装的Arduino软件下载为文件夹,并将其用作C#程序的资源。然后,您可以在后台调用编译器,并将CMD属性设置为 cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.StartInfo.CreateNoWindow = true;

然后对上传者执行相同的操作。

这样,程序的所有操作都会在后台自动完成,而无需用户注意。