Hardware/TBalancer/TBalancerGroup.cs
changeset 47 1d02fde1bb23
parent 45 eadad41d21a6
child 51 e5fc5fd57dbe
     1.1 --- a/Hardware/TBalancer/TBalancerGroup.cs	Mon Feb 15 22:58:29 2010 +0000
     1.2 +++ b/Hardware/TBalancer/TBalancerGroup.cs	Tue Feb 16 21:44:25 2010 +0000
     1.3 @@ -56,54 +56,64 @@
     1.4  
     1.5            SerialPort serialPort =
     1.6              new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One);
     1.7 -          serialPort.Open();
     1.8 +          
     1.9            bool isValid = false;
    1.10            byte protocolVersion = 0;
    1.11            report.Append("Port Name: "); report.AppendLine(portNames[i]);
    1.12  
    1.13 +          try {
    1.14 +            serialPort.Open();
    1.15 +          } catch (UnauthorizedAccessException) {
    1.16 +            report.AppendLine("Exception: Access Denied");
    1.17 +          }
    1.18 +       
    1.19            if (serialPort.IsOpen) {
    1.20 -            serialPort.DiscardInBuffer();
    1.21 -            serialPort.DiscardOutBuffer();
    1.22 -            serialPort.Write(new byte[] { 0x38 }, 0, 1);
    1.23 -            int j = 0;
    1.24 -            while (serialPort.BytesToRead == 0 && j < 2) {
    1.25 -              Thread.Sleep(100);
    1.26 -              j++;
    1.27 -            }
    1.28 -            if (serialPort.BytesToRead > 0) {
    1.29 -              if (serialPort.ReadByte() == TBalancer.STARTFLAG) {
    1.30 -                while (serialPort.BytesToRead < 284 && j < 5) {
    1.31 -                  Thread.Sleep(100);
    1.32 -                  j++;
    1.33 -                }
    1.34 -                int length = serialPort.BytesToRead;
    1.35 -                if (length >= 284) {
    1.36 -                  byte[] data = new byte[285];
    1.37 -                  data[0] = TBalancer.STARTFLAG;
    1.38 -                  for (int k = 1; k < data.Length; k++)
    1.39 -                    data[k] = (byte)serialPort.ReadByte();
    1.40 +            if (serialPort.CtsHolding) {
    1.41 +              serialPort.DiscardInBuffer();
    1.42 +              serialPort.DiscardOutBuffer();
    1.43 +              serialPort.Write(new byte[] { 0x38 }, 0, 1);
    1.44 +              int j = 0;
    1.45 +              while (serialPort.BytesToRead == 0 && j < 2) {
    1.46 +                Thread.Sleep(100);
    1.47 +                j++;
    1.48 +              }
    1.49 +              if (serialPort.BytesToRead > 0) {
    1.50 +                if (serialPort.ReadByte() == TBalancer.STARTFLAG) {
    1.51 +                  while (serialPort.BytesToRead < 284 && j < 5) {
    1.52 +                    Thread.Sleep(100);
    1.53 +                    j++;
    1.54 +                  }
    1.55 +                  int length = serialPort.BytesToRead;
    1.56 +                  if (length >= 284) {
    1.57 +                    byte[] data = new byte[285];
    1.58 +                    data[0] = TBalancer.STARTFLAG;
    1.59 +                    for (int k = 1; k < data.Length; k++)
    1.60 +                      data[k] = (byte)serialPort.ReadByte();
    1.61  
    1.62 -                  // check protocol version 2X (protocols seen: 2C, 2A, 28)
    1.63 -                  isValid = (data[274] & 0xF0) == 0x20;
    1.64 -                  protocolVersion = data[274];
    1.65 -                  if (!isValid) {
    1.66 -                    report.Append("Status: Wrong Protocol Version: 0x");
    1.67 -                    report.AppendLine(protocolVersion.ToString("X"));
    1.68 +                    // check protocol version 2X (protocols seen: 2C, 2A, 28)
    1.69 +                    isValid = (data[274] & 0xF0) == 0x20;
    1.70 +                    protocolVersion = data[274];
    1.71 +                    if (!isValid) {
    1.72 +                      report.Append("Status: Wrong Protocol Version: 0x");
    1.73 +                      report.AppendLine(protocolVersion.ToString("X"));
    1.74 +                    }
    1.75 +                  } else {
    1.76 +                    report.AppendLine("Status: Wrong Message Length: " +length);
    1.77                    }
    1.78                  } else {
    1.79 -                  report.AppendLine("Status: Wrong Message Length: " + length);
    1.80 +                  report.AppendLine("Status: Wrong Startflag");
    1.81                  }
    1.82                } else {
    1.83 -                report.AppendLine("Status: Wrong Startflag");
    1.84 +                report.AppendLine("Status: No Response");
    1.85                }
    1.86              } else {
    1.87 -              report.AppendLine("Status: No Response");
    1.88 +              report.AppendLine("Status: Not Clear to Send");
    1.89              }
    1.90 -          } else {
    1.91 +            serialPort.DiscardInBuffer();
    1.92 +            serialPort.Close();
    1.93 +          } else {            
    1.94              report.AppendLine("Status: Port not Open");
    1.95 -          }
    1.96 -          serialPort.DiscardInBuffer();
    1.97 -          serialPort.Close();
    1.98 +          }                          
    1.99            if (isValid) {
   1.100              report.AppendLine("Status: OK");
   1.101              hardware.Add(new TBalancer(portNames[i], protocolVersion));
   1.102 @@ -111,8 +121,6 @@
   1.103            }
   1.104          } catch (IOException ioe) {
   1.105            report.AppendLine(ioe.ToString());
   1.106 -        } catch (UnauthorizedAccessException uae) {
   1.107 -          report.AppendLine(uae.ToString());
   1.108          } catch (NullReferenceException ne) {
   1.109            report.AppendLine(ne.ToString());
   1.110          }