Inheritance: WoopsaProperty
Example #1
0
        public void WriteAdsValue(IWoopsaProperty property, IWoopsaValue value)
        {
            WoopsaAdsProperty adsProperty = (WoopsaAdsProperty)property;
            AdsStream         stream      = new AdsStream();

            switch (property.Type)
            {
            case WoopsaValueType.Integer:
                if (adsProperty.AdsInfo.Type == BeckhoffValueType.WORD ||
                    adsProperty.AdsInfo.Type == BeckhoffValueType.UINT)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToUInt16()));
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.DWORD ||
                         adsProperty.AdsInfo.Type == BeckhoffValueType.UDINT)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToUInt32()));
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.SINT)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToSByte()));
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.INT)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToInt16()));
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.DINT)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToInt32()));
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.LINT)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToInt64()));
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.USINT ||
                         adsProperty.AdsInfo.Type == BeckhoffValueType.BYTE)
                {
                    stream = new AdsStream(1);
                    stream.WriteByte(value.ToByte());
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.ULINT)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToUInt64()));
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Integer");
                }
                break;

            case WoopsaValueType.Logical:
                stream = new AdsStream(BitConverter.GetBytes(value.ToBool()));
                break;

            case WoopsaValueType.Real:
                if (adsProperty.AdsInfo.Type == BeckhoffValueType.REAL)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToFloat()));
                }
                else if (adsProperty.AdsInfo.Type == BeckhoffValueType.LREAL)
                {
                    stream = new AdsStream(BitConverter.GetBytes(value.ToDouble()));
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Real");
                }
                break;

            case WoopsaValueType.Text:
                stream = new AdsStream(80);
                byte[] byteString = System.Text.Encoding.ASCII.GetBytes(value.ToString());
                stream.Write(byteString, 0, byteString.Length);
                break;

            case WoopsaValueType.TimeSpan:
                stream = new AdsStream(BitConverter.GetBytes((uint)(value.ToTimeSpan().Ticks / TimeSpan.TicksPerMillisecond)));
                break;

            case WoopsaValueType.DateTime:
                TimeSpan timeSp;
                DateTime dateTime;
                if (adsProperty.AdsInfo.Type == BeckhoffValueType.TIME_OF_DAY)
                {
                    dateTime = value.ToDateTime();
                    uint timeOfDay = (uint)dateTime.Millisecond + ((uint)dateTime.Second + ((uint)dateTime.Minute + ((uint)dateTime.Hour * 60)) * 60) * 1000;
                    stream = new AdsStream(BitConverter.GetBytes(timeOfDay));
                }
                else
                {
                    timeSp = value.ToDateTime() - BeckhoffPlcReferenceDateTime;
                    stream = new AdsStream(BitConverter.GetBytes((uint)timeSp.TotalSeconds));
                }
                break;

            default:
                stream = new AdsStream(1);
                break;
            }
            _tcAds.Write(adsProperty.AdsInfo.IndexGroup, adsProperty.AdsInfo.IndexOffset, stream);
        }
