Skip to content

Commit

Permalink
#5 timeout on connection
Browse files Browse the repository at this point in the history
  • Loading branch information
djbios committed Jan 16, 2019
1 parent dbff54e commit 75cf414
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions CartridgeWriterForms/DeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,19 @@ private void LoadDevices()
//}
}

private bool PollForChip(SerialPort sp)
private bool PollForChip(SerialPort sp, int retries = 5)
{
byte[] buffer = new byte[1];
sp.Write("x");

// Pause for buffer to fill.
while (sp.BytesToRead < 1)
while (sp.BytesToRead < 1 && retries > 0)
{
retries--;
Thread.Sleep(10);
}
if (sp.BytesToRead == 0)
throw new Exception("Timeout");

sp.Read(buffer, 0, 1);
return Encoding.ASCII.GetString(buffer).Equals("p");
Expand Down
11 changes: 8 additions & 3 deletions CartridgeWriterForms/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ private void cmdRead_Click(object sender, EventArgs e)
MessageBox.Show("I need a Device and Printer Type before I can read.");
return;
}

c = dm.ReadCartridge(cboDevice.Text, Machine.FromType(cboPrinterType.Text));

try
{
c = dm.ReadCartridge(cboDevice.Text, Machine.FromType(cboPrinterType.Text));
}
catch (Exception)
{
MessageBox.Show("Timeout expired");
}
if (c == null)
return;
LoadControls();
Expand Down

0 comments on commit 75cf414

Please sign in to comment.