Пример #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>
        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);
        }
Пример #2
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);
        }
Пример #3
0
        internal static DataStoreEventArgs CreateDataStoreEventArgs <T>(ushort startAddress,
                                                                        ModbusDataType modbusDataType, IEnumerable <T> data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            DataStoreEventArgs eventArgs;

            if (typeof(T) == typeof(bool))
            {
                var a = new ReadOnlyCollection <bool>(data.Cast <bool>().ToArray());
                eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
                {
                    Data = DiscriminatedUnion <ReadOnlyCollection <bool>, ReadOnlyCollection <ushort> > .CreateA(a)
                };
            }
            else if (typeof(T) == typeof(ushort))
            {
                var b = new ReadOnlyCollection <ushort>(data.Cast <ushort>().ToArray());
                eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
                {
                    Data = DiscriminatedUnion <ReadOnlyCollection <bool>, ReadOnlyCollection <ushort> > .CreateB(b)
                };
            }
            else
            {
                throw new ArgumentException("Generic type T should be of type bool or ushort");
            }

            return(eventArgs);
        }
Пример #4
0
        /// <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);
        }
        private void Modbus_DataStoreWriteTo(object sender, Modbus.Data.DataStoreEventArgs e)
        {
            switch (e.ModbusDataType)
            {
            case ModbusDataType.HoldingRegister:
                //запросы состояния
                if (e.Data.B.Count == 1)
                {
                    logger.Info("Перешел в состояние " + e.Data.B[0] + " обработка");
                    slave.DataStore.HoldingRegisters[1] = SlaveState.havenot_time;

                    processing_singleregx(e.Data.B[0]);
                    //Console.WriteLine("Пришла команда на обработку"+e.Data.B[0]);
                    logger.Info("Пришла команда на обработку" + e.Data.B[0]);
                }

                if (e.Data.B.Count > 1)
                {
                    //Console.WriteLine("Пришел пакет с данными:");
                    logger.Info("Пришел пакет с данными:");
                    for (int i = 0; i < e.Data.B.Count; i++)
                    {
                        receive_packet_data[i] = e.Data.B[i];
                        Console.Write(receive_packet_data[i] + " ");
                    }
                    Console.WriteLine("");
                    logger.Info("Перешел в состояние " + e.Data.B[0] + " обработка");
                    slave.DataStore.HoldingRegisters[1] = SlaveState.havenot_time;

                    //Console.WriteLine("Обработка пакета");
                    logger.Info("Обработка пакета");
                    processing_infopaket(receive_packet_data);
                }

                break;

            case ModbusDataType.Coil:
                for (int i = 0; i < e.Data.A.Count; i++)
                {
                    //set DO
                    //e.Data.A[i] already write to
                    //slave.DataStore.CoilDiscretes[e.StartAdd

                    //ress + i + 1];
                    //e.StartAddress starts from 0
                    //You can set DO value to hardware here
                }
                break;
            }
        }
Пример #6
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));
        }
Пример #7
0
        /// <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));
        }
Пример #8
0
        private void Modbus_DataStoreWriteTo(object sender, Modbus.Data.DataStoreEventArgs e)
        {
            //this.Text = "DataType=" + e.ModbusDataType.ToString() + "  StartAdress=" + e.StartAddress;
            int iAddress = e.StartAddress;//e.StartAddress;

            switch (e.ModbusDataType)
            {
            case ModbusDataType.HoldingRegister:

                for (int i = 0; i < e.Data.B.Count; i++)
                {
                    //Set AO

                    //e.Data.B[i] already write to slave.DataStore.HoldingRegisters[e.StartAddress + i + 1]
                    //e.StartAddress starts from 0
                    //You can set AO value to hardware here

                    DoAOUpdate(iAddress, e.Data.B[i].ToString());
                    iAddress++;
                }
                break;

            case ModbusDataType.Coil:
                for (int i = 0; i < e.Data.A.Count; i++)
                {
                    //Set DO
                    //e.Data.A[i] already write to slave.DataStore.CoilDiscretes[e.StartAddress + i + 1]
                    //e.StartAddress starts from 0
                    //You can set DO value to hardware here

                    DoDOUpdate(iAddress, e.Data.A[i]);
                    iAddress++;
                    if (e.Data.A.Count == 1)
                    {
                        break;
                    }
                }
                break;
            }
        }