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@1: using System.IO; moel@1: using System.IO.Ports; moel@32: using System.Text; moel@1: using System.Threading; moel@1: moel@1: namespace OpenHardwareMonitor.Hardware.TBalancer { moel@1: public class TBalancerGroup : IGroup { moel@1: moel@1: private List hardware = new List(); moel@32: private StringBuilder report = new StringBuilder(); moel@1: moel@1: public TBalancerGroup() { moel@32: moel@32: string[] portNames = SerialPort.GetPortNames(); moel@1: for (int i = portNames.Length - 1; i >= 0; i--) { moel@1: try { moel@32: moel@1: SerialPort serialPort = moel@1: new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One); moel@47: moel@1: bool isValid = false; moel@33: byte protocolVersion = 0; moel@41: report.Append("Port Name: "); report.AppendLine(portNames[i]); moel@32: moel@47: try { moel@47: serialPort.Open(); moel@47: } catch (UnauthorizedAccessException) { moel@47: report.AppendLine("Exception: Access Denied"); moel@47: } moel@47: moel@41: if (serialPort.IsOpen) { moel@47: if (serialPort.CtsHolding) { moel@47: serialPort.DiscardInBuffer(); moel@47: serialPort.DiscardOutBuffer(); moel@47: serialPort.Write(new byte[] { 0x38 }, 0, 1); moel@47: int j = 0; moel@47: while (serialPort.BytesToRead == 0 && j < 2) { moel@47: Thread.Sleep(100); moel@47: j++; moel@47: } moel@47: if (serialPort.BytesToRead > 0) { moel@47: if (serialPort.ReadByte() == TBalancer.STARTFLAG) { moel@47: while (serialPort.BytesToRead < 284 && j < 5) { moel@47: Thread.Sleep(100); moel@47: j++; moel@47: } moel@47: int length = serialPort.BytesToRead; moel@47: if (length >= 284) { moel@47: byte[] data = new byte[285]; moel@47: data[0] = TBalancer.STARTFLAG; moel@47: for (int k = 1; k < data.Length; k++) moel@47: data[k] = (byte)serialPort.ReadByte(); moel@32: moel@47: // check protocol version 2X (protocols seen: 2C, 2A, 28) moel@47: isValid = (data[274] & 0xF0) == 0x20; moel@47: protocolVersion = data[274]; moel@47: if (!isValid) { moel@47: report.Append("Status: Wrong Protocol Version: 0x"); moel@47: report.AppendLine(protocolVersion.ToString("X")); moel@47: } moel@47: } else { moel@47: report.AppendLine("Status: Wrong Message Length: " +length); moel@41: } moel@41: } else { moel@47: report.AppendLine("Status: Wrong Startflag"); moel@32: } moel@32: } else { moel@47: report.AppendLine("Status: No Response"); moel@1: } moel@32: } else { moel@47: report.AppendLine("Status: Not Clear to Send"); moel@1: } moel@47: serialPort.DiscardInBuffer(); moel@47: serialPort.Close(); moel@47: } else { moel@41: report.AppendLine("Status: Port not Open"); moel@47: } moel@1: if (isValid) { moel@32: report.AppendLine("Status: OK"); moel@33: hardware.Add(new TBalancer(portNames[i], protocolVersion)); moel@1: return; moel@1: } moel@51: } catch (Exception e) { moel@51: report.AppendLine(e.ToString()); moel@51: } 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@32: report.Insert(0, "Serial Port T-Balancer" + 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: }