Hardware/TBalancer/TBalancer.cs
changeset 87 ecdc3bcef083
parent 86 b4f0f206173d
child 88 bce8363c119c
     1.1 --- a/Hardware/TBalancer/TBalancer.cs	Fri Apr 02 16:05:07 2010 +0000
     1.2 +++ b/Hardware/TBalancer/TBalancer.cs	Mon Apr 05 15:31:19 2010 +0000
     1.3 @@ -39,16 +39,14 @@
     1.4  using System.Collections.Generic;
     1.5  using System.Configuration;
     1.6  using System.Drawing;
     1.7 -using System.IO;
     1.8 -using System.IO.Ports;
     1.9  using System.Text;
    1.10  
    1.11  namespace OpenHardwareMonitor.Hardware.TBalancer {
    1.12    public class TBalancer : IHardware {
    1.13  
    1.14 -    private string portName;
    1.15 +    private int portIndex;
    1.16 +    private FT_HANDLE handle;
    1.17      private Image icon;
    1.18 -    private SerialPort serialPort;
    1.19      private byte protocolVersion;
    1.20      private Sensor[] digitalTemperatures = new Sensor[8];
    1.21      private Sensor[] analogTemperatures = new Sensor[4];
    1.22 @@ -68,8 +66,8 @@
    1.23      private delegate void MethodDelegate();
    1.24      private MethodDelegate alternativeRequest;    
    1.25  
    1.26 -    public TBalancer(string portName, byte protocolVersion) {
    1.27 -      this.portName = portName;
    1.28 +    public TBalancer(int portIndex, byte protocolVersion) {
    1.29 +      this.portIndex = portIndex;
    1.30        this.icon = Utilities.EmbeddedResources.GetImage("bigng.png");
    1.31        this.protocolVersion = protocolVersion;
    1.32  
    1.33 @@ -108,12 +106,8 @@
    1.34  
    1.35        alternativeRequest = new MethodDelegate(DelayedAlternativeRequest);
    1.36  
    1.37 -      try {
    1.38 -        serialPort = new SerialPort(portName, 19200, Parity.None, 8,
    1.39 -          StopBits.One);
    1.40 -        serialPort.Open();
    1.41 -        Update();
    1.42 -      } catch (IOException) { }      
    1.43 +      Open();
    1.44 +      Update(); 
    1.45      }
    1.46  
    1.47      private void ActivateSensor(Sensor sensor) {
    1.48 @@ -171,10 +165,10 @@
    1.49      private void ReadData() {
    1.50        int[] data = new int[285];
    1.51        for (int i = 0; i < data.Length; i++)
    1.52 -        data[i] = serialPort.ReadByte();
    1.53 +        data[i] = FTD2XX.ReadByte(handle);
    1.54  
    1.55        if (data[0] != STARTFLAG) {
    1.56 -        serialPort.DiscardInBuffer();   
    1.57 +        FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_RX);   
    1.58          return;
    1.59        }
    1.60  
    1.61 @@ -258,8 +252,7 @@
    1.62      }
    1.63  
    1.64      public string Identifier {
    1.65 -      get { return "/bigng/" + 
    1.66 -        this.portName.TrimStart(new char[]{'/'}).ToLower(); }
    1.67 +      get { return "/bigng/" + this.portIndex; }
    1.68      }
    1.69  
    1.70      public IHardware[] SubHardware {
    1.71 @@ -275,7 +268,7 @@
    1.72  
    1.73        r.AppendLine("T-Balancer bigNG");
    1.74        r.AppendLine();
    1.75 -      r.Append("Port Name: "); r.AppendLine(serialPort.PortName);
    1.76 +      r.Append("Port Index: "); r.AppendLine(portIndex.ToString());
    1.77        r.AppendLine();
    1.78  
    1.79        r.AppendLine("Primary System Information Answer");
    1.80 @@ -318,31 +311,32 @@
    1.81      }
    1.82  
    1.83      private void DelayedAlternativeRequest() {
    1.84 -      System.Threading.Thread.Sleep(500);
    1.85 -      try {
    1.86 -        if (serialPort.IsOpen)
    1.87 -          serialPort.Write(new byte[] { 0x37 }, 0, 1);
    1.88 -      } catch (Exception) { }
    1.89 +      System.Threading.Thread.Sleep(500);      
    1.90 +      FTD2XX.Write(handle, new byte[] { 0x37 });
    1.91      }
    1.92  
    1.93 -    public void Update() {      
    1.94 -      try {
    1.95 -        while (serialPort.IsOpen && serialPort.BytesToRead >= 285)
    1.96 -          ReadData();
    1.97 -        if (serialPort.BytesToRead == 1)
    1.98 -          serialPort.ReadByte();
    1.99 +    public void Open() {
   1.100 +      FTD2XX.FT_Open(portIndex, out handle); 
   1.101 +      FTD2XX.FT_SetBaudRate(handle, 19200);
   1.102 +      FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
   1.103 +      FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11,
   1.104 +        0x13);
   1.105 +      FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
   1.106 +      FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
   1.107 +    }
   1.108  
   1.109 -        serialPort.Write(new byte[] { 0x38 }, 0, 1);
   1.110 -        alternativeRequest.BeginInvoke(null, null);
   1.111 -      } catch (InvalidOperationException) {
   1.112 -        foreach (Sensor sensor in active)
   1.113 -          sensor.Value = null;
   1.114 -      }      
   1.115 +    public void Update() {
   1.116 +      while (FTD2XX.BytesToRead(handle) >= 285)
   1.117 +        ReadData();
   1.118 +      if (FTD2XX.BytesToRead(handle) == 1)
   1.119 +        FTD2XX.ReadByte(handle);
   1.120 +
   1.121 +      FTD2XX.Write(handle, new byte[] { 0x38 });
   1.122 +      alternativeRequest.BeginInvoke(null, null);
   1.123      }
   1.124  
   1.125      public void Close() {
   1.126 -      if (serialPort.IsOpen)
   1.127 -        serialPort.Close();
   1.128 +      FTD2XX.FT_Close(handle);
   1.129      }
   1.130  
   1.131      public event SensorEventHandler SensorAdded;