Hardware/TBalancer/TBalancerGroup.cs
changeset 32 a7f5e400f0da
parent 1 361e324a0ed4
child 33 800a36a1ca8e
     1.1 --- a/Hardware/TBalancer/TBalancerGroup.cs	Sun Feb 07 19:53:51 2010 +0000
     1.2 +++ b/Hardware/TBalancer/TBalancerGroup.cs	Sun Feb 07 20:11:30 2010 +0000
     1.3 @@ -39,24 +39,30 @@
     1.4  using System.Collections.Generic;
     1.5  using System.IO;
     1.6  using System.IO.Ports;
     1.7 +using System.Text;
     1.8  using System.Threading;
     1.9  
    1.10  namespace OpenHardwareMonitor.Hardware.TBalancer {
    1.11    public class TBalancerGroup : IGroup {
    1.12  
    1.13      private List<TBalancer> hardware = new List<TBalancer>();
    1.14 +    private StringBuilder report = new StringBuilder();
    1.15  
    1.16      public TBalancerGroup() {
    1.17 -
    1.18 -      string[] portNames = SerialPort.GetPortNames();
    1.19 +   
    1.20 +      string[] portNames = SerialPort.GetPortNames();      
    1.21        for (int i = portNames.Length - 1; i >= 0; i--) {
    1.22          try {
    1.23 +
    1.24            SerialPort serialPort =
    1.25              new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One);
    1.26            serialPort.Open();
    1.27            bool isValid = false;
    1.28            if (serialPort.IsOpen && serialPort.CDHolding &&
    1.29              serialPort.CtsHolding) {
    1.30 +
    1.31 +            report.Append("Port name: "); report.AppendLine(portNames[i]);            
    1.32 +
    1.33              serialPort.DiscardInBuffer();
    1.34              serialPort.DiscardOutBuffer();
    1.35              serialPort.Write(new byte[] { 0x38 }, 0, 1);
    1.36 @@ -65,32 +71,46 @@
    1.37                Thread.Sleep(100);
    1.38                j++;
    1.39              }
    1.40 -            if (serialPort.BytesToRead > 0 && 
    1.41 -              serialPort.ReadByte() == TBalancer.STARTFLAG) 
    1.42 -            {
    1.43 +            if (serialPort.BytesToRead > 0 &&
    1.44 +              serialPort.ReadByte() == TBalancer.STARTFLAG) {
    1.45                while (serialPort.BytesToRead < 284 && j < 5) {
    1.46                  Thread.Sleep(100);
    1.47                  j++;
    1.48                }
    1.49 -              if (serialPort.BytesToRead == 284) {
    1.50 +              int length = serialPort.BytesToRead;
    1.51 +              if (length >= 284) {
    1.52                  int[] data = new int[285];
    1.53                  data[0] = TBalancer.STARTFLAG;
    1.54                  for (int k = 1; k < data.Length; k++)
    1.55                    data[k] = serialPort.ReadByte();
    1.56 -                
    1.57 +
    1.58                  // check protocol version
    1.59 -                isValid = (data[274] == TBalancer.PROTOCOL_VERSION); 
    1.60 +                isValid = (data[274] == TBalancer.PROTOCOL_VERSION);
    1.61 +                if (!isValid) {
    1.62 +                  report.Append("Status: Wrong Protocol Version: 0x");
    1.63 +                  report.AppendLine(data[274].ToString("X"));
    1.64 +                }
    1.65 +              } else {
    1.66 +                report.AppendLine("Status: Wrong Message Length: " + length);
    1.67                }
    1.68 +            } else {
    1.69 +              report.AppendLine("Status: Wrong Startflag");
    1.70              }
    1.71            }
    1.72            serialPort.DiscardInBuffer();
    1.73            serialPort.Close();
    1.74            if (isValid) {
    1.75 +            report.AppendLine("Status: OK");
    1.76              hardware.Add(new TBalancer(portNames[i]));
    1.77              return;
    1.78            }
    1.79 -        } catch (IOException) { } catch (UnauthorizedAccessException) { } 
    1.80 -          catch (NullReferenceException) { }
    1.81 +        } catch (IOException ioe) {
    1.82 +          report.AppendLine(ioe.ToString());
    1.83 +        } catch (UnauthorizedAccessException uae) {
    1.84 +          report.AppendLine(uae.ToString());
    1.85 +        } catch (NullReferenceException ne) {
    1.86 +          report.AppendLine(ne.ToString());
    1.87 +        }
    1.88        }
    1.89      }
    1.90  
    1.91 @@ -101,7 +121,13 @@
    1.92      }
    1.93  
    1.94      public string GetReport() {
    1.95 -      return null;
    1.96 +      if (report.Length > 0) {
    1.97 +        report.Insert(0, "Serial Port T-Balancer" + Environment.NewLine +
    1.98 +          Environment.NewLine);
    1.99 +        report.AppendLine();
   1.100 +        return report.ToString();
   1.101 +      } else
   1.102 +        return null;
   1.103      }
   1.104  
   1.105      public void Close() {