Пример #1
0
        /// <summary>
        /// Gets the appropriate default byte order as array.
        /// </summary>
        public int[] GetDefaultByteOrder(int byteCnt)
        {
            switch (byteCnt)
            {
            case 2:
                if (byteOrderArr2 == null)
                {
                    byteOrderArr2 = ModbusUtils.ParseByteOrder(DefByteOrder2);
                }
                return(byteOrderArr2);

            case 4:
                if (byteOrderArr4 == null)
                {
                    byteOrderArr4 = ModbusUtils.ParseByteOrder(DefByteOrder4);
                }
                return(byteOrderArr4);

            case 8:
                if (byteOrderArr8 == null)
                {
                    byteOrderArr8 = ModbusUtils.ParseByteOrder(DefByteOrder8);
                }
                return(byteOrderArr8);

            default:
                return(null);
            }
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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();
        }