Esempio n. 1
0
 public object[] Read(ReadParameter[] readParameters)
 {
     lock (_itemValues)
     {
         if (_canSubscribe)
         {
             SubscribeToRead(readParameters);
         }
         var result = new object[readParameters.Length];
         if (_canSubscribe)
         {
             for (var i = 0; i < readParameters.Length; i++)
             {
                 result[i] = _itemValues[readParameters[i].Address];
             }
         }
         else
         {
             var items = readParameters.Select(p => new Item
             {
                 ItemName = p.Address
             }).ToArray();
             var itemValues = _server.Read(items);
             for (var i = 0; i < readParameters.Length; i++)
             {
                 result[i] = itemValues[i].Value;
             }
         }
         return result;
     }
 }
Esempio n. 2
0
 public object[] Read(ReadParameter[] readParameters)
 {
     var items = new Item[readParameters.Length];
     for (var i = 0; i < readParameters.Length; i++)
     {
         items[i] = new Item {ItemName = readParameters[i].Address};
     }
     var itemValues = _server.Read(items);
     var result = new Object[itemValues.Length];
     for (var i = 0; i < itemValues.Length; i++)
     {
         result[i] = itemValues[i].Value;
     }
     return result;
 }
Esempio n. 3
0
 public object[] Read(ReadParameter[] readParameters)
 {
     for (var i = 1; i <= _tryCount; i++)
     {
         try
         {
             return ReadOnce(readParameters);
         }
         catch
         {
             if (i >= _tryCount)
                 throw;
         }
     }
     throw new NotSupportedException();
 }
Esempio n. 4
0
        public object[] Read(ReadParameter[] readParameters)
        {
            var values = new object[readParameters.Length];
            for (var i = 0; i < readParameters.Length; i++)
            {
                var readParameter = readParameters[i];
                object value;
                if (_values.TryGetValue(readParameter.Address, out value))
                {
                    if (value == null || value.GetType() == readParameter.ValueType)
                    {
                        values[i] = value;
                    }
                    else
                    {
                        throw new Exception(string.Format(
                            Res.TypeMismatchMessage,
                            readParameter.Address,
                            readParameter.ValueType,
                            value.GetType()));
                    }
                }
                else
                {
                    if (readParameter.ValueType.IsClass)
                    {
                        var constructor = readParameter.ValueType.GetConstructor(new Type[0]);
                        if (constructor == null)
                        {

                            throw new Exception(string.Format(
                                Res.ConstructorErrorMessage,
                                readParameter.Address,
                                readParameter.ValueType));
                        }
                        values[i] = constructor.Invoke(new object[0]);
                    }
                    else
                    {
                        value = Activator.CreateInstance(readParameter.ValueType);
                        values[i] = value;
                    }
                }
            }
            return values;
        }
Esempio n. 5
0
 private void SubscribeToRead(ReadParameter[] readParameters)
 {
     var newParameters = readParameters.Where(p => !_itemValues.ContainsKey(p.Address)).ToArray();
     if (!newParameters.Any())
         return;
     var items = newParameters.Select(p => new Item
     {
         ItemName = p.Address
     }).ToArray();
     try
     {
         _group.AddItems(items);
     }
     catch
     {
         _server.CancelSubscription(_group);
         _canSubscribe = false;
     }
     if (_canSubscribe)
     {
         var itemValues = _server.Read(items);
         foreach (var itemValue in itemValues)
         {
             _itemValues.Add(itemValue.ItemName, itemValue.Value);
         }
     }
 }
