I've got a strange problem on one PC. My application uses the code below to open the port. For some reason the program doesn't report any problems but my writes report the port not open several times then I see a port open message just before the application locks up. It's fine on my production machine. Both are Windows 10. Or maybe I'm missing something basic and the code just keeps going after failing to open the port?
Here's the Open() code
And the sending code
Here's the Open() code
C#:
try
{
ArduinoPort.Open();
AddTextToTerminal("## " + comPort + "@" + baudRate + " opened."); // Display in status textbox
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException)
{
MessageBox.Show("Can't open " + comPort + " is it in use?",
ex.GetType().FullName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (ex is IOException)
{
MessageBox.Show("Can't open " + comPort + "\nPort name or settings invalid.", ex.GetType().FullName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Can't open " + comPort, ex.GetType().FullName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
And the sending code
C#:
if (ArduinoPort.IsOpen)
{
try
{
if (value.Length > 0) command += " " + value;
command = "%" + command; // All commands from configurator have this first for extra feedback ASCOM can't handle.
ArduinoPort.WriteLine(command);
}
catch (Exception ex)
{
if (ex is InvalidOperationException)
{
MessageBox.Show(ArduinoPort.PortName + " not open. is it in use?",
ex.GetType().FullName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (ex is ArgumentNullException)
{
MessageBox.Show("Attempt to send null value", ex.GetType().FullName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show(ArduinoPort.PortName + " error.", ex.GetType().FullName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
else
{
AddTextToTerminal("Serial port not open. (" + command + ")");
Disconnect();
MessageBox.Show(ArduinoPort.PortName + " unexpectedly closed?",
"Serial Port Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}