Hardware/Heatmaster/Heatmaster.cs
changeset 172 c9d8de472546
parent 171 81ab5e53122e
child 173 fb96c0ca3c2d
     1.1 --- a/Hardware/Heatmaster/Heatmaster.cs	Sun Aug 22 21:53:11 2010 +0000
     1.2 +++ b/Hardware/Heatmaster/Heatmaster.cs	Mon Aug 23 20:00:06 2010 +0000
     1.3 @@ -61,6 +61,8 @@
     1.4      
     1.5      private bool available = false;
     1.6  
     1.7 +    private StringBuilder buffer = new StringBuilder();
     1.8 +
     1.9      private string ReadLine(int timeout) {
    1.10        int i = 0;
    1.11        StringBuilder builder = new StringBuilder();
    1.12 @@ -82,7 +84,7 @@
    1.13      private string ReadField(int device, char field) {
    1.14        serialPort.WriteLine("[0:" + device + "]R" + field);
    1.15        for (int i = 0; i < 5; i++) {
    1.16 -        string s = ReadLine(100);
    1.17 +        string s = ReadLine(200);
    1.18          Match match = Regex.Match(s, @"-\[0:" + device.ToString() + @"\]R" +
    1.19            Regex.Escape(field.ToString()) + ":(.*)");
    1.20          if (match.Success) 
    1.21 @@ -111,7 +113,7 @@
    1.22      private bool WriteField(int device, char field, string value) {
    1.23        serialPort.WriteLine("[0:" + device + "]W" + field + ":" + value);
    1.24        for (int i = 0; i < 5; i++) {
    1.25 -        string s = ReadLine(100);
    1.26 +        string s = ReadLine(200);
    1.27          Match match = Regex.Match(s, @"-\[0:" + device.ToString() + @"\]W" + 
    1.28            Regex.Escape(field.ToString()) + ":" + value);
    1.29          if (match.Success)
    1.30 @@ -222,42 +224,52 @@
    1.31        get { return "Heatmaster"; }
    1.32      }
    1.33  
    1.34 +    private void ProcessUpdateLine(string line) {
    1.35 +      Match match = Regex.Match(line, @">\[0:(\d+)\]([0-9:\|-]+)");
    1.36 +      if (match.Success) {
    1.37 +        int device;
    1.38 +        if (int.TryParse(match.Groups[1].Value, out device)) {
    1.39 +          foreach (string s in match.Groups[2].Value.Split('|')) {
    1.40 +            string[] strings = s.Split(':');
    1.41 +            int[] ints = new int[strings.Length];
    1.42 +            for (int i = 0; i < ints.Length; i++)
    1.43 +              ints[i] = int.Parse(strings[i]);
    1.44 +            switch (device) {
    1.45 +              case 32:
    1.46 +                if (ints.Length == 3 && ints[0] <= fans.Length) {
    1.47 +                  fans[ints[0] - 1].Value = ints[1];
    1.48 +                  controls[ints[0] - 1].Value = (100 / 255.0f) * ints[2];
    1.49 +                }
    1.50 +                break;
    1.51 +              case 48:
    1.52 +                if (ints.Length == 2 && ints[0] <= temperatures.Length)
    1.53 +                  temperatures[ints[0] - 1].Value = 0.1f * ints[1];
    1.54 +                break;
    1.55 +              case 64:
    1.56 +                if (ints.Length == 3 && ints[0] <= flows.Length)
    1.57 +                  flows[ints[0] - 1].Value = 0.1f * ints[1];
    1.58 +                break;
    1.59 +              case 80:
    1.60 +                if (ints.Length == 2 && ints[0] <= relays.Length)
    1.61 +                  relays[ints[0] - 1].Value = 100 * ints[1];
    1.62 +                break;
    1.63 +            }
    1.64 +          }
    1.65 +        }
    1.66 +      }
    1.67 +    }
    1.68 +
    1.69      public override void Update() {
    1.70        if (!available)
    1.71          return;
    1.72  
    1.73        while (serialPort.BytesToRead > 0) {
    1.74 -        Match match = Regex.Match(ReadLine(0), @">\[0:(\d+)\]([0-9:\|-]+)");
    1.75 -        if (match.Success) {
    1.76 -          int device;
    1.77 -          if (int.TryParse(match.Groups[1].Value, out device)) {
    1.78 -            foreach (string s in match.Groups[2].Value.Split('|')) {
    1.79 -              string[] strings = s.Split(':');
    1.80 -              int[] ints = new int[strings.Length];
    1.81 -              for (int i = 0; i < ints.Length; i++)
    1.82 -                ints[i] = int.Parse(strings[i]);
    1.83 -              switch (device) {
    1.84 -                case 32:
    1.85 -                  if (ints.Length == 3 && ints[0] <= fans.Length) {
    1.86 -                    fans[ints[0] - 1].Value = ints[1];
    1.87 -                    controls[ints[0] - 1].Value = (100 / 255.0f) * ints[2];
    1.88 -                  }
    1.89 -                  break;
    1.90 -                case 48:
    1.91 -                  if (ints.Length == 2 && ints[0] <= temperatures.Length) 
    1.92 -                      temperatures[ints[0] - 1].Value = 0.1f * ints[1];
    1.93 -                  break;
    1.94 -                case 64:
    1.95 -                  if (ints.Length == 3 && ints[0] <= flows.Length)
    1.96 -                    flows[ints[0] - 1].Value = 0.1f * ints[1];
    1.97 -                  break;
    1.98 -                case 80:
    1.99 -                  if (ints.Length == 2 && ints[0] <= relays.Length)
   1.100 -                    relays[ints[0] - 1].Value = 100 * ints[1];
   1.101 -                  break;
   1.102 -              } 
   1.103 -            } 
   1.104 -          }
   1.105 +        byte b = (byte)serialPort.ReadByte();
   1.106 +        if (b == 0x0D) {
   1.107 +          ProcessUpdateLine(buffer.ToString());
   1.108 +          buffer.Length = 0;
   1.109 +        } else {
   1.110 +          buffer.Append((char)b);
   1.111          }
   1.112        }
   1.113      }