Пример #1
0
        /// <summary>
        /// Creates a new Modbus command based on the element configuration.
        /// </summary>
        private ModbusCmd CreateModbusCmd(DeviceTemplateOptions options,
                                          ElemGroupConfig elemGroupConfig, ElemConfig elemConfig, int elemIndex)
        {
            ModbusCmd modbusCmd = deviceModel.CreateModbusCmd(elemGroupConfig.DataBlock, false);

            modbusCmd.Name      = elemConfig.Name;
            modbusCmd.Address   = (ushort)(elemGroupConfig.Address + elemIndex);
            modbusCmd.ElemType  = elemConfig.ElemType;
            modbusCmd.ElemCnt   = 1;
            modbusCmd.ByteOrder = ModbusUtils.ParseByteOrder(elemConfig.ByteOrder) ??
                                  options.GetDefaultByteOrder(ModbusUtils.GetDataLength(elemConfig.ElemType));
            modbusCmd.CmdNum  = 0;
            modbusCmd.CmdCode = elemConfig.TagCode;

            modbusCmd.InitReqPDU();
            modbusCmd.InitReqADU(deviceModel.Addr, transMode);
            return(modbusCmd);
        }
Пример #2
0
        /// <summary>
        /// Creates a new Modbus command based on the command configuration.
        /// </summary>
        private ModbusCmd CreateModbusCmd(DeviceTemplateOptions options, CmdConfig cmdConfig)
        {
            ModbusCmd modbusCmd = deviceModel.CreateModbusCmd(cmdConfig.DataBlock, cmdConfig.Multiple);

            modbusCmd.Name      = cmdConfig.Name;
            modbusCmd.Address   = (ushort)cmdConfig.Address;
            modbusCmd.ElemType  = cmdConfig.ElemType;
            modbusCmd.ElemCnt   = cmdConfig.ElemCnt;
            modbusCmd.ByteOrder = ModbusUtils.ParseByteOrder(cmdConfig.ByteOrder) ??
                                  options.GetDefaultByteOrder(ModbusUtils.GetDataLength(cmdConfig.ElemType) * cmdConfig.ElemCnt);
            modbusCmd.CmdNum  = cmdConfig.CmdNum;
            modbusCmd.CmdCode = cmdConfig.CmdCode;

            if (cmdConfig.DataBlock == DataBlock.Custom)
            {
                modbusCmd.SetFuncCode((byte)cmdConfig.CustomFuncCode);
            }

            modbusCmd.InitReqPDU();
            modbusCmd.InitReqADU(deviceModel.Addr, transMode);
            return(modbusCmd);
        }
Пример #3
0
        /// <summary>
        /// Initializes the device tags.
        /// </summary>
        public override void InitDeviceTags()
        {
            DeviceTemplate deviceTemplate = GetDeviceTemplate();

            if (deviceTemplate == null)
            {
                return;
            }

            // create device model
            deviceModel      = CreateDeviceModel();
            deviceModel.Addr = (byte)NumAddress;

            // add model elements and device tags
            foreach (ElemGroupConfig elemGroupConfig in deviceTemplate.ElemGroups)
            {
                bool      groupActive   = elemGroupConfig.Active;
                bool      groupCommands = groupActive && elemGroupConfig.ReadOnlyEnabled;
                ElemGroup elemGroup     = null;
                TagGroup  tagGroup      = new TagGroup(elemGroupConfig.Name)
                {
                    Hidden = !groupActive
                };
                int elemIndex = 0;

                if (groupActive)
                {
                    elemGroup             = deviceModel.CreateElemGroup(elemGroupConfig.DataBlock);
                    elemGroup.Name        = elemGroupConfig.Name;
                    elemGroup.Address     = (ushort)elemGroupConfig.Address;
                    elemGroup.StartTagIdx = DeviceTags.Count;
                }

                foreach (ElemConfig elemConfig in elemGroupConfig.Elems)
                {
                    // add model element
                    if (groupActive)
                    {
                        Elem elem = elemGroup.CreateElem();
                        elem.Name      = elemConfig.Name;
                        elem.ElemType  = elemConfig.ElemType;
                        elem.ByteOrder = ModbusUtils.ParseByteOrder(elemConfig.ByteOrder) ??
                                         deviceTemplate.Options.GetDefaultByteOrder(ModbusUtils.GetDataLength(elemConfig.ElemType));
                        elemGroup.Elems.Add(elem);
                    }

                    // add model command
                    if (groupCommands && !elemConfig.ReadOnly && !string.IsNullOrEmpty(elemConfig.TagCode))
                    {
                        deviceModel.Cmds.Add(
                            CreateModbusCmd(deviceTemplate.Options, elemGroupConfig, elemConfig, elemIndex));
                    }

                    // add device tag
                    tagGroup.AddTag(elemConfig.TagCode, elemConfig.Name).SetFormat(GetTagFormat(elemConfig));
                    elemIndex++;
                }

                if (groupActive)
                {
                    elemGroup.InitReqPDU();
                    elemGroup.InitReqADU(deviceModel.Addr, transMode);
                    deviceModel.ElemGroups.Add(elemGroup);
                }

                DeviceTags.AddGroup(tagGroup);
            }

            // add model commands
            foreach (CmdConfig cmdConfig in deviceTemplate.Cmds)
            {
                deviceModel.Cmds.Add(CreateModbusCmd(deviceTemplate.Options, cmdConfig));
            }

            deviceModel.InitCmdMap();
            CanSendCommands = deviceModel.Cmds.Count > 0;
            InitModbusPoll();
        }
