Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        internal static WriteSingleRegisterRequestResponse WriteSingleRegister(
            WriteSingleRegisterRequestResponse request, IDataStore dataStore, IModbusDataCollection <ushort> dataSource)
        {
            dataStore.WriteData(request.Data, dataSource, request.StartAddress, dataStore.SyncRoot);

            return(request);
        }
Ejemplo n.º 3
0
        internal static ReadCoilsInputsResponse ReadDiscretes(ReadCoilsInputsRequest request, IDataStore dataStore,
                                                              IModbusDataCollection <bool> dataSource)
        {
            DiscreteCollection data = dataStore.ReadData <DiscreteCollection, bool>(dataSource,
                                                                                    request.StartAddress, request.NumberOfPoints, dataStore.SyncRoot);
            var response = new ReadCoilsInputsResponse(request.FunctionCode, request.SlaveAddress, data.ByteCount, data);

            return(response);
        }
Ejemplo n.º 4
0
        /// <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));
        }
Ejemplo n.º 5
0
        internal static WriteMultipleCoilsResponse WriteMultipleCoils(WriteMultipleCoilsRequest request,
                                                                      IDataStore dataStore, IModbusDataCollection <bool> dataSource)
        {
            dataStore.WriteData(request.Data.Take(request.NumberOfPoints), dataSource, request.StartAddress,
                                dataStore.SyncRoot);
            var response = new WriteMultipleCoilsResponse(request.SlaveAddress, request.StartAddress, request.NumberOfPoints);

            return(response);
        }
Ejemplo n.º 6
0
        internal static WriteSingleCoilRequestResponse WriteSingleCoil(WriteSingleCoilRequestResponse request,
                                                                       IDataStore dataStore, IModbusDataCollection <bool> dataSource)
        {
            dataStore.WriteData(new DiscreteCollection(request.Data[0] == Modbus.CoilOn), dataSource,
                                request.StartAddress, dataStore.SyncRoot);

            return(request);
        }
Ejemplo n.º 7
0
        internal static ReadHoldingInputRegistersResponse ReadRegisters(ReadHoldingInputRegistersRequest request,
                                                                        IDataStore dataStore, IModbusDataCollection <ushort> dataSource)
        {
            RegisterCollection data = dataStore.ReadData <RegisterCollection, ushort>(dataSource,
                                                                                      request.StartAddress, request.NumberOfPoints, dataStore.SyncRoot);
            var response = new ReadHoldingInputRegistersResponse(request.FunctionCode, request.SlaveAddress, data);

            return(response);
        }