Hardware/TBalancer/TBalancer.cs
changeset 83 3fdadd4a830f
parent 75 be9bbc350884
child 86 b4f0f206173d
     1.1 --- a/Hardware/TBalancer/TBalancer.cs	Fri Mar 26 20:58:10 2010 +0000
     1.2 +++ b/Hardware/TBalancer/TBalancer.cs	Sat Mar 27 12:57:09 2010 +0000
     1.3 @@ -326,17 +326,23 @@
     1.4      }
     1.5  
     1.6      public void Update() {
     1.7 -      while (serialPort.BytesToRead >= 285)
     1.8 -        ReadData();
     1.9 -      if (serialPort.BytesToRead == 1)
    1.10 -        serialPort.ReadByte();
    1.11 +      try {
    1.12 +        while (serialPort.IsOpen && serialPort.BytesToRead >= 285)
    1.13 +          ReadData();
    1.14 +        if (serialPort.BytesToRead == 1)
    1.15 +          serialPort.ReadByte();
    1.16  
    1.17 -      serialPort.Write(new byte[] { 0x38 }, 0, 1);
    1.18 -      alternativeRequest.BeginInvoke(null, null);
    1.19 +        serialPort.Write(new byte[] { 0x38 }, 0, 1);
    1.20 +        alternativeRequest.BeginInvoke(null, null);
    1.21 +      } catch (InvalidOperationException) {
    1.22 +        foreach (Sensor sensor in active)
    1.23 +          sensor.Value = null;
    1.24 +      }
    1.25      }
    1.26  
    1.27      public void Close() {
    1.28 -      serialPort.Close();
    1.29 +      if (serialPort.IsOpen)
    1.30 +        serialPort.Close();
    1.31      }
    1.32  
    1.33      public event SensorEventHandler SensorAdded;