Esempio n. 1
0
        public bool AddItems(IList <TagMetaData> items)
        {
            int count = items.Count;

            if (_items == null)
            {
                _items = new Dictionary <short, ITag>(count);
            }
            lock (_server.SyncRoot)
            {
                for (int i = 0; i < count; i++)
                {
                    ITag          dataItem = null;
                    TagMetaData   meta     = items[i];
                    DeviceAddress addr     = new DeviceAddress(0, 0, 0, meta.ID, meta.Size, 0, meta.DataType);
                    switch (meta.DataType)
                    {
                    case DataType.BOOL:
                        dataItem = new BoolTag(meta.ID, addr, this);
                        break;

                    case DataType.BYTE:
                        dataItem = new ByteTag(meta.ID, addr, this);
                        break;

                    case DataType.WORD:
                        dataItem = new UShortTag(meta.ID, addr, this);
                        break;

                    case DataType.SHORT:
                        dataItem = new ShortTag(meta.ID, addr, this);
                        break;

                    case DataType.INT:
                        dataItem = new IntTag(meta.ID, addr, this);
                        break;

                    case DataType.DWORD:
                        dataItem = new UIntTag(meta.ID, addr, this);
                        break;

                    case DataType.FLOAT:
                        dataItem = new FloatTag(meta.ID, addr, this);
                        break;

                    case DataType.STR:
                        dataItem = new StringTag(meta.ID, addr, this);
                        break;

                    default:
                        dataItem = new BoolTag(meta.ID, addr, this);
                        break;
                    }
                    _items.Add(meta.ID, dataItem);
                    _server.AddItemIndex(meta.Name, dataItem);
                }
            }
            //Init();
            return(true);
        }
Esempio n. 2
0
 private void InnerWriteBoolTagValue(BoolTag tag)
 {
     foreach (var item in tag.GetAddresses())
     {
         master.WriteSingleCoil(myDevice.Id, (ushort)(item - 1), tag.GetWriteValue());
     }
 }
Esempio n. 3
0
 public void WriteBoolTagValue(BoolTag tag)
 {
     foreach (var item in tag.GetAddresses())
     {
         master.WriteSingleCoilAsync((ushort)(item - 1), tag.GetWriteValue());
     }
 }
Esempio n. 4
0
        public void BoolTag_AddAddress()
        {
            BoolTag myTag = new BoolTag(StatusFunction.InputStatus);

            myTag.AddAddress(5);
            myTag.AddAddress(12);
            myTag.SetData(GetModbusBoolValues());
        }
Esempio n. 5
0
        public bool AddItems(IList <TagMetaData> items)
        {
            int count = items.Count;

            if (_items == null)
            {
                _items = new List <ITag>();
            }
            lock (_server.SyncRoot)
            {
                int j = 0;
                for (int i = 0; i < count; i++)
                {
                    ITag        dataItem = null;
                    TagMetaData meta     = items[i];
                    if (meta.GroupID == this._id)
                    {
                        DeviceAddress addr = new DeviceAddress(-1, (ushort)meta.GroupID, (ushort)meta.ID, j++, meta.Size, 0, meta.DataType);
                        switch (meta.DataType)
                        {
                        case DataType.BOOL:
                            dataItem = new BoolTag(meta.ID, addr, this);
                            break;

                        case DataType.BYTE:
                            dataItem = new ByteTag(meta.ID, addr, this);
                            break;

                        case DataType.WORD:
                        case DataType.SHORT:
                            dataItem = new ShortTag(meta.ID, addr, this);
                            break;

                        case DataType.TIME:
                        case DataType.INT:
                            dataItem = new IntTag(meta.ID, addr, this);
                            break;

                        case DataType.FLOAT:
                            dataItem = new FloatTag(meta.ID, addr, this);
                            break;

                        case DataType.STR:
                            dataItem = new StringTag(meta.ID, addr, this);
                            break;

                        default:
                            dataItem = new BoolTag(meta.ID, addr, this);
                            break;
                        }
                        _items.Add(dataItem);
                        _server.AddItemIndex(meta.Name, dataItem);
                    }
                }
            }
            return(true);
        }
Esempio n. 6
0
        public void BoolTag_OrMerge3()
        {
            BoolTag myTag = new BoolTag(StatusFunction.InputStatus);

            myTag.AddAddress(8); // false
            myTag.AddAddress(9); // false
            myTag.MergeType = TagAddressMergeType.OrMerge;
            myTag.SetData(GetModbusBoolValues());
            Assert.AreEqual(false, myTag.Value);
        }
Esempio n. 7
0
        public void BoolTag_AndMerge2()
        {
            BoolTag myTag = new BoolTag(StatusFunction.InputStatus);

            myTag.AddAddress(3); // true
            myTag.AddAddress(4); // true
            myTag.MergeType = TagAddressMergeType.AndMerge;
            myTag.SetData(GetModbusBoolValues());
            Assert.AreEqual(true, myTag.Value);
        }
