Skip to content

Commit

Permalink
Improve serial error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
EarToEarOak committed Oct 22, 2015
1 parent bc3795a commit bf434f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
7 changes: 7 additions & 0 deletions NetRemote/ControlPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private void SerialControl()
if (_threadSerial == null)
{
_serial = new Serial(_parser, comboSerial.SelectedItem.ToString());
_serial.SerialError += OnSerialError;
_threadSerial = new Thread(new ThreadStart(_serial.Start));
_threadSerial.Start();
}
Expand All @@ -149,5 +150,11 @@ private void CheckChangedSerial(object sender, EventArgs e)
comboSerial.Enabled = !checkSerial.Checked;
SerialControl();
}

private void OnSerialError(object sender, EventArgs e)
{
_threadSerial = null;
checkSerial.Checked = false;
}
}
}
40 changes: 36 additions & 4 deletions NetRemote/Serial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace SDRSharp.NetRemote
{
class Serial
{
public event EventHandler SerialError;

private Parser _parser;
private string _data;
private string _port;
Expand Down Expand Up @@ -63,22 +65,31 @@ public void Start()
{
port.Open();
}
catch (IOException ex) {
MessageBox.Show(ex.Message, Info.Title(),
catch (IOException ex)
{
OnSerialError();
string msg = "Serial Port Error:\n" + ex.Message;
MessageBox.Show(msg, Info.Title() + "Serial Port Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Close(port);
return;
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message, Info.Title(),
OnSerialError();
string msg = "Serial Port Error:\n" + ex.Message;
MessageBox.Show(msg, Info.Title() + "Serial Port Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Close(port);
return;
}

Send(port, _parser.Motd());

_signal.Reset();
_signal.WaitOne();

port.Close();
Close(port);
}

public void Stop()
Expand Down Expand Up @@ -112,5 +123,26 @@ private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
_data = "";
}
}

private void Close(SerialPort port)
{
try
{
port.Close();
}
catch (IOException)
{
}

}

protected virtual void OnSerialError()
{
EventHandler handler = SerialError;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
}

0 comments on commit bf434f3

Please sign in to comment.