/// <summary> /// Retrieves subset of data from collection. /// </summary> /// <typeparam name="T">The collection type.</typeparam> /// <typeparam name="U">The type of elements in the collection.</typeparam> public T ReadData <T, U>(IModbusDataCollection <U> dataSource, ushort startAddress, ushort count, object syncRoot) where T : Collection <U>, new() { int startIndex = startAddress + 1; if (startIndex < 0 || dataSource.Count < startIndex + count) { throw new InvalidModbusRequestException(Modbus.IllegalDataAddress); } U[] dataToRetrieve; lock (syncRoot) dataToRetrieve = dataSource.Slice(startIndex, count).ToArray(); T result = new T(); for (int i = 0; i < count; i++) { result.Add(dataToRetrieve[i]); } DataStoreReadFrom.Raise(this, DataStoreEventArgs.CreateDataStoreEventArgs(startAddress, dataSource.ModbusDataType, result)); return(result); }
/// <summary> /// Retrieves subset of data from collection. /// </summary> /// <typeparam name="T">The collection type.</typeparam> /// <typeparam name="U">The type of elements in the collection.</typeparam> internal static T ReadData <T, U>(DataStore dataStore, ModbusDataCollection <U> dataSource, ushort startAddress, ushort count, object syncRoot) where T : Collection <U>, new() { int startIndex = startAddress + 1; if (startIndex < 0 || startIndex >= dataSource.Count) { throw new ArgumentOutOfRangeException("Start address was out of range. Must be non-negative and <= the size of the collection."); } if (dataSource.Count < startIndex + count) { throw new ArgumentOutOfRangeException("Read is outside valid range."); } U[] dataToRetrieve; lock (syncRoot) dataToRetrieve = dataSource.Slice(startIndex, count).ToArray(); T result = new T(); for (int i = 0; i < count; i++) { result.Add(dataToRetrieve[i]); } dataStore.DataStoreReadFrom.Raise(dataStore, DataStoreEventArgs.CreateDataStoreEventArgs(startAddress, dataSource.ModbusDataType, result)); return(result); }
/// <summary> /// Write data to data store. /// </summary> /// <typeparam name="TData">The type of the data.</typeparam> internal static void WriteData <TData>( DataStore dataStore, IEnumerable <TData> items, ModbusDataCollection <TData> destination, ushort startAddress, object syncRoot) { DataStoreEventArgs dataStoreEventArgs; int startIndex = startAddress + 1; if (startIndex < 0 || destination.Count < startIndex + items.Count()) { throw new InvalidModbusRequestException(Modbus.IllegalDataAddress); } lock (syncRoot) { Update(items, destination, startIndex); } dataStoreEventArgs = DataStoreEventArgs.CreateDataStoreEventArgs( startAddress, destination.ModbusDataType, items); dataStore.DataStoreWrittenTo?.Invoke(dataStore, dataStoreEventArgs); }
/// <summary> /// Write data to data store. /// </summary> /// <typeparam name="TData">The type of the data.</typeparam> public void WriteData <TData>(IEnumerable <TData> items, IModbusDataCollection <TData> destination, ushort startAddress, object syncRoot) { int startIndex = startAddress + 1; if (startIndex < 0 || destination.Count < startIndex + items.Count()) { throw new InvalidModbusRequestException(Modbus.IllegalDataAddress); } lock (syncRoot) Update(items, (IList <TData>)destination, startIndex); DataStoreWrittenTo.Raise(this, DataStoreEventArgs.CreateDataStoreEventArgs(startAddress, destination.ModbusDataType, items)); }
/// <summary> /// Write data to data store. /// </summary> /// <typeparam name="TData">The type of the data.</typeparam> internal static void WriteData <TData>(DataStore dataStore, IEnumerable <TData> items, ModbusDataCollection <TData> destination, ushort startAddress, object syncRoot) { int startIndex = startAddress + 1; if (startIndex < 0 || startIndex >= destination.Count) { throw new ArgumentOutOfRangeException("Start address was out of range. Must be non-negative and <= the size of the collection."); } if (destination.Count < startIndex + items.Count()) { throw new ArgumentOutOfRangeException("Items collection is too large to write at specified start index."); } lock (syncRoot) Update(items, destination, startIndex); dataStore.DataStoreWrittenTo.Raise(dataStore, DataStoreEventArgs.CreateDataStoreEventArgs(startAddress, destination.ModbusDataType, items)); }