Example #1
0
        private void GetParametersReadInfo(
            IEnumerable<ReadParameter> parameters,
            out Dictionary<byte, SlaveReadInfo> infos,
            out List<KeyValuePair<ReadParameter, byte?>> paramSlaveReadAddresses)
        {
            infos = new Dictionary<byte, SlaveReadInfo>();
            var readParameters = parameters as ReadParameter[] ?? parameters.ToArray();
            paramSlaveReadAddresses = new List<KeyValuePair<ReadParameter, byte?>>(readParameters.Count());
            foreach (var parameter in readParameters)
            {
                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");
                        }
                    }
                }

                paramSlaveReadAddresses.Add(new KeyValuePair<ReadParameter, byte?>(parameter, slaveAddress));

                #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.ValueType == typeof(bool))
                {
                    if (match2 != "0" && match2 != "1")
                    {
                        throw new Exception(
                            "Type " + parameter.ValueType.Name + " is not supported for" + parameter.Address);
                    }
                    length = 1;
                }
                else
                {
                    if (match2 != "3" && match2 != "4")
                    {
                        throw new Exception(
                            "Type " + parameter.ValueType.Name + " is not supported for" + parameter.Address);
                    }
                    length = TypeToRegistersCount(parameter.ValueType);
                    if (length == null)
                    {
                        throw new Exception(
                            "Type " + parameter.ValueType.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 ReadInfo

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

                #endregion

                #region Fill ReadInfo

                switch (match2)
                {
                    case "0":
                        if (!readInfo.Coils.Contains(deviceAddress))
                            readInfo.Coils.Add(deviceAddress);
                        readInfo.CoilsAddresses.Add(parameter, deviceAddress);
                        break;
                    case "1":
                        if (!readInfo.Contacts.Contains(deviceAddress))
                            readInfo.Contacts.Add(deviceAddress);
                        readInfo.ContactsAddresses.Add(parameter, deviceAddress);
                        break;
                    case "3":
                        var ir = new KeyValuePair<ushort, int>(deviceAddress, deviceLength);
                        if (!readInfo.InputRegisters.Contains(ir))
                            readInfo.InputRegisters.Add(ir);
                        readInfo.InputRegistersAddresses.Add(parameter, ir);
                        break;
                    case "4":
                        var hr = new KeyValuePair<ushort, int>(deviceAddress, deviceLength);
                        if (!readInfo.HoldingRegisters.Contains(hr))
                            readInfo.HoldingRegisters.Add(hr);
                        readInfo.HoldingRegistersAddresses.Add(parameter, hr);
                        break;
                }

                #endregion
            }
        }
Example #2
0
 private static void SortReadInfoDevices(SlaveReadInfo readInfo)
 {
     readInfo.Coils.Sort();
     readInfo.Contacts.Sort();
     Comparison<KeyValuePair<ushort, int>> comparison =
         (c1, c2) =>
         {
             if (c1.Key > c2.Key)
                 return 1;
             if (c1.Key < c2.Key)
                 return -1;
             if (c1.Value > c2.Value)
                 return -1;
             if (c1.Value < c2.Value)
                 return 1;
             return 0;
         };
     readInfo.InputRegisters.Sort(comparison);
     readInfo.HoldingRegisters.Sort(comparison);
 }