Beispiel #1
0
        public AdsErrorCode TryGetSymbol(string symbolPath, bool bLookup, out ITcAdsSymbol symbol)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentOutOfRangeException("name");
            }
            symbol = null;
            SymbolInfoTable table = this;

            lock (table)
            {
                if (bLookup)
                {
                    TcAdsSymbol symbol2 = null;
                    if (this._symbolTable.TryGetValue(symbolPath, out symbol2))
                    {
                        symbol = symbol2;
                        return(AdsErrorCode.NoError);
                    }
                }
            }
            AdsErrorCode deviceSymbolNotFound = AdsErrorCode.DeviceSymbolNotFound;
            AdsStream    stream = new AdsStream(symbolPath.Length + 1);

            using (AdsBinaryWriter writer = new AdsBinaryWriter(stream))
            {
                writer.WritePlcAnsiString(symbolPath, symbolPath.Length + 1);
                AdsStream rdDataStream = new AdsStream(0xffff);
                int       readBytes    = 0;
                deviceSymbolNotFound = this._adsClient.TryReadWrite(0xf009, 0, rdDataStream, 0, (int)rdDataStream.Length, stream, 0, (int)stream.Length, out readBytes);
                if (deviceSymbolNotFound == AdsErrorCode.NoError)
                {
                    using (AdsBinaryReader reader = new AdsBinaryReader(rdDataStream))
                    {
                        AdsSymbolEntry symbolEntry = new AdsSymbolEntry(-1L, this._encoding, reader);
                        bool           flag2       = true;
                        flag2  = StringComparer.OrdinalIgnoreCase.Compare(symbolPath, symbolEntry.name) == 0;
                        symbol = new TcAdsSymbol(symbolEntry, (TcAdsDataType)this._datatypeTable.ResolveDataType(symbolEntry.type));
                        SymbolInfoTable table2 = this;
                        lock (table2)
                        {
                            this._symbolTable[symbolPath] = (TcAdsSymbol)symbol;
                            if (!flag2 && !this._symbolTable.ContainsKey(symbol.Name))
                            {
                                this._symbolTable[symbol.Name] = (TcAdsSymbol)symbol;
                                string message = $"InstancePath Ambiguity '{symbolPath}' and '{symbol.Name}'!";
                                TwinCAT.Ads.Module.Trace.TraceWarning(message);
                            }
                        }
                    }
                }
            }
            return(deviceSymbolNotFound);
        }
Beispiel #2
0
 private void button2_Click(object sender, EventArgs e)
 {
     try
     {
         Form1_Load(sender, e);
         AdsStream       adsStream = new AdsStream(30);
         AdsBinaryWriter writer    = new AdsBinaryWriter(adsStream);
         writer.WritePlcAnsiString(textBox1.Text, 30);
         adsClient.Write(varHandle, adsStream);
         adsClient.Dispose();
     }
     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
 }
