Hi all,
I use eclipse as my IDE of choice. Everything works well except when uploading to an Arduino Mega 2560. For some reason, avrdude does not reset the mega like it does for the uno.
I have looked high and low for a solution, but every suggestion I found seemed to fail.
So I wrote a simple wrapper for avrdude.exe that will automatically reset the arduino before running the real avrdude.exe
Installation:
- Create a folder called realavrdude (name is important!) in the directory where your current avrdude.exe resides.
- Move the real avrdude.exe into the folder you just created.
- Copy my avrdude.exe into the original folder.
How it works:
It is a simple wrapper which will take the COM port from the parameters and connect to the Arduino, it then toggles the DRT. This will reset your arduino.
It then calls realavrdude/avrdude.exe and passes the original parameters to it.
C# Code:
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Threading;
namespace avrdude
{
internal class Program
{
private const string RealAvrDude = "realavrdude\\avrdude.exe";
private static int Main(string[] args)
{
try
{
var reset = args.Any(x => x == "-pm2560" || x == "-patmega2560" || x == "-reset");
var p = new Process
{
StartInfo =
{
FileName = RealAvrDude,
Arguments = string.Join(" ", args.Where(x => x != "-wait" && x != "-reset")
.Select(x => string.Format("\"{0}\"", x))),
RedirectStandardOutput = true,
UseShellExecute = false
},
EnableRaisingEvents = true
};
p.OutputDataReceived += POutputDataReceived;
if (reset)
{
var com = args.FirstOrDefault(x => x.StartsWith("-P"));
if (com == null)
{
Console.WriteLine("Fake avrdude: Cannot find -P parameter. Deferring to real avrdude.");
Console.WriteLine();
}
else
{
com = com
.Replace("-P", "")
.Replace("\\", "")
.Replace(".", "");
Console.Write("Fake avrdude: Resetting device...");
using (var serial = new SerialPort(com))
{
serial.Open();
serial.DtrEnable = false;
serial.RtsEnable = false;
Thread.Sleep(100);
serial.DtrEnable = true;
serial.RtsEnable = true;
}
Console.WriteLine(" Done.");
Console.WriteLine();
}
}
else
{
Console.WriteLine("Fake avrdude: Deferring to real avrdude.");
Console.WriteLine();
}
Console.WriteLine(string.Format("Starting '{0}' {1}", RealAvrDude, p.StartInfo.Arguments));
Console.WriteLine();
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
if (args.Any(x => x == "-wait"))
{
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return 1;
}
return 0;
}
private static void POutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.Write(e.Data);
}
}
}
Hope this helps someone else as much as it has helped me!
-edit:
Modified code to only reset if -pm2560, -patmega2560 or -reset is found.
Edit RealAvrDude variable to point elsewhere if you like.