Exemple #1
0
        private void GetParametersWriteInfo(
            IEnumerable<WriteParameter> parameters, out Dictionary<byte, SlaveWriteInfo> infos)
        {
            infos = new Dictionary<byte, SlaveWriteInfo>();
            var index = -1;
            foreach (var parameter in parameters)
            {
                ++index;
                var regex = new Regex(DevicePattern);
                var match = regex.Match(parameter.Address);
                if (!match.Success)
                {
                    throw new Exception(parameter.Address + " is not a valid device address");
                }

                #region Get slave address

                var slaveAddress = _defaultAddress;
                var match1 = match.Groups[1].ToString();
                if (!string.IsNullOrEmpty(match1) && match1.Length > 1)
                {
                    int i;
                    if (int.TryParse(match1.Substring(0, match1.Length - 1), out i))
                    {
                        if (i >= byte.MinValue && i <= byte.MaxValue)
                        {
                            slaveAddress = (byte)i;
                        }
                        else
                        {
                            throw new Exception(parameter.Address + ": slave address incorrect");
                        }
                    }
                }

                #endregion

                #region Get device address

                ushort deviceAddress;
                if (!ushort.TryParse(match.Groups[3].ToString(), out deviceAddress) || deviceAddress == 0)
                {
                    throw new Exception(parameter.Address + ": device index not supported");
                }
                deviceAddress -= 1;
                var match2 = match.Groups[2].ToString();
                int? length;
                if (parameter.Value is bool)
                {
                    if (match2 != "0" && match2 != "1")
                    {
                        throw new Exception("Type " + parameter.Value.GetType().Name + " is not supported for " + parameter.Address);
                    }
                    length = 1;
                }
                else
                {
                    if (match2 != "3" && match2 != "4")
                    {
                        throw new Exception("Type " + parameter.Value.GetType().Name + " is not supported for " + parameter.Address);
                    }
                    length = TypeToRegistersCount(parameter.Value.GetType());
                    if (length == null)
                    {
                        throw new Exception("Type " + parameter.Value.GetType().Name + " is not supported for " + parameter.Address);
                    }
                }

                #endregion

                #region Check device index

                var deviceLength = length.Value;
                if (deviceLength + deviceAddress - 1 > ushort.MaxValue)
                {
                    throw new Exception(parameter.Address + ": device index is out of range");
                }

                #endregion

                #region WriteInfo

                SlaveWriteInfo writeInfo;
                if (!infos.TryGetValue(slaveAddress, out writeInfo))
                {
                    writeInfo = new SlaveWriteInfo();
                    infos.Add(slaveAddress, writeInfo);
                }

                #endregion

                #region Fill WriteInfo

                switch (match2)
                {
                    case "0":
                        var coil = new CoilWriteInfo
                                       {
                                           Address = deviceAddress,
                                           Index = index,
                                           WriteParameter = parameter
                                       };
                        writeInfo.Coils.Add(coil);
                        break;
                    case "4":
                        var hr = new HoldingRegisterWriteInfo
                                     {
                                         Address = new KeyValuePair<ushort, int>(deviceAddress, deviceLength),
                                         Index = index,
                                         WriteParameter = parameter
                                     };
                        writeInfo.HoldingRegisters.Add(hr);
                        break;
                }

                #endregion
            }
        }
Exemple #2
0
 private static void SortWriteInfoDevices(SlaveWriteInfo info)
 {
     Comparison<CoilWriteInfo> coilComparison =
         (c1, c2) =>
             {
                 if (c1.Address > c2.Address)
                     return 1;
                 if (c1.Address < c2.Address)
                     return -1;
                 return 0;
             };
     info.Coils.Sort(coilComparison);
     Comparison<HoldingRegisterWriteInfo> hrComparison =
         (c1, c2) =>
         {
             if (c1.Address.Key > c2.Address.Key)
                 return 1;
             if (c1.Address.Key < c2.Address.Key)
                 return -1;
             if (c1.Address.Value > c2.Address.Value)
                 return -1;
             if (c1.Address.Value < c2.Address.Value)
                 return 1;
             return 0;
         };
     info.HoldingRegisters.Sort(hrComparison);
 }