moel@171: /* moel@171: moel@171: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@171: moel@171: The contents of this file are subject to the Mozilla Public License Version moel@171: 1.1 (the "License"); you may not use this file except in compliance with moel@171: the License. You may obtain a copy of the License at moel@171: moel@171: http://www.mozilla.org/MPL/ moel@171: moel@171: Software distributed under the License is distributed on an "AS IS" basis, moel@171: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@171: for the specific language governing rights and limitations under the License. moel@171: moel@171: The Original Code is the Open Hardware Monitor code. moel@171: moel@171: The Initial Developer of the Original Code is moel@171: Michael Möller . moel@171: Portions created by the Initial Developer are Copyright (C) 2010 moel@171: the Initial Developer. All Rights Reserved. moel@171: moel@171: Contributor(s): moel@171: moel@171: Alternatively, the contents of this file may be used under the terms of moel@171: either the GNU General Public License Version 2 or later (the "GPL"), or moel@171: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@171: in which case the provisions of the GPL or the LGPL are applicable instead moel@171: of those above. If you wish to allow use of your version of this file only moel@171: under the terms of either the GPL or the LGPL, and not to allow others to moel@171: use your version of this file under the terms of the MPL, indicate your moel@171: decision by deleting the provisions above and replace them with the notice moel@171: and other provisions required by the GPL or the LGPL. If you do not delete moel@171: the provisions above, a recipient may use your version of this file under moel@171: the terms of any one of the MPL, the GPL or the LGPL. moel@171: moel@171: */ moel@171: moel@171: using System; moel@171: using System.Collections.Generic; moel@171: using System.IO.Ports; moel@171: using System.Text; moel@171: using System.Threading; moel@171: moel@171: namespace OpenHardwareMonitor.Hardware.Heatmaster { moel@171: internal class HeatmasterGroup : IGroup { moel@171: moel@171: private List hardware = new List(); moel@171: private StringBuilder report = new StringBuilder(); moel@171: moel@171: private static string ReadLine(SerialPort port, int timeout) { moel@171: int i = 0; moel@171: StringBuilder builder = new StringBuilder(); moel@171: while (i < timeout) { moel@171: while (port.BytesToRead > 0) { moel@171: byte b = (byte)port.ReadByte(); moel@171: switch (b) { moel@171: case 0xAA: return ((char)b).ToString(); moel@171: case 0x0D: return builder.ToString(); moel@171: default: builder.Append((char)b); break; moel@171: } moel@171: } moel@171: i++; moel@171: Thread.Sleep(1); moel@171: } moel@171: throw new TimeoutException(); moel@171: } moel@171: moel@171: public HeatmasterGroup(ISettings settings) { moel@171: moel@171: string[] portNames = SerialPort.GetPortNames(); moel@171: for (int i = portNames.Length - 1; i >= 0; i--) { moel@171: try { moel@171: moel@171: SerialPort serialPort = moel@171: new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One); moel@171: serialPort.NewLine = ((char)0x0D).ToString(); moel@171: moel@171: bool isValid = false; moel@171: report.Append("Port Name: "); report.AppendLine(portNames[i]); moel@171: moel@171: try { moel@171: serialPort.Open(); moel@171: } catch (UnauthorizedAccessException) { moel@171: report.AppendLine("Exception: Access Denied"); moel@171: } moel@171: moel@171: if (serialPort.IsOpen) { moel@171: if (serialPort.CtsHolding) { moel@171: serialPort.DiscardInBuffer(); moel@171: serialPort.DiscardOutBuffer(); moel@171: serialPort.Write(new byte[] { 0xAA }, 0, 1); moel@171: moel@171: int j = 0; moel@171: while (serialPort.BytesToRead == 0 && j < 10) { moel@171: Thread.Sleep(20); moel@171: j++; moel@171: } moel@171: if (serialPort.BytesToRead > 0) { moel@171: bool flag = false; moel@171: while (serialPort.BytesToRead > 0 && !flag) { moel@171: flag |= (serialPort.ReadByte() == 0xAA); moel@171: } moel@171: if (flag) { moel@171: serialPort.WriteLine("[0:0]RH"); moel@171: try { moel@171: int k = 0; moel@171: int revision = 0; moel@171: while (k < 5) { moel@171: string line = ReadLine(serialPort, 100); moel@171: if (line.StartsWith("-[0:0]RH:")) { moel@171: int.TryParse(line.Substring(9), out revision); moel@171: break; moel@171: } moel@171: k++; moel@171: } moel@171: isValid = (revision == 770); moel@171: if (!isValid) { moel@171: report.Append("Status: Wrong Hardware Revision " + moel@171: revision.ToString()); moel@171: } moel@171: } catch (TimeoutException) { moel@171: report.AppendLine("Status: Timeout Reading Revision"); moel@171: } moel@171: } else { moel@171: report.AppendLine("Status: Wrong Startflag"); moel@171: } moel@171: } else { moel@171: report.AppendLine("Status: No Response"); moel@171: } moel@171: } else { moel@171: report.AppendLine("Status: Not Clear to Send"); moel@171: } moel@171: serialPort.DiscardInBuffer(); moel@171: serialPort.Close(); moel@171: serialPort.Dispose(); moel@171: } else { moel@171: report.AppendLine("Status: Port not Open"); moel@171: } moel@171: if (isValid) { moel@171: report.AppendLine("Status: OK"); moel@171: hardware.Add(new Heatmaster(portNames[i], settings)); moel@171: return; moel@171: } moel@171: } catch (Exception e) { moel@171: report.AppendLine(e.ToString()); moel@171: } moel@171: report.AppendLine(); moel@171: } moel@171: } moel@171: moel@171: public IHardware[] Hardware { moel@171: get { moel@171: return hardware.ToArray(); moel@171: } moel@171: } moel@171: moel@171: public string GetReport() { moel@171: if (report.Length > 0) { moel@171: report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine + moel@171: Environment.NewLine); moel@171: report.AppendLine(); moel@171: return report.ToString(); moel@171: } else moel@171: return null; moel@171: } moel@171: moel@171: public void Close() { moel@171: foreach (Heatmaster heatmaster in hardware) moel@171: heatmaster.Close(); moel@171: } moel@171: } moel@171: }