Esempio n. 8
0
        public void BoolTag_OrMask5()
        {
            BoolTag myTag = new BoolTag(StatusFunction.InputStatus);

            myTag.AddAddress(5); // false
            myTag.MaskType = TagAddressMaskType.None;
            myTag.Mask     = true;
            myTag.SetData(GetModbusBoolValues());
            Assert.AreEqual(false, myTag.Value);
        }
Esempio n. 9
0
        public void BoolTag_OrMask2()
        {
            BoolTag myTag = new BoolTag(StatusFunction.InputStatus);

            myTag.AddAddress(3); // true
            myTag.MaskType = TagAddressMaskType.OrMask;
            myTag.Mask     = false;
            myTag.SetData(GetModbusBoolValues());
            Assert.AreEqual(true, myTag.Value);
        }
Esempio n. 10
0
 public void RefreshValues(ReadMap map, bool[] modbusvalues)
 {
     //TODO : May performance better
     for (int i = 0; i < modbusvalues.Length; i++)
     {
         ushort address = (ushort)(i + map.StartAddress);
         if (!CoilValues.ContainsKey(address))
         {
             CoilValues.Add(address, modbusvalues[i]);
         }
         else
         {
             CoilValues[address] = modbusvalues[i];
         }
     }
     foreach (var item in Values)
     {
         BoolTag tag = item as BoolTag;
         tag.SetData(CoilValues);
     }
 }
Esempio n. 11
0
        public void DefineBoolTag(
            StatusFunction function,
            TagAddressMaskType masktype = TagAddressMaskType.None,
            bool mask = false,
            TagAddressMergeType mergetype = TagAddressMergeType.OrMerge)
        {
            if (InnerTag != null)
            {
                return;
            }
            BoolTag innertag = new BoolTag(function)
            {
                MaskType  = masktype,
                Mask      = mask,
                MergeType = mergetype
            };

            InnerTag = innertag;
            InnerTag.TagValueChangedEvent += delegate(ITagType tag)
            {
                TagStatusChanged?.Invoke(this, EventArgs.Empty);
            };
        }
Esempio n. 12
0
        public bool AddItems(IList <TagMetaData> items)
        {
            int count = items.Count;

            if (_items == null)
            {
                _items  = new List <ITag>(count);
                mapping = new Dictionary <short, ITag>(count);
            }
            List <OPCITEMDEF> itemArray = new List <OPCITEMDEF>(count);

            for (int i = 0; i < count; i++)
            {
                if (items[i].GroupID == this._Id)
                {
                    itemArray.Add(new OPCITEMDEF {
                        hClient = items[i].ID, szItemID = items[i].Address, bActive = true, wReserved = (short)i
                    });
                }
            }
            IntPtr pAddResults;
            IntPtr pErrors;

            if (!HRESULTS.Succeeded(_itemMgt.AddItems(itemArray.Count, itemArray.ToArray(), out pAddResults, out pErrors)))
            {
                return(false);
            }
            int iStructSize = Marshal.SizeOf(typeof(OPCITEMRESULT));

            lock (_server.SyncRoot)
            {
                for (int i = 0; i < itemArray.Count; i++)
                {
                    try
                    {
                        if (Marshal.ReadInt32(pErrors) == 0)
                        {
                            ITag dataItem = null;
                            var  itemDef  = itemArray[i];
                            //string addr = string.Concat(_serverId, ',', Marshal.ReadInt32(pAddResults));
                            DataType type = items[itemDef.wReserved].DataType;
                            switch (type)
                            {
                            case DataType.BOOL:
                                dataItem = new BoolTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 1, 0, DataType.BOOL), this);
                                break;

                            case DataType.BYTE:
                                dataItem = new ByteTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 1, 0, DataType.BYTE), this);
                                break;

                            case DataType.WORD:
                                dataItem = new UShortTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 2, 0, DataType.WORD), this);
                                break;

                            case DataType.SHORT:
                                dataItem = new ShortTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 2, 0, DataType.SHORT), this);
                                break;

                            case DataType.INT:
                                dataItem = new IntTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 4, 0, DataType.INT), this);
                                break;

                            case DataType.DWORD:
                                dataItem = new UIntTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 4, 0, DataType.DWORD), this);
                                break;

                            case DataType.FLOAT:
                                dataItem = new FloatTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 4, 0, DataType.FLOAT), this);
                                break;

                            case DataType.SYS:
                            case DataType.STR:
                                dataItem = new StringTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), 64, 0, DataType.STR), this);
                                break;

                            default:
                                break;
                                //case VarEnum.VT_ARRAY:
                                //    dataItem = new ArrayTag((short)itemDef.hClient, new DeviceAddress(-0x100, 0, 0, Marshal.ReadInt32(pAddResults), (byte)Marshal.ReadInt16(pAddResults + 18), 0, DataType.ARRAY), this);
                                //    break;
                            }
                            if (dataItem != null)
                            {
                                _items.Add(dataItem);
                                mapping.Add((short)itemDef.hClient, dataItem);
                                _server.AddItemIndex(items[itemDef.wReserved].Name, dataItem);
                            }
                            pAddResults += iStructSize;
                            pErrors     += 4;
                        }
                    }
                    catch (Exception err)
                    {
                        if (err.Message != null)
                        {
                        }
                    }
                }
                //Marshal.FreeCoTaskMem(pAddResults);
                //Marshal.FreeCoTaskMem(pErrors);
                _items.TrimExcess();
                return(true);
            }
        }