Пример #4
0
        /// <summary>
        /// Gets the channel prototypes for the device.
        /// </summary>
        public override ICollection <CnlPrototype> GetCnlPrototypes()
        {
            if (LoadDeviceTemplate() is not DeviceTemplate deviceTemplate)
            {
                return(null);
            }

            List <CnlPrototype> cnlPrototypes = new();
            int tagNum = 1;

            foreach (ElemGroupConfig elemGroupConfig in deviceTemplate.ElemGroups)
            {
                foreach (ElemConfig elemConfig in elemGroupConfig.Elems)
                {
                    // create channel for element
                    bool isBool    = elemConfig.ElemType == ElemType.Bool;
                    int  eventMask = new EventMask
                    {
                        Enabled      = true,
                        DataChange   = isBool,
                        StatusChange = !isBool,
                        Command      = !elemConfig.ReadOnly
                    }.Value;

                    cnlPrototypes.Add(new CnlPrototype
                    {
                        Active     = elemGroupConfig.Active,
                        Name       = elemConfig.Name,
                        CnlTypeID  = elemConfig.ReadOnly ? CnlTypeID.Input : CnlTypeID.InputOutput,
                        TagNum     = string.IsNullOrEmpty(elemConfig.TagCode) ? tagNum : null,
                        TagCode    = elemConfig.TagCode,
                        FormatCode = isBool
                            ? FormatCode.OffOn
                            : elemConfig.IsBitMask ? FormatCode.X : null,
                        EventMask = eventMask
                    });

                    // create channels for bit mask
                    if (elemConfig.IsBitMask && elemConfig.ElemType != ElemType.Bool)
                    {
                        eventMask = new EventMask
                        {
                            Enabled    = true,
                            DataChange = true,
                            Command    = !elemConfig.ReadOnly
                        }.Value;

                        for (int bit = 0, bitCnt = ModbusUtils.GetDataLength(elemConfig.ElemType) * 8;
                             bit < bitCnt; bit++)
                        {
                            cnlPrototypes.Add(new CnlPrototype
                            {
                                Active         = elemGroupConfig.Active,
                                Name           = elemConfig.Name + "[" + bit + "]",
                                CnlTypeID      = elemConfig.ReadOnly ? CnlTypeID.Calculated : CnlTypeID.CalculatedOutput,
                                FormatCode     = FormatCode.OffOn,
                                FormulaEnabled = true,
                                InFormula      = $"GetBit(DataRel({-bit - 1}), {bit})",
                                OutFormula     = elemConfig.ReadOnly ? null : $"SetBit(DataRel({-bit - 1}), {bit}, Cmd)",
                                EventMask      = eventMask
                            });
                        }
                    }

                    tagNum++;
                }
            }

            // create channels for commands
            int cmdEventMask = new EventMask {
                Enabled = true, Command = true
            }.Value;

            foreach (CmdConfig cmdConfig in deviceTemplate.Cmds)
            {
                cnlPrototypes.Add(new CnlPrototype
                {
                    Name       = cmdConfig.Name,
                    CnlTypeID  = CnlTypeID.Output,
                    TagNum     = string.IsNullOrEmpty(cmdConfig.CmdCode) ? cmdConfig.CmdNum : null,
                    TagCode    = cmdConfig.CmdCode,
                    FormatCode = cmdConfig.DataBlock == DataBlock.Coils && !cmdConfig.Multiple ?
                                 FormatCode.OffOn : null,
                    EventMask = cmdEventMask
                });
            }

            return(cnlPrototypes);
        }