Hardware/TBalancer/TBalancerGroup.cs
changeset 87 ecdc3bcef083
parent 51 e5fc5fd57dbe
child 131 b68cb7c8d5ce
     1.1 --- a/Hardware/TBalancer/TBalancerGroup.cs	Fri Apr 02 16:05:07 2010 +0000
     1.2 +++ b/Hardware/TBalancer/TBalancerGroup.cs	Mon Apr 05 15:31:19 2010 +0000
     1.3 @@ -49,79 +49,87 @@
     1.4      private StringBuilder report = new StringBuilder();
     1.5  
     1.6      public TBalancerGroup() {
     1.7 -   
     1.8 -      string[] portNames = SerialPort.GetPortNames();      
     1.9 -      for (int i = portNames.Length - 1; i >= 0; i--) {
    1.10 -        try {
    1.11  
    1.12 -          SerialPort serialPort =
    1.13 -            new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One);
    1.14 -          
    1.15 -          bool isValid = false;
    1.16 -          byte protocolVersion = 0;
    1.17 -          report.Append("Port Name: "); report.AppendLine(portNames[i]);
    1.18 +      uint numDevices;
    1.19 +      try {
    1.20 +        FTD2XX.FT_CreateDeviceInfoList(out numDevices);
    1.21 +      } catch (DllNotFoundException) { return; } 
    1.22 +        catch (ArgumentNullException) { return; }
    1.23 +     
    1.24 +      FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
    1.25 +      FTD2XX.FT_GetDeviceInfoList(info, ref numDevices);
    1.26  
    1.27 -          try {
    1.28 -            serialPort.Open();
    1.29 -          } catch (UnauthorizedAccessException) {
    1.30 -            report.AppendLine("Exception: Access Denied");
    1.31 -          }
    1.32 -       
    1.33 -          if (serialPort.IsOpen) {
    1.34 -            if (serialPort.CtsHolding) {
    1.35 -              serialPort.DiscardInBuffer();
    1.36 -              serialPort.DiscardOutBuffer();
    1.37 -              serialPort.Write(new byte[] { 0x38 }, 0, 1);
    1.38 -              int j = 0;
    1.39 -              while (serialPort.BytesToRead == 0 && j < 2) {
    1.40 -                Thread.Sleep(100);
    1.41 -                j++;
    1.42 -              }
    1.43 -              if (serialPort.BytesToRead > 0) {
    1.44 -                if (serialPort.ReadByte() == TBalancer.STARTFLAG) {
    1.45 -                  while (serialPort.BytesToRead < 284 && j < 5) {
    1.46 -                    Thread.Sleep(100);
    1.47 -                    j++;
    1.48 -                  }
    1.49 -                  int length = serialPort.BytesToRead;
    1.50 -                  if (length >= 284) {
    1.51 -                    byte[] data = new byte[285];
    1.52 -                    data[0] = TBalancer.STARTFLAG;
    1.53 -                    for (int k = 1; k < data.Length; k++)
    1.54 -                      data[k] = (byte)serialPort.ReadByte();
    1.55 +      for (int i = 0; i < numDevices; i++) {
    1.56 +        report.Append("Device Index: "); report.AppendLine(i.ToString());
    1.57 +        
    1.58 +        FT_HANDLE handle;
    1.59 +        FT_STATUS status;
    1.60 +        status = FTD2XX.FT_Open(i, out handle);
    1.61 +        if (status != FT_STATUS.FT_OK) {
    1.62 +          report.AppendLine("Open Status: " + status);
    1.63 +          continue;
    1.64 +        }
    1.65  
    1.66 -                    // check protocol version 2X (protocols seen: 2C, 2A, 28)
    1.67 -                    isValid = (data[274] & 0xF0) == 0x20;
    1.68 -                    protocolVersion = data[274];
    1.69 -                    if (!isValid) {
    1.70 -                      report.Append("Status: Wrong Protocol Version: 0x");
    1.71 -                      report.AppendLine(protocolVersion.ToString("X"));
    1.72 -                    }
    1.73 -                  } else {
    1.74 -                    report.AppendLine("Status: Wrong Message Length: " +length);
    1.75 -                  }
    1.76 -                } else {
    1.77 -                  report.AppendLine("Status: Wrong Startflag");
    1.78 -                }
    1.79 -              } else {
    1.80 -                report.AppendLine("Status: No Response");
    1.81 +        FTD2XX.FT_SetBaudRate(handle, 19200);
    1.82 +        FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
    1.83 +        FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 
    1.84 +          0x13);
    1.85 +        FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
    1.86 +        FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
    1.87 +        
    1.88 +        status = FTD2XX.Write(handle, new byte[] { 0x38 });
    1.89 +        if (status != FT_STATUS.FT_OK) {
    1.90 +          report.AppendLine("Write Status: " + status);
    1.91 +          FTD2XX.FT_Close(handle);
    1.92 +          continue;
    1.93 +        }
    1.94 +
    1.95 +        bool isValid = false;
    1.96 +        byte protocolVersion = 0;
    1.97 +
    1.98 +        int j = 0;
    1.99 +        while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
   1.100 +          Thread.Sleep(100);
   1.101 +          j++;
   1.102 +        }
   1.103 +        if (FTD2XX.BytesToRead(handle) > 0) {
   1.104 +          if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
   1.105 +            while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
   1.106 +              Thread.Sleep(100);
   1.107 +              j++;
   1.108 +            }
   1.109 +            int length = FTD2XX.BytesToRead(handle);
   1.110 +            if (length >= 284) {
   1.111 +              byte[] data = new byte[285];
   1.112 +              data[0] = TBalancer.STARTFLAG;
   1.113 +              for (int k = 1; k < data.Length; k++)
   1.114 +                data[k] = FTD2XX.ReadByte(handle);
   1.115 +
   1.116 +              // check protocol version 2X (protocols seen: 2C, 2A, 28)
   1.117 +              isValid = (data[274] & 0xF0) == 0x20;
   1.118 +              protocolVersion = data[274];
   1.119 +              if (!isValid) {
   1.120 +                report.Append("Status: Wrong Protocol Version: 0x");
   1.121 +                report.AppendLine(protocolVersion.ToString("X"));
   1.122                }
   1.123              } else {
   1.124 -              report.AppendLine("Status: Not Clear to Send");
   1.125 +              report.AppendLine("Status: Wrong Message Length: " + length);
   1.126              }
   1.127 -            serialPort.DiscardInBuffer();
   1.128 -            serialPort.Close();
   1.129 -          } else {            
   1.130 -            report.AppendLine("Status: Port not Open");
   1.131 -          }                          
   1.132 -          if (isValid) {
   1.133 -            report.AppendLine("Status: OK");
   1.134 -            hardware.Add(new TBalancer(portNames[i], protocolVersion));
   1.135 -            return;
   1.136 +          } else {
   1.137 +            report.AppendLine("Status: Wrong Startflag");
   1.138            }
   1.139 -        } catch (Exception e) {
   1.140 -          report.AppendLine(e.ToString());
   1.141 -        } 
   1.142 +        } else {
   1.143 +          report.AppendLine("Status: No Response");
   1.144 +        }
   1.145 +
   1.146 +        FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
   1.147 +        FTD2XX.FT_Close(handle);
   1.148 +
   1.149 +        if (isValid) {
   1.150 +          report.AppendLine("Status: OK");
   1.151 +          hardware.Add(new TBalancer(i, protocolVersion));
   1.152 +          return;
   1.153 +        }
   1.154          report.AppendLine();
   1.155        }
   1.156      }
   1.157 @@ -134,7 +142,7 @@
   1.158  
   1.159      public string GetReport() {
   1.160        if (report.Length > 0) {
   1.161 -        report.Insert(0, "Serial Port T-Balancer" + Environment.NewLine +
   1.162 +        report.Insert(0, "FTD2XX" + Environment.NewLine +
   1.163            Environment.NewLine);
   1.164          report.AppendLine();
   1.165          return report.ToString();