Esempio n. 13
0
        public void BoolTag_Initialize()
        {
            BoolTag myTag1 = new BoolTag(StatusFunction.InputStatus);

            Assert.AreEqual(myTag1.InnerTagType, TagType.BoolTag);
        }
Esempio n. 14
0
 public void WriteBoolTagValue(BoolTag tag)
 {
     boolWriteList.Enqueue(tag);
 }
Esempio n. 15
0
        public bool AddItems(IList <TagMetaData> items)
        {
            FxSerialReader fxReader = _plcReader as FxSerialReader;

            if (fxReader == null)
            {
                return(false);
            }


            int count = items.Count;

            if (_items == null)
            {
                _items = new List <ITag>(count);
            }
            lock (_server.SyncRoot)
            {
                for (int i = 0; i < count; i++)
                {
                    ITag        dataItem = null;
                    TagMetaData meta     = items[i];
                    if (meta.GroupID == this._id)
                    {
                        DeviceAddress addr = fxReader.GetDeviceAddress(meta);
                        if (addr.DataSize == 0)
                        {
                            addr.DataSize = meta.Size;
                        }
                        if (addr.VarType == DataType.NONE)
                        {
                            addr.VarType = meta.DataType;
                        }
                        if (addr.VarType != DataType.BOOL)
                        {
                            addr.Bit = 0;
                        }
                        switch (meta.DataType)
                        {
                        case DataType.BOOL:
                            dataItem = new BoolTag(meta.ID, addr, this);
                            break;

                        case DataType.BYTE:
                            dataItem = new ByteTag(meta.ID, addr, this);
                            break;

                        case DataType.WORD:
                            dataItem = new UShortTag(meta.ID, addr, this);
                            break;

                        case DataType.SHORT:
                            dataItem = new ShortTag(meta.ID, addr, this);
                            break;

                        case DataType.DWORD:
                            dataItem = new UIntTag(meta.ID, addr, this);
                            break;

                        case DataType.INT:
                            dataItem = new IntTag(meta.ID, addr, this);
                            break;

                        case DataType.FLOAT:
                            dataItem = new FloatTag(meta.ID, addr, this);
                            break;

                        case DataType.STR:
                            dataItem = new StringTag(meta.ID, addr, this);
                            break;
                        }
                        if (dataItem != null)
                        {
                            //dataItem.Active = meta.Active;
                            _items.Add(dataItem);
                            _server.AddItemIndex(meta.Name, dataItem);
                        }
                    }
                }
            }
            _items.TrimExcess();
            _items.Sort((x, y) => x.Address.CompareTo(y.Address));
            UpdatePDUArea();
            return(true);
        }
Esempio n. 16
0
        public bool AddItems(IList <TagMetaData> items)
        {
            int count = items.Count;

            if (_items == null)
            {
                _items   = new List <ITag>(count);
                _mapping = new Dictionary <short, string>(count);
            }
            lock (_server.SyncRoot)
            {
                for (int i = 0; i < count; i++)
                {
                    ITag        dataItem = null;
                    TagMetaData meta     = items[i];
                    if (meta.GroupID == this._id)
                    {
                        DeviceAddress addr = new DeviceAddress {
                            Start = meta.ID, DataSize = meta.Size, VarType = meta.DataType
                        };
                        switch (meta.DataType)
                        {
                        case DataType.BOOL:
                            dataItem = new BoolTag(meta.ID, addr, this);
                            break;

                        case DataType.BYTE:
                            dataItem = new ByteTag(meta.ID, addr, this);
                            break;

                        case DataType.WORD:
                        case DataType.SHORT:
                            dataItem = new ShortTag(meta.ID, addr, this);
                            break;

                        case DataType.DWORD:
                        case DataType.INT:
                            dataItem = new IntTag(meta.ID, addr, this);
                            break;

                        case DataType.FLOAT:
                            dataItem = new FloatTag(meta.ID, addr, this);
                            break;

                        case DataType.STR:
                            dataItem = new StringTag(meta.ID, addr, this);
                            break;
                        }
                        if (dataItem != null)
                        {
                            //dataItem.Active = meta.Active;
                            _items.Add(dataItem);
                            _plcReader.TransactItem(meta.Name);
                            _mapping.Add(meta.ID, meta.Address);
                            _server.AddItemIndex(meta.Name, dataItem);
                        }
                    }
                }
                return(true);
            }
        }