Example #2
0
        public void loadHierarchy(WoopsaObject root)
        {
            try
            {
                _symbolLoader     = _tcAds.CreateSymbolInfoLoader();
                _woopsaObjects    = new Dictionary <string, WoopsaObject>();
                _woopsaProperties = new Dictionary <string, WoopsaAdsProperty>();

                foreach (TcAdsSymbolInfo symbol in _symbolLoader)
                {
                    WoopsaObject      newObject   = null;
                    WoopsaAdsProperty newProperty = null;
                    TcAdsSymbolInfo   parentInfo;
                    WoopsaValueType   propertyType;
                    string[]          path = symbol.Name.Split('.');
                    string            name = path[path.Length - 1];
                    bool isProperties      = BeckhoffToWoopsaValueType(symbol.Type, out propertyType);
                    if (symbol.Parent != null)
                    {
                        parentInfo = symbol.Parent;
                        if (_woopsaObjects.ContainsKey(parentInfo.Name))
                        {
                            if (isProperties)
                            {
                                newProperty = new WoopsaAdsProperty(_woopsaObjects[parentInfo.Name], name, propertyType,
                                                                    _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                            }
                            else
                            {
                                newObject = new WoopsaObject(_woopsaObjects[parentInfo.Name], name);
                            }
                        }
                        else
                        {
                            throw new Exception("Parent WoopsaObject not found !");
                        }
                    }
                    else
                    {
                        if (isProperties)
                        {
                            newProperty = new WoopsaAdsProperty(root, name, propertyType, _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                        }
                        else
                        {
                            newObject = new WoopsaObject(root, name);
                        }
                    }

                    if (isProperties)
                    {
                        _woopsaProperties.Add(symbol.Name, newProperty);
                    }
                    else
                    {
                        _woopsaObjects.Add(symbol.Name, newObject);
                    }
                }
            }catch (Exception e)
            {
                DiagnosticWindow.AddToDebug(e.Message);
            }
            isHierarchieLoaded = true;
        }
Example #3
0
        public WoopsaValue ReadAdsValue(IWoopsaProperty woopsaProperty)
        {
            WoopsaAdsProperty property = (WoopsaAdsProperty)woopsaProperty;
            AdsStream         stream   = new AdsStream(80); // for STRING(80)
            long data = 0;

            stream.Flush();
            try
            {
                _tcAds.Read(property.AdsInfo.IndexGroup, property.AdsInfo.IndexOffset, stream);
            }
            catch (Exception)
            {
                isAdsConnected = false;
                return(null);
            }
            switch (property.Type)
            {
            case WoopsaValueType.Integer:
                if (property.AdsInfo.Type == BeckhoffValueType.WORD ||
                    property.AdsInfo.Type == BeckhoffValueType.UINT)
                {
                    return(BitConverter.ToUInt16(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.DWORD ||
                         property.AdsInfo.Type == BeckhoffValueType.UDINT)
                {
                    return(BitConverter.ToUInt32(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.SINT)
                {
                    return(Convert.ToSByte((sbyte)stream.GetBuffer()[0]));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.INT)
                {
                    return(BitConverter.ToInt16(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.DINT)
                {
                    return(BitConverter.ToInt32(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.LINT || property.AdsInfo.Type == BeckhoffValueType.ULINT)
                {
                    return(BitConverter.ToInt64(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.USINT || property.AdsInfo.Type == BeckhoffValueType.BYTE)
                {
                    return((byte)stream.GetBuffer()[0]);
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Integer");
                }

            case WoopsaValueType.Logical:
                return(Convert.ToBoolean(stream.GetBuffer()[0]));

            case WoopsaValueType.Real:
                if (property.AdsInfo.Type == BeckhoffValueType.REAL)
                {
                    return(BitConverter.ToSingle(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.LREAL)
                {
                    return(BitConverter.ToDouble(stream.GetBuffer(), 0));
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Real");
                }

            case WoopsaValueType.Text:
                string s     = System.Text.Encoding.ASCII.GetString(stream.GetBuffer());
                int    index = s.IndexOf('\0');
                return(s.Remove(index, s.Length - index));

            case WoopsaValueType.TimeSpan:
                data = bufferToLong(stream.GetBuffer(), 4);
                TimeSpan timeSpan = new TimeSpan(TimeSpan.TicksPerMillisecond * data);
                return(timeSpan);

            case WoopsaValueType.DateTime:
                TimeSpan timeSp;
                DateTime dateTime;
                data     = bufferToLong(stream.GetBuffer(), 4);
                dateTime = BeckhoffPlcReferenceDateTime;
                if (property.AdsInfo.Type == BeckhoffValueType.TIME_OF_DAY)
                {
                    timeSp = TimeSpan.FromMilliseconds(data);
                }
                else
                {
                    timeSp = TimeSpan.FromSeconds(data);
                }
                dateTime = dateTime + timeSp;
                WoopsaValue value = new WoopsaValue(dateTime);
                return(value);

            default:
                return(null);
            }
        }
        public void loadHierarchy(WoopsaObject root)
        {
            try
            {
                _symbolLoader = _tcAds.CreateSymbolInfoLoader();
                _woopsaObjects = new Dictionary<string, WoopsaObject>();
                _woopsaProperties = new Dictionary<string, WoopsaAdsProperty>();

                foreach (TcAdsSymbolInfo symbol in _symbolLoader)
                {
                    WoopsaObject newObject = null;
                    WoopsaAdsProperty newProperty = null;
                    TcAdsSymbolInfo parentInfo;
                    WoopsaValueType propertyType;
                    string[] path = symbol.Name.Split('.');
                    string name = path[path.Length - 1];
                    bool isProperties = BeckhoffToWoopsaValueType(symbol.Type, out propertyType);
                    if (symbol.Parent != null)
                    {
                        parentInfo = symbol.Parent;
                        if (_woopsaObjects.ContainsKey(parentInfo.Name))
                        {
                            if (isProperties)
                            {
                                newProperty = new WoopsaAdsProperty(_woopsaObjects[parentInfo.Name], name, propertyType,
                                                                    _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                            }
                            else
                                newObject = new WoopsaObject(_woopsaObjects[parentInfo.Name], name);
                        }
                        else
                        {
                            throw new Exception("Parent WoopsaObject not found !");
                        }
                    }
                    else
                    {
                        if (isProperties)
                        {
                            newProperty = new WoopsaAdsProperty(root, name, propertyType, _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                        }
                        else
                            newObject = new WoopsaObject(root, name);
                    }

                    if (isProperties)
                        _woopsaProperties.Add(symbol.Name, newProperty);
                    else
                        _woopsaObjects.Add(symbol.Name, newObject);
                }
            }catch(Exception e)
            {
                DiagnosticWindow.AddToDebug(e.Message);
            }
            isHierarchieLoaded = true;
        }