예제 #1
0
        protected override void DoProcess(Dictionary <int, bool> variablesToRead)
        {
            // 1. Cambiar valores
            // 2. Leer valores
            Tuple <int, double> element;

            while (ValuesToWrite.TryDequeue(out element) && element != null)
            {
                if (((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.Coils) ||
                    ((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.DiscreteInputs ||
                     ((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.SingleCoil)))
                {
                    stationRtus[(_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Station.ID].CreateMessageResponsePackageWriteCoil(_variables[element.Item1], element.Item2);
                }
                else
                {
                    stationRtus[(_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Station.ID].CreateMessageResponsePackageWriteRegister(_variables[element.Item1], element.Item2);
                }
            }

            List <int> varsToRead = (from v in variablesToRead
                                     where v.Value
                                     select v.Key).ToList();

            if (varsToRead.Count > 0)
            {
                List <Variable> vars = new List <Variable>();
                for (int i = 0; i < varsToRead.Count; i++)
                {
                    if (_variables[varsToRead[i]].DriverConfiguration == null)
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "DriverConfiguration not assigned, variable: {0}", varsToRead[i]));
                    }
                    vars.Add(_variables[varsToRead[i]]);
                }
                foreach (var station in FactoryGroupModbus.CreateGroups(vars))
                {
                    if (!stationRtus.ContainsKey(station.Key))
                    {
                        stationRtus.Add(station.Key, new ModbusEncoderDecoder((station.Value.Values.First()[0].DriverConfiguration as ModbusConfiguration).Station));
                    }
                    List <Variable> Coils = new List <Variable>();
                    if (station.Value.TryGetValue(ModbusTypeData.Coils, out Coils))
                    {
                        ReadCoils(Coils, station.Key);
                    }
                    List <Variable> Registers = new List <Variable>();
                    if (station.Value.TryGetValue(ModbusTypeData.Inputs_Registers, out Registers))
                    {
                        ReadRegisters(Registers, station.Key);
                    }
                }
            }
        }
예제 #2
0
        protected override void DoProcess(Dictionary <int, bool> variablesToRead)
        {
            // 1. Cambiar valores
            // 2. Leer valores
            Tuple <int, double> element;

            while (ValuesToWrite.TryDequeue(out element) && element != null)
            {
                if (((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.Coils) ||
                    ((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.DiscreteInputs ||
                     ((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.SingleCoil)))
                {
                    tcp.WriteSingleCoils((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Station.ID, (_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Address, Convert.ToBoolean(element.Item2));
                }
                else
                {
                    tcp.WriteSingleRegister((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Station.ID, (_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Address, new byte[] { (byte)element.Item2 });
                }
            }

            List <int> varsToRead = (from v in variablesToRead
                                     where v.Value
                                     select v.Key).ToList();

            if (varsToRead.Count > 0)
            {
                List <Variable> vars = new List <Variable>();
                for (int i = 0; i < varsToRead.Count; i++)
                {
                    if (_variables[varsToRead[i]].DriverConfiguration == null)
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "DriverConfiguration not assigned, variable: {0}", varsToRead[i]));
                    }
                    vars.Add(_variables[varsToRead[i]]);
                }
                foreach (var variable in vars)
                {
                    if (((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.Coils) ||
                        ((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.DiscreteInputs ||
                         ((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).TypeData == ModbusTypeData.SingleCoil)))
                    {
                        byte[] value = null;
                        tcp.ReadCoils((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Station.ID, (_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Address, 1, ref value);
                        if (value != null)
                        {
                            SendValueToStorage(variable.ID, value[0]);
                        }
                    }
                    else
                    {
                        byte[] value = null;
                        tcp.ReadDiscreteInputs((_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Station.ID, (_variables[element.Item1].DriverConfiguration as ModbusConfiguration).Address, 1, ref value);
                        if (value != null)
                        {
                            SendValueToStorage(variable.ID, value[0]);
                        }
                    }
                }
            }
        }