Beispiel #3
0
 private void btnCalibMode_Click(object sender, EventArgs e)
 {
     modeStream = new AdsStream(30);
     adsWriter  = new AdsBinaryWriter(modeStream);
     strMode    = "Calibration";
     try
     {
         adsWriter.WritePlcAnsiString(strMode, 30);
         client.Write(hModeUI, modeStream);
     }
     catch (Exception err)
     {
         MessageBox.Show("Try to write Calibration to PLC: " + err.Message);
     }
     btnAutoMode.BackColor    = Color.Empty;
     btnCalibMode.BackColor   = Color.Green;
     btnManualMode.BackColor  = Color.Empty;
     btnSettingMode.BackColor = Color.Empty;
 }
 /// <summary>
 /// Internal util function that maps arbitrary object write actions to the corresponding AdsBinaryWriter write function
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="value"></param>
 private void WriteObjectToWriter(AdsBinaryWriter writer, string typeName, object value)
 {
     if (typeName == "bool")
     {
         writer.Write(bool.Parse(value.ToString()));
     }
     else if (typeName == "byte")
     {
         writer.Write((byte)value);
     }
     else if (typeName == "sint")
     {
         writer.Write(Int16.Parse(value.ToString()));
     }
     else if (typeName == "int")
     {
         writer.Write(Int32.Parse(value.ToString()));
     }
     else if (typeName == "dint")
     {
         writer.Write(Int64.Parse(value.ToString()));
     }
     else if (typeName == "usint")
     {
         writer.Write(UInt16.Parse(value.ToString()));
     }
     else if (typeName == "uint")
     {
         writer.Write(UInt32.Parse(value.ToString()));
     }
     else if (typeName == "udint")
     {
         writer.Write(UInt64.Parse(value.ToString()));
     }
     else if (typeName == "real")
     {
         writer.Write(float.Parse(value.ToString(), CultureInfo.InvariantCulture.NumberFormat));
     }
     else if (typeName == "lreal")
     {
         writer.Write(Double.Parse(value.ToString(), CultureInfo.InvariantCulture.NumberFormat));
     }
     else if (typeName == "time")
     {
         writer.WritePlcType(TimeSpan.Parse(value.ToString()));
     }
     else if (typeName == "date")
     {
         writer.WritePlcType(DateTime.Parse(value.ToString()));
     }
     else if (typeName.StartsWith("string"))
     {
         int length = typeName == "string" ? 80 : int.Parse(typeName.Substring(6));
         writer.WritePlcAnsiString(value.ToString(), length);
     }
     else
     {
         Send_TcClient_EventHandling(DateTime.Now, LogTextCategory.Error,
                                     string.Format("Can not write to AdsBinaryReader. Data type '{0}' not supported", typeName));
     }
 }
Beispiel #5
0
        internal void WritePrimitiveValue(string symbolPath, object value, Type managedType, AdsDatatypeId dataType, int byteSize, AdsBinaryWriter writer, int writerOffset)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentNullException("symbolPath");
            }
            if (managedType == null)
            {
                throw new ArgumentNullException("managedType");
            }
            if (byteSize < 0)
            {
                throw new ArgumentOutOfRangeException("byteSize");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writerOffset < 0)
            {
                throw new ArgumentOutOfRangeException("writerOffset");
            }
            Type type  = value.GetType();
            bool flag2 = PrimitiveTypeConverter.IsPlcOpenType(value.GetType());

            if (PrimitiveTypeConverter.IsPlcOpenType(managedType))
            {
                Type tp = null;
                if (PrimitiveTypeConverter.TryGetManagedType(dataType, out tp))
                {
                    managedType = tp;
                }
                else if (managedType == typeof(LTIME))
                {
                    managedType = typeof(ulong);
                    dataType    = AdsDatatypeId.ADST_UINT64;
                }
            }
            if (managedType != type)
            {
                try
                {
                    object obj1 = PrimitiveTypeConverter.Convert(value, managedType);
                    value = obj1;
                }
                catch (MarshalException exception)
                {
                    throw new ArgumentException($"Cannot convert value type '{value.GetType()}' to symbol type '{managedType}'!", "value", exception);
                }
            }
            writer.BaseStream.Position = writerOffset;
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT16:
                writer.Write((short)value);
                return;

            case AdsDatatypeId.ADST_INT32:
                writer.Write((int)value);
                return;

            case AdsDatatypeId.ADST_REAL32:
                writer.Write((float)value);
                return;

            case AdsDatatypeId.ADST_REAL64:
                writer.Write((double)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT8:
                writer.Write((sbyte)value);
                return;

            case AdsDatatypeId.ADST_UINT8:
                writer.Write((byte)value);
                return;

            case AdsDatatypeId.ADST_UINT16:
                writer.Write((ushort)value);
                return;

            case AdsDatatypeId.ADST_UINT32:
                writer.Write((uint)value);
                return;

            case AdsDatatypeId.ADST_INT64:
                writer.Write((long)value);
                return;

            case AdsDatatypeId.ADST_UINT64:
                writer.Write((ulong)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_STRING:
            {
                int  byteCount = Encoding.Default.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcAnsiString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_WSTRING:
            {
                int  byteCount = Encoding.Unicode.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcUnicodeString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_BIT:
                if ((bool)value)
                {
                    writer.Write((byte)1);
                    return;
                }
                writer.Write((byte)0);
                return;
            }
            throw new ArgumentException("Unexpected datatype. Cannot convert datatype of symbol to this type.", "type");
        }