diff -r 2392f7402fb6 -r d92bacc2116e Hardware/TBalancer/TBalancerGroup.cs --- a/Hardware/TBalancer/TBalancerGroup.cs Fri Feb 12 00:36:56 2010 +0000 +++ b/Hardware/TBalancer/TBalancerGroup.cs Fri Feb 12 08:17:51 2010 +0000 @@ -59,11 +59,9 @@ serialPort.Open(); bool isValid = false; byte protocolVersion = 0; - if (serialPort.IsOpen && serialPort.CDHolding && - serialPort.CtsHolding) { + report.Append("Port Name: "); report.AppendLine(portNames[i]); - report.Append("Port Name: "); report.AppendLine(portNames[i]); - + if (serialPort.IsOpen) { serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); serialPort.Write(new byte[] { 0x38 }, 0, 1); @@ -72,34 +70,39 @@ Thread.Sleep(100); j++; } - if (serialPort.BytesToRead > 0 && - serialPort.ReadByte() == TBalancer.STARTFLAG) { - while (serialPort.BytesToRead < 284 && j < 5) { - Thread.Sleep(100); - j++; - } - int length = serialPort.BytesToRead; - if (length >= 284) { - byte[] data = new byte[285]; - data[0] = TBalancer.STARTFLAG; - for (int k = 1; k < data.Length; k++) - data[k] = (byte)serialPort.ReadByte(); + if (serialPort.BytesToRead > 0) { + if (serialPort.ReadByte() == TBalancer.STARTFLAG) { + while (serialPort.BytesToRead < 284 && j < 5) { + Thread.Sleep(100); + j++; + } + int length = serialPort.BytesToRead; + if (length >= 284) { + byte[] data = new byte[285]; + data[0] = TBalancer.STARTFLAG; + for (int k = 1; k < data.Length; k++) + data[k] = (byte)serialPort.ReadByte(); - // check protocol version - isValid = - data[274] == TBalancer.PROTOCOL_VERSION_2A || - data[274] == TBalancer.PROTOCOL_VERSION_2C; - protocolVersion = data[274]; - if (!isValid) { - report.Append("Status: Wrong Protocol Version: 0x"); - report.AppendLine(protocolVersion.ToString("X")); + // check protocol version + isValid = + data[274] == TBalancer.PROTOCOL_VERSION_2A || + data[274] == TBalancer.PROTOCOL_VERSION_2C; + protocolVersion = data[274]; + if (!isValid) { + report.Append("Status: Wrong Protocol Version: 0x"); + report.AppendLine(protocolVersion.ToString("X")); + } + } else { + report.AppendLine("Status: Wrong Message Length: " + length); } } else { - report.AppendLine("Status: Wrong Message Length: " + length); + report.AppendLine("Status: Wrong Startflag"); } } else { - report.AppendLine("Status: Wrong Startflag"); + report.AppendLine("Status: No Response"); } + } else { + report.AppendLine("Status: Port not Open"); } serialPort.DiscardInBuffer(); serialPort.Close(); @@ -115,6 +118,7 @@ } catch (NullReferenceException ne) { report.AppendLine(ne.ToString()); } + report.AppendLine(); } } @@ -127,8 +131,7 @@ public string GetReport() { if (report.Length > 0) { report.Insert(0, "Serial Port T-Balancer" + Environment.NewLine + - Environment.NewLine); - report.AppendLine(); + Environment.NewLine); return report.ToString(); } else return null;