Ejemplo n.º 1
0
        private void AcquisitionThread()
        {
            try
            {
                Logger.LogInfo("AcquisitionThread is started.");

                while (threadActiveSignal)
                {
                    if (this.readCommandEnqueuer == null)
                    {
                        string message = $"Read command enqueuer is null.";
                        Logger.LogError(message);

                        Thread.Sleep(scadaConfig.Interval);
                        continue;
                    }

                    //MODEL UPDATE -> will swap incoming and current SCADAModel in commit step, so we have to save the reference locally
                    Dictionary <PointType, Dictionary <ushort, long> > currentAddressToGidMap = scadaModel.CurrentAddressToGidMap;

                    foreach (PointType pointType in currentAddressToGidMap.Keys)
                    {
                        ushort length       = 6; //expected by protocol
                        ushort startAddress = 1;
                        ushort quantity;

                        IReadModbusFunction         modbusFunction;
                        ModbusReadCommandParameters mdb_read;

                        if (pointType == PointType.DIGITAL_OUTPUT)
                        {
                            quantity = (ushort)currentAddressToGidMap[PointType.DIGITAL_OUTPUT].Count;

                            if (quantity == 0)
                            {
                                continue;
                            }

                            mdb_read = new ModbusReadCommandParameters(length,
                                                                       (byte)ModbusFunctionCode.READ_COILS,
                                                                       startAddress,
                                                                       quantity);
                        }
                        //DIGITAL_INPUT
                        else if (pointType == PointType.DIGITAL_INPUT)
                        {
                            quantity = (ushort)currentAddressToGidMap[PointType.DIGITAL_INPUT].Count;

                            if (quantity == 0)
                            {
                                continue;
                            }

                            mdb_read = new ModbusReadCommandParameters(length,
                                                                       (byte)ModbusFunctionCode.READ_DISCRETE_INPUTS,
                                                                       startAddress,
                                                                       quantity);
                        }
                        //ANALOG_OUTPUT
                        else if (pointType == PointType.ANALOG_OUTPUT)
                        {
                            quantity = (ushort)currentAddressToGidMap[PointType.ANALOG_OUTPUT].Count;

                            if (quantity == 0)
                            {
                                continue;
                            }

                            mdb_read = new ModbusReadCommandParameters(length,
                                                                       (byte)ModbusFunctionCode.READ_HOLDING_REGISTERS,
                                                                       startAddress,
                                                                       quantity);
                        }
                        //ANALOG_INPUT
                        else if (pointType == PointType.ANALOG_INPUT)
                        {
                            quantity = (ushort)currentAddressToGidMap[PointType.ANALOG_INPUT].Count;

                            if (quantity == 0)
                            {
                                continue;
                            }

                            mdb_read = new ModbusReadCommandParameters(length,
                                                                       (byte)ModbusFunctionCode.READ_INPUT_REGISTERS,
                                                                       startAddress,
                                                                       quantity);
                        }
                        else if (pointType == PointType.HR_LONG)
                        {
                            continue;
                        }
                        else
                        {
                            mdb_read       = null;
                            modbusFunction = null;
                            string message = $"PointType:{pointType} value is invalid";
                            Logger.LogError(message);
                            continue;
                        }

                        modbusFunction = FunctionFactory.CreateReadModbusFunction(mdb_read);

                        if (this.readCommandEnqueuer.EnqueueReadCommand(modbusFunction))
                        {
                            //Logger.LogDebug($"Modbus function enquided. Point type is {pointType}, quantity {quantity}.");
                        }
                    }

                    Thread.Sleep(scadaConfig.Interval);
                }

                Logger.LogInfo("AcquisitionThread is stopped.");
            }
            catch (Exception e)
            {
                string message = "Exception caught in AcquisitionThread.";
                Logger.LogError(message, e);
            }
        }