Esempio n. 6
0
        public object[] ReadOnce(ReadParameter[] readParameters)
        {
            if (readParameters == null)
                return null;
            var values = new List<object>();

            Dictionary<byte, SlaveReadInfo> readInfos;
            List<KeyValuePair<ReadParameter, byte?>> readParamSlaveAddresses;
            GetParametersReadInfo(readParameters, out readInfos, out readParamSlaveAddresses);

            var result = new Dictionary<byte, SlaveReadResult>();
            foreach (var slaveReadInfo in readInfos)
            {
                var slaveAddress = slaveReadInfo.Key;
                var r = new SlaveReadResult();
                result.Add(slaveAddress, r);
                var readInfo = slaveReadInfo.Value;
                SortReadInfoDevices(readInfo);

                r.CoilsData = new Dictionary<ushort, bool>(readInfo.Coils.Count);
                r.ContactsData = new Dictionary<ushort, bool>(readInfo.Contacts.Count);
                r.InputRegistersData = new Dictionary<KeyValuePair<ushort, int>, byte[]>(readInfo.InputRegisters.Count);
                r.HoldingRegistersData = new Dictionary<KeyValuePair<ushort, int>, byte[]>(readInfo.HoldingRegisters.Count);
                ReadBits(readInfo.Coils, slaveAddress, r.CoilsData, _mb.ReadCoils, "discrete coils");
                ReadBits(readInfo.Contacts, slaveAddress, r.ContactsData, _mb.ReadContacts, "discrete contacts");
                ReadRegisters(readInfo.InputRegisters, slaveAddress, r.InputRegistersData,
                    _mb.ReadInputRegisters, "input registers");
                ReadRegisters(readInfo.HoldingRegisters, slaveAddress, r.HoldingRegistersData,
                    _mb.ReadHoldingRegisters, "holding registers");
            }

            foreach (var k in readParamSlaveAddresses)
            {
                if (k.Value == null)
                {
                    values.Add(null);
                    continue;
                }
                var slaveAddress = k.Value.Value;
                SlaveReadResult r;
                if (!result.TryGetValue(slaveAddress, out r))
                {
                    values.Add(null);
                    continue;
                }
                SlaveReadInfo slaveReadInfo;
                if (readInfos.TryGetValue(slaveAddress, out slaveReadInfo))
                {
                    var readParameter = k.Key;
                    ushort bitAddress;
                    KeyValuePair<ushort, int> registerAddress;
                    if (slaveReadInfo.CoilsAddresses.TryGetValue(readParameter, out bitAddress))
                    {
                        bool value;
                        if (r.CoilsData.TryGetValue(bitAddress, out value))
                        {
                            values.Add(value);
                        }
                        else
                        {
                            values.Add(null);
                        }
                    }
                    else if (slaveReadInfo.ContactsAddresses.TryGetValue(readParameter, out bitAddress))
                    {
                        bool value;
                        if (r.ContactsData.TryGetValue(bitAddress, out value))
                        {
                            values.Add(value);
                        }
                        else
                        {
                            values.Add(null);
                        }
                    }
                    else if (slaveReadInfo.InputRegistersAddresses.TryGetValue(readParameter, out registerAddress))
                    {
                        byte[] value;
                        values.Add(r.InputRegistersData.TryGetValue(registerAddress, out value)
                                       ? GetValue(value, readParameter.ValueType)
                                       : null);
                    }
                    else if (slaveReadInfo.HoldingRegistersAddresses.TryGetValue(readParameter, out registerAddress))
                    {
                        byte[] value;
                        values.Add(r.HoldingRegistersData.TryGetValue(registerAddress, out value)
                                       ? GetValue(value, readParameter.ValueType)
                                       : null);
                    }
                }
                else
                {
                    values.Add(null);
                }
            }

            return values.ToArray();
        }
Esempio n. 7
0
 public object[] Read(ReadParameter[] readParameters)
 {
     return _ioDevice.Read(readParameters);
 }
Esempio n. 8
0
 public object[] Read(ReadParameter[] readParameters)
 {
     if (!_opened)
         throw new Exception(NotOpenedExceptionMessage);
     if (readParameters.Length == 1
         && readParameters[0].Address == Address
         && readParameters[0].ValueType == typeof(int))
     {
         return new object[] { Value };
     }
     throw new Exception();
 }
