1.1 --- a/Hardware/Heatmaster/HeatmasterGroup.cs Tue Sep 07 22:15:02 2010 +0000
1.2 +++ b/Hardware/Heatmaster/HeatmasterGroup.cs Wed Sep 08 19:29:58 2010 +0000
1.3 @@ -37,6 +37,7 @@
1.4
1.5 using System;
1.6 using System.Collections.Generic;
1.7 +using System.Globalization;
1.8 using System.IO.Ports;
1.9 using System.Security;
1.10 using System.Text;
1.11 @@ -99,77 +100,77 @@
1.12
1.13 string[] portNames = GetRegistryPortNames();
1.14 for (int i = portNames.Length - 1; i >= 0; i--) {
1.15 - try {
1.16 + bool isValid = false;
1.17 + try {
1.18 + using (SerialPort serialPort =
1.19 + new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One)) {
1.20 + serialPort.NewLine = ((char)0x0D).ToString();
1.21 + report.Append("Port Name: "); report.AppendLine(portNames[i]);
1.22
1.23 - SerialPort serialPort =
1.24 - new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One);
1.25 - serialPort.NewLine = ((char)0x0D).ToString();
1.26 + try {
1.27 + serialPort.Open();
1.28 + } catch (UnauthorizedAccessException) {
1.29 + report.AppendLine("Exception: Access Denied");
1.30 + }
1.31
1.32 - bool isValid = false;
1.33 - report.Append("Port Name: "); report.AppendLine(portNames[i]);
1.34 + if (serialPort.IsOpen) {
1.35 + serialPort.DiscardInBuffer();
1.36 + serialPort.DiscardOutBuffer();
1.37 + serialPort.Write(new byte[] { 0xAA }, 0, 1);
1.38
1.39 - try {
1.40 - serialPort.Open();
1.41 - } catch (UnauthorizedAccessException) {
1.42 - report.AppendLine("Exception: Access Denied");
1.43 - }
1.44 -
1.45 - if (serialPort.IsOpen) {
1.46 - serialPort.DiscardInBuffer();
1.47 - serialPort.DiscardOutBuffer();
1.48 - serialPort.Write(new byte[] { 0xAA }, 0, 1);
1.49 -
1.50 - int j = 0;
1.51 - while (serialPort.BytesToRead == 0 && j < 10) {
1.52 - Thread.Sleep(20);
1.53 - j++;
1.54 - }
1.55 - if (serialPort.BytesToRead > 0) {
1.56 - bool flag = false;
1.57 - while (serialPort.BytesToRead > 0 && !flag) {
1.58 - flag |= (serialPort.ReadByte() == 0xAA);
1.59 + int j = 0;
1.60 + while (serialPort.BytesToRead == 0 && j < 10) {
1.61 + Thread.Sleep(20);
1.62 + j++;
1.63 }
1.64 - if (flag) {
1.65 - serialPort.WriteLine("[0:0]RH");
1.66 - try {
1.67 - int k = 0;
1.68 - int revision = 0;
1.69 - while (k < 5) {
1.70 - string line = ReadLine(serialPort, 100);
1.71 - if (line.StartsWith("-[0:0]RH:")) {
1.72 - int.TryParse(line.Substring(9), out revision);
1.73 - break;
1.74 + if (serialPort.BytesToRead > 0) {
1.75 + bool flag = false;
1.76 + while (serialPort.BytesToRead > 0 && !flag) {
1.77 + flag |= (serialPort.ReadByte() == 0xAA);
1.78 + }
1.79 + if (flag) {
1.80 + serialPort.WriteLine("[0:0]RH");
1.81 + try {
1.82 + int k = 0;
1.83 + int revision = 0;
1.84 + while (k < 5) {
1.85 + string line = ReadLine(serialPort, 100);
1.86 + if (line.StartsWith("-[0:0]RH:",
1.87 + StringComparison.Ordinal)) {
1.88 + revision = int.Parse(line.Substring(9),
1.89 + CultureInfo.InvariantCulture);
1.90 + break;
1.91 + }
1.92 + k++;
1.93 }
1.94 - k++;
1.95 + isValid = (revision == 770);
1.96 + if (!isValid) {
1.97 + report.Append("Status: Wrong Hardware Revision " +
1.98 + revision.ToString(CultureInfo.InvariantCulture));
1.99 + }
1.100 + } catch (TimeoutException) {
1.101 + report.AppendLine("Status: Timeout Reading Revision");
1.102 }
1.103 - isValid = (revision == 770);
1.104 - if (!isValid) {
1.105 - report.Append("Status: Wrong Hardware Revision " +
1.106 - revision.ToString());
1.107 - }
1.108 - } catch (TimeoutException) {
1.109 - report.AppendLine("Status: Timeout Reading Revision");
1.110 + } else {
1.111 + report.AppendLine("Status: Wrong Startflag");
1.112 }
1.113 } else {
1.114 - report.AppendLine("Status: Wrong Startflag");
1.115 + report.AppendLine("Status: No Response");
1.116 }
1.117 + serialPort.DiscardInBuffer();
1.118 } else {
1.119 - report.AppendLine("Status: No Response");
1.120 - }
1.121 - serialPort.DiscardInBuffer();
1.122 - serialPort.Close();
1.123 - serialPort.Dispose();
1.124 - } else {
1.125 - report.AppendLine("Status: Port not Open");
1.126 - }
1.127 - if (isValid) {
1.128 - report.AppendLine("Status: OK");
1.129 - hardware.Add(new Heatmaster(portNames[i], settings));
1.130 - return;
1.131 + report.AppendLine("Status: Port not Open");
1.132 + }
1.133 }
1.134 } catch (Exception e) {
1.135 report.AppendLine(e.ToString());
1.136 - }
1.137 + }
1.138 +
1.139 + if (isValid) {
1.140 + report.AppendLine("Status: OK");
1.141 + hardware.Add(new Heatmaster(portNames[i], settings));
1.142 + return;
1.143 + }
1.144 report.AppendLine();
1.145 }
1.146 }