moel@1: /* moel@1: moel@1: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@1: moel@1: The contents of this file are subject to the Mozilla Public License Version moel@1: 1.1 (the "License"); you may not use this file except in compliance with moel@1: the License. You may obtain a copy of the License at moel@1: moel@1: http://www.mozilla.org/MPL/ moel@1: moel@1: Software distributed under the License is distributed on an "AS IS" basis, moel@1: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@1: for the specific language governing rights and limitations under the License. moel@1: moel@1: The Original Code is the Open Hardware Monitor code. moel@1: moel@1: The Initial Developer of the Original Code is moel@1: Michael Möller . moel@1: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@1: the Initial Developer. All Rights Reserved. moel@1: moel@1: Contributor(s): moel@1: moel@1: Alternatively, the contents of this file may be used under the terms of moel@1: either the GNU General Public License Version 2 or later (the "GPL"), or moel@1: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@1: in which case the provisions of the GPL or the LGPL are applicable instead moel@1: of those above. If you wish to allow use of your version of this file only moel@1: under the terms of either the GPL or the LGPL, and not to allow others to moel@1: use your version of this file under the terms of the MPL, indicate your moel@1: decision by deleting the provisions above and replace them with the notice moel@1: and other provisions required by the GPL or the LGPL. If you do not delete moel@1: the provisions above, a recipient may use your version of this file under moel@1: the terms of any one of the MPL, the GPL or the LGPL. moel@1: moel@1: */ moel@1: moel@1: using System; moel@1: using System.Collections.Generic; moel@166: using System.Globalization; moel@32: using System.Text; moel@1: using System.Threading; moel@1: moel@1: namespace OpenHardwareMonitor.Hardware.TBalancer { moel@165: internal class TBalancerGroup : IGroup { moel@1: moel@195: private readonly List hardware = new List(); moel@195: private readonly StringBuilder report = new StringBuilder(); moel@1: moel@165: public TBalancerGroup(ISettings settings) { moel@32: moel@87: uint numDevices; moel@87: try { moel@216: if (FTD2XX.FT_CreateDeviceInfoList(out numDevices) != FT_STATUS.FT_OK) { moel@216: report.AppendLine("Status: FT_CreateDeviceInfoList failed"); moel@216: return; moel@216: } moel@87: } catch (DllNotFoundException) { return; } moel@87: catch (ArgumentNullException) { return; } moel@131: catch (EntryPointNotFoundException) { return; } moel@160: catch (BadImageFormatException) { return; } moel@87: moel@87: FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices]; moel@216: if (FTD2XX.FT_GetDeviceInfoList(info, ref numDevices) != FT_STATUS.FT_OK) moel@216: { moel@216: report.AppendLine("Status: FT_GetDeviceInfoList failed"); moel@216: return; moel@216: } moel@216: moel@216: // make sure numDevices is not larger than the info array moel@216: if (numDevices > info.Length) moel@216: numDevices = (uint)info.Length; moel@32: moel@87: for (int i = 0; i < numDevices; i++) { moel@166: report.Append("Device Index: "); moel@166: report.AppendLine(i.ToString(CultureInfo.InvariantCulture)); moel@186: report.Append("Device Type: "); moel@186: report.AppendLine(info[i].Type.ToString()); moel@186: moel@186: // the T-Balancer always uses an FT232BM moel@186: if (info[i].Type != FT_DEVICE.FT_DEVICE_232BM) { moel@186: report.AppendLine("Status: Wrong device type"); moel@186: continue; moel@186: } moel@186: moel@87: FT_HANDLE handle; moel@195: FT_STATUS status = FTD2XX.FT_Open(i, out handle); moel@87: if (status != FT_STATUS.FT_OK) { moel@87: report.AppendLine("Open Status: " + status); moel@87: continue; moel@87: } moel@32: moel@87: FTD2XX.FT_SetBaudRate(handle, 19200); moel@87: FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0); moel@87: FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, moel@87: 0x13); moel@87: FTD2XX.FT_SetTimeouts(handle, 1000, 1000); moel@87: FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL); moel@87: moel@87: status = FTD2XX.Write(handle, new byte[] { 0x38 }); moel@87: if (status != FT_STATUS.FT_OK) { moel@87: report.AppendLine("Write Status: " + status); moel@87: FTD2XX.FT_Close(handle); moel@87: continue; moel@87: } moel@87: moel@87: bool isValid = false; moel@87: byte protocolVersion = 0; moel@87: moel@87: int j = 0; moel@87: while (FTD2XX.BytesToRead(handle) == 0 && j < 2) { moel@87: Thread.Sleep(100); moel@87: j++; moel@87: } moel@87: if (FTD2XX.BytesToRead(handle) > 0) { moel@87: if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) { moel@87: while (FTD2XX.BytesToRead(handle) < 284 && j < 5) { moel@87: Thread.Sleep(100); moel@87: j++; moel@87: } moel@87: int length = FTD2XX.BytesToRead(handle); moel@87: if (length >= 284) { moel@87: byte[] data = new byte[285]; moel@87: data[0] = TBalancer.STARTFLAG; moel@87: for (int k = 1; k < data.Length; k++) moel@87: data[k] = FTD2XX.ReadByte(handle); moel@87: moel@87: // check protocol version 2X (protocols seen: 2C, 2A, 28) moel@87: isValid = (data[274] & 0xF0) == 0x20; moel@87: protocolVersion = data[274]; moel@87: if (!isValid) { moel@87: report.Append("Status: Wrong Protocol Version: 0x"); moel@166: report.AppendLine( moel@166: protocolVersion.ToString("X", CultureInfo.InvariantCulture)); moel@1: } moel@32: } else { moel@87: report.AppendLine("Status: Wrong Message Length: " + length); moel@1: } moel@87: } else { moel@87: report.AppendLine("Status: Wrong Startflag"); moel@1: } moel@87: } else { moel@87: report.AppendLine("Status: No Response"); moel@87: } moel@87: moel@87: FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL); moel@87: FTD2XX.FT_Close(handle); moel@87: moel@87: if (isValid) { moel@87: report.AppendLine("Status: OK"); moel@165: hardware.Add(new TBalancer(i, protocolVersion, settings)); moel@87: return; moel@87: } moel@41: report.AppendLine(); moel@1: } moel@1: } moel@1: moel@1: public IHardware[] Hardware { moel@1: get { moel@1: return hardware.ToArray(); moel@1: } moel@1: } moel@1: moel@1: public string GetReport() { moel@32: if (report.Length > 0) { moel@87: report.Insert(0, "FTD2XX" + Environment.NewLine + moel@45: Environment.NewLine); moel@45: report.AppendLine(); moel@32: return report.ToString(); moel@32: } else moel@32: return null; moel@1: } moel@1: moel@1: public void Close() { moel@1: foreach (TBalancer tbalancer in hardware) moel@1: tbalancer.Close(); moel@1: } moel@1: } moel@1: }