private void OpenSerialPort_Click(object sender, EventArgs e)
{
if (IsSerialPortClosed())
{
timer1.Enabled = false;
var port = comPortSelector.SelectedItem?.ToString();
if (port == null)
{
textOutput.Text = @"Please select a port to open";
return;
}
serialPort.PortName = port;
try
{
serialPort.Open();
}
catch
{
textOutput.Text = @"Selected serial port is currently in use. Close whatever application is currently using it and try again.";
return;
}
try
{
serialPort.ReadTimeout = 5000;
Text = serialPort.ReadLine();
serialPort.ReadTimeout = 1000;
while (!serialPort.ReadLine().Contains("Initializing SD card"))
{
}
}
catch
{
Text = "";
serialPort.Close();
textOutput.Text = @"Sorry, this loader only works on the APU Mega shield. :(";
return;
}
MakeDirectoryListing(ReadLine());
//50 milliseconds for reading a bunch of lines for the directory entries.
//5 seconds for uploading spcs, and waiting for a response, and during initial serial port open
// as it takes 2 seconds for the arduino bootloader to jump into the sketch code.
textOutput.Text = @"Serial Port " + comPortSelector.SelectedItem + @" Opened Successfully" + Environment.NewLine;
OpenSerialPort.Text = @"Close Serial Port";
comPortSelector.Enabled = false;
RefreshSerialPorts.Enabled = false;
timer1.Enabled = true;
}
else
{
_filetreedepth = 0;
DirectoryView.Nodes.Clear();
_direntries[0].Clear();
serialPort.Close();
OpenSerialPort.Text = @"Open Serial Port";
comPortSelector.Enabled = true;
RefreshSerialPorts.Enabled = true;
}
}