Esempio n. 9
0
        private object[] ReadOnce(ReadParameter[] readParameters)
        {
            if (readParameters == null)
                return null;
            var values = new List<object>();

            var merkers = new List<int>();
            var merkerParameters = new Dictionary<ReadParameter, int>();
            var lMerkers = new List<int>();
            var lMerkerParameters = new Dictionary<ReadParameter, int>();
            var registers = new List<KeyValuePair<int, int>>();
            var registerParameters = new Dictionary<ReadParameter, KeyValuePair<int, int>>();

            foreach (var parameter in readParameters)
            {
                var regex = new Regex(Pattern);
                var match = regex.Match(parameter.Address);
                if (!match.Success)
                {
                    throw new Exception(parameter.Address + " is not a valid device address");
                }

                #region Get device address

                var letter = match.Groups[1].ToString().ToUpper();
                var index = int.Parse(match.Groups[2].ToString());

                int? length;
                if (parameter.ValueType == typeof(bool))
                {
                    if (letter != "M" && letter != "L")
                    {
                        throw new Exception("Type " + parameter.ValueType.Name + " is not supported for" + parameter.Address);
                    }
                    length = 1;
                }
                else
                {
                    if (letter != "D")
                    {
                        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 deviceLastIndex = length.Value + index - 1;
                if ((letter == "M" && deviceLastIndex >= _melsec.MCount)
                    ||
                    (letter == "L" && deviceLastIndex >= _melsec.LCount)
                    ||
                    (letter == "D" && deviceLastIndex >= _melsec.DCount))
                {
                    throw new Exception(parameter.Address + ": device index is out of range");
                }

                #endregion

                #region Fill ReadInfo

                switch (letter)
                {
                    case "M":
                        if (!merkers.Contains(index))
                            merkers.Add(index);
                        if (!merkerParameters.ContainsKey(parameter))
                            merkerParameters.Add(parameter, index);
                        break;
                    case "L":
                        if (!lMerkers.Contains(index))
                            lMerkers.Add(index);
                        if (!lMerkerParameters.ContainsKey(parameter))
                            lMerkerParameters.Add(parameter, index);
                        break;
                    case "D":
                        var ir = new KeyValuePair<int, int>(index, length.Value);
                        if (!registers.Contains(ir))
                            registers.Add(ir);
                        if (!registerParameters.ContainsKey(parameter))
                            registerParameters.Add(parameter, ir);
                        break;
                }

                #endregion
            }

            #region Sort

            merkers = merkers.OrderBy(index => index).ToList();
            lMerkers = lMerkers.OrderBy(index => index).ToList();
            registers = registers.OrderBy(r => r.Key).ThenByDescending(r => r.Value).ToList();

            #endregion

            var merkersData = new Dictionary<int, bool>(merkers.Count);
            ReadBits(merkers, merkersData, _melsec.ReadMerkers, "M");
            var lMerkersData = new Dictionary<int, bool>(lMerkers.Count);
            ReadBits(lMerkers, lMerkersData, _melsec.ReadLMerkers, "L");
            var registersData = new Dictionary<KeyValuePair<int, int>, byte[]>(registers.Count);
            ReadRegisters(registers, registersData, _melsec.ReadRegisters);

            foreach (var parameter in readParameters)
            {
                int merkerIndex;
                int lMerkerIndex;
                KeyValuePair<int, int> registerAddr;
                if (merkerParameters.TryGetValue(parameter, out merkerIndex))
                {
                    bool value;
                    if (merkersData.TryGetValue(merkerIndex, out value))
                        values.Add(value);
                    else
                        values.Add(null);
                }
                else if (lMerkerParameters.TryGetValue(parameter, out lMerkerIndex))
                {
                    bool value;
                    if (lMerkersData.TryGetValue(lMerkerIndex, out value))
                        values.Add(value);
                    else
                        values.Add(null);
                }
                else if (registerParameters.TryGetValue(parameter, out registerAddr))
                {
                    byte[] value;
                    values.Add(registersData.TryGetValue(registerAddr, out value)
                                   ? GetValue(value, parameter.ValueType)
                                   : null);
                }
                else
                {
                    values.Add(null);
                }
            }

            return values.ToArray();
        }