示例#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);
        }
示例#2
0
        public static object ToBaseType(this IWoopsaValue value, Type targetType)
        {
            switch (value.Type)
            {
            case WoopsaValueType.Null:
                if (targetType == typeof(void) || !targetType.IsValueType)
                {
                    return(null);
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Logical:
                if (targetType == typeof(bool))
                {
                    return(value.ToBool());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Integer:
                if (targetType == typeof(byte))
                {
                    return(value.ToByte());
                }
                else if (targetType == typeof(sbyte))
                {
                    return(value.ToSByte());
                }
                else if (targetType == typeof(Int16))
                {
                    return(value.ToInt16());
                }
                else if (targetType == typeof(UInt16))
                {
                    return(value.ToUInt16());
                }
                else if (targetType == typeof(Int32))
                {
                    return(value.ToInt32());
                }
                else if (targetType == typeof(UInt32))
                {
                    return(value.ToUInt32());
                }
                else if (targetType == typeof(Int64))
                {
                    return(value.ToInt64());
                }
                else if (targetType == typeof(UInt64))
                {
                    return(value.ToUInt64());
                }
                else if (targetType == typeof(float))
                {
                    return(value.ToFloat());
                }
                else if (targetType == typeof(double))
                {
                    return(value.ToDouble());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Real:
                if (targetType == typeof(float))
                {
                    return(value.ToFloat());
                }
                else if (targetType == typeof(double))
                {
                    return(value.ToDouble());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.DateTime:
                if (targetType == typeof(DateTime))
                {
                    return(value.ToDateTime());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.TimeSpan:
                if (targetType == typeof(TimeSpan))
                {
                    return(value.ToTimeSpan());
                }
                else
                {
                    break;
                }

            case WoopsaValueType.Text:
            case WoopsaValueType.WoopsaLink:
            case WoopsaValueType.JsonData:
            case WoopsaValueType.ResourceUrl:
                if (targetType == typeof(string))
                {
                    return(value.AsText);
                }
                else
                {
                    break;
                }

            default:
                break;
            }
            throw new InvalidCastException(
                      string.Format("Unable to cast WoopsaValue '{0}' to type '{1}'",
                                    value.Serialize(), targetType.FullName));
        }
示例#3
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);
        }