Exemplo n.º 1
0
        public static async Task <IEnumerable <Function> > ListFunctionsAsync(AmsNetId target, CancellationToken cancel)
        {
            var functions = new List <Function>();

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));

                foreach (var key in RegistryDefinitions.Keys)
                {
                    try
                    {
                        var name   = RegistryDefinitions[key];
                        var regKey = @"SOFTWARE\WOW6432Node\Beckhoff\TwinCAT3 Functions\" + name + @"\Common";

                        var version = await Registry.QueryValueAsync(target, regKey, "Version");

                        functions.Add(new Function
                        {
                            Name    = name,
                            Version = System.Version.Parse(version),
                            Type    = key
                        });
                    }
                    catch { }
                }
            }

            return(functions);
        }
        public static ConnectedReadClient CreateAndConnect(AmsNetId id, int port, TimeSpan readCycle)
        {
            var connectedClient = new ConnectedReadClient(readCycle);

            connectedClient.client.Connect(id.ToString(), port);
            return(connectedClient);
        }
Exemplo n.º 3
0
 private void SetAcquisition(ScopeViewControlChannel channel, string Symbolname)
 {
     if (scopeViewControl2.Charts.Count == 0)
     {
         MessageBox.Show(this, "Please create a chart first!", "No chart connected!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     else if (scopeViewControl2.Charts[0].Axes.Count == 0)
     {
         MessageBox.Show(this, "Please create an YAxis first!", "No axis connected!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     else if (scopeViewControl2.Charts[0].Axes[0].Channels.Count == 0)
     {
         MessageBox.Show(this, "Please create a Channel first!", "No channel connected!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     else
     {
         // AmsNetId und AmsPort benötigen die TwinCAT.Ads.dll
         channel.Acquisition.AmsNetId      = AmsNetId.Parse("192.168.0.2.1.1");
         channel.Acquisition.TargetPort    = 851;
         channel.Acquisition.IsSymbolBased = true;
         channel.Acquisition.SymbolName    = Symbolname;
         channel.Acquisition.DataType      = DataTypeConverter.AdsToScope2Datatype(AdsDatatypeId.ADST_REAL64);
         channel.Acquisition.SampleTime    = (uint)(1 * TimeSpan.TicksPerMillisecond);
     }
 }
        /// <summary>
        /// Connects to TwinCAT Ads via TcAdsClient.Connect. Hooks up Ads events to logging text box
        /// </summary>
        /// <param name="amsNetId">As defined in TwinCAT Project (in Project > System > Routes > Project Routes). Something like 192.168.0.1.1.1 </param>
        /// <param name="port">As defined in TwinCAT. Normally 851 or 852</param>
        public void Connect(string amsNetId, int port)
        {
            try
            {
                if (TcClient == null)
                {
                    TcClient = new TcAdsClient();
                }

                TcClient.ConnectionStateChanged  += TcClient_ConnectionStateChanged;
                TcClient.AdsNotification         += TcClient_AdsNotification;
                TcClient.AdsNotificationError    += TcClient_AdsNotificationError;
                TcClient.AdsNotificationEx       += TcClient_AdsNotificationEx;
                TcClient.AdsStateChanged         += TcClient_AdsStateChanged;
                TcClient.AdsSymbolVersionChanged += TcClient_AdsSymbolVersionChanged;
                TcClient.AmsRouterNotification   += TcClient_AmsRouterNotification;

                AmsNetId id = new AmsNetId(amsNetId);
                TcClient.Connect(id, port);
            }
            catch (Exception e)
            {
                Send_TcClient_EventHandling(DateTime.Now, LogTextCategory.Error, string.Format("Could not connect to ADS Server: {0}", e));
            }
        }
Exemplo n.º 5
0
 public static void Restart(AmsNetId target)
 {
     using (AdsClient client = new AdsClient())
     {
         client.Connect(new AmsAddress(target, AmsPort.SystemService));
         client.WriteControl(new StateInfo(AdsState.Reset, 0));
     }
 }
Exemplo n.º 6
0
 public static async Task RestartAsync(AmsNetId target, CancellationToken cancel)
 {
     using (AdsClient client = new AdsClient())
     {
         client.Connect(new AmsAddress(target, AmsPort.SystemService));
         await client.WriteControlAsync(AdsState.Reset, 0, cancel);
     }
 }
Exemplo n.º 7
0
 public DeviceInfoViewModel(AmsNetId target)
 {
     Target = target;
     try
     {
         TargetName = AmsRouter.ListRoutes().Where(x => x.NetId == Target.ToString()).FirstOrDefault().Name;
     }
     catch { }
 }
Exemplo n.º 8
0
        public static void Reboot(AmsNetId target, TimeSpan delay)
        {
            var data   = BitConverter.GetBytes((int)delay.TotalSeconds);
            var buffer = new ReadOnlyMemory <byte>(data);

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));
                client.WriteControl(new StateInfo(AdsState.Shutdown, 1), buffer);
            }
        }
Exemplo n.º 9
0
        public static async Task RebootAsync(AmsNetId target, TimeSpan delay, CancellationToken cancel)
        {
            var data   = BitConverter.GetBytes((int)delay.TotalSeconds);
            var buffer = new ReadOnlyMemory <byte>(data);

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));
                await client.WriteControlAsync(AdsState.Shutdown, 1, buffer, cancel);
            }
        }
Exemplo n.º 10
0
        public DeviceInfoView(AmsNetId target)
        {
            InitializeComponent();

            this.Title = "Device Info Remote";

            viewModel   = new DeviceInfoViewModel(target);
            DataContext = viewModel;

            Loaded += OnLoaded;
        }
Exemplo n.º 11
0
        public static async Task SetLocalTimeAsync(AmsNetId target, DateTime time, CancellationToken cancel)
        {
            var buffer = new ReadOnlyMemory <byte>(BitConverter.GetBytes(time.Ticks));

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));
                var result = await client.WriteAsync(400, 1, buffer, cancel);

                result.ThrowOnError();
            }
        }
Exemplo n.º 12
0
        public static int GetSystemLatencyMaximum(AmsNetId target)
        {
            var buffer = new Memory <byte>(new byte[8]);

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.R0_Realtime));
                client.Read(0x01, 0x02, buffer);
            }

            return(BitConverter.ToInt32(buffer.ToArray(), 4));
        }
Exemplo n.º 13
0
        public static int GetCpuUsage(AmsNetId target)
        {
            var buffer = new Memory <byte>(new byte[4]);

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.R0_Realtime));
                client.Read(0x01, 0x06, buffer);
            }

            return(BitConverter.ToInt32(buffer.ToArray(), 0));
        }
Exemplo n.º 14
0
        public static async Task <int> GetSystemLatencyMaximumAsync(AmsNetId target, CancellationToken cancel)
        {
            var buffer = new Memory <byte>(new byte[8]);

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.R0_Realtime));
                var result = await client.ReadAsync(0x01, 0x02, buffer, cancel);

                result.ThrowOnError();
            }

            return(BitConverter.ToInt32(buffer.ToArray(), 4));
        }
Exemplo n.º 15
0
        public static async Task <DateTime> GetTimeAsync(AmsNetId target, CancellationToken cancel)
        {
            var buffer = new Memory <byte>(new byte[16]);

            using (AdsClient client = new AdsClient())
            {
                await client.ConnectAsync(new AmsAddress(target, AmsPort.SystemService), cancel);

                var result = await client.ReadAsync(400, 1, buffer, cancel);

                result.ThrowOnError();
            }

            return(DateTime.FromBinary(BitConverter.ToInt64(buffer.ToArray(), 0)));
        }
Exemplo n.º 16
0
        public static bool IsTargetReachable(AmsNetId target)
        {
            using (AdsClient client = new AdsClient())
            {
                try
                {
                    client.Connect(new AmsAddress(target, AmsPort.R0_Realtime));
                    client.ReadState();

                    return(true);
                }
                catch (Exception ex)
                {
                }
            }

            return(false);
        }
Exemplo n.º 17
0
        private string QueryHostname(AmsNetId amsNetId)
        {
            using (var client = new TcAdsClient(AdsClientSettings.Default))
            {
                client.Connect(amsNetId, AmsPort.SystemService);

                var stream = new AdsStream(256);
                var error  = client.TryRead(702 /*SYSTEMSERVICE_IPHOSTNAME*/, 0, stream, out int readBytes);
                if (error != AdsErrorCode.NoError)
                {
                    _logger.LogWarning("Error {error} query hostname for {amsNetId}.", error, amsNetId);
                    return(null);
                }

                var hostname = Encoding.GetEncoding(1252).GetString(stream.GetBuffer(), 0, readBytes - 1);
                return(hostname);
            }
        }
        public static async Task SetValueAsync(AmsNetId target, string subKey, string valueName, RegistryValueType type, IEnumerable <byte> data)
        {
            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));

                var writeBuffer = new List <byte>();

                writeBuffer.AddRange(System.Text.Encoding.UTF8.GetBytes(subKey));
                writeBuffer.Add(new byte()); // End delimiter
                writeBuffer.AddRange(System.Text.Encoding.UTF8.GetBytes(valueName));
                writeBuffer.Add(new byte());
                writeBuffer.AddRange(data);

                var result = await client.WriteAsync(200, 0, new ReadOnlyMemory <byte>(writeBuffer.ToArray()), CancellationToken.None);

                result.ThrowOnError();
            }
        }
Exemplo n.º 19
0
        public static void StartProcess(AmsNetId target, string path, string dir, string args)
        {
            byte[] Data = new byte[777];

            BitConverter.GetBytes(path.Length).CopyTo(Data, 0);
            BitConverter.GetBytes(dir.Length).CopyTo(Data, 4);
            BitConverter.GetBytes(args.Length).CopyTo(Data, 8);

            System.Text.Encoding.ASCII.GetBytes(path).CopyTo(Data, 12);
            System.Text.Encoding.ASCII.GetBytes(dir).CopyTo(Data, 12 + path.Length + 1);
            System.Text.Encoding.ASCII.GetBytes(args).CopyTo(Data, 12 + path.Length + 1 + dir.Length + 1);

            ReadOnlyMemory <byte> buffer = new ReadOnlyMemory <byte>(Data);

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));
                client.Write(500, 0, buffer);
                client.Dispose();
            }
        }
Exemplo n.º 20
0
        public static async Task StartProcessAsync(AmsNetId target, string path, string directory, string args, CancellationToken cancel)
        {
            byte[] Data = new byte[777];

            BitConverter.GetBytes(path.Length).CopyTo(Data, 0);
            BitConverter.GetBytes(directory.Length).CopyTo(Data, 4);
            BitConverter.GetBytes(args.Length).CopyTo(Data, 8);

            System.Text.Encoding.ASCII.GetBytes(path).CopyTo(Data, 12);
            System.Text.Encoding.ASCII.GetBytes(directory).CopyTo(Data, 12 + path.Length + 1);
            System.Text.Encoding.ASCII.GetBytes(args).CopyTo(Data, 12 + path.Length + 1 + directory.Length + 1);

            ReadOnlyMemory <byte> buffer = new ReadOnlyMemory <byte>(Data);

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));
                var res = await client.WriteAsync(500, 0, buffer, cancel);

                res.ThrowOnError();
            }
        }
Exemplo n.º 21
0
        private void button_Load_Click(object sender, EventArgs e)
        {
            string filename = "";

            openFileDialog1.FileName    = "";
            openFileDialog1.FilterIndex = 3;
            openFileDialog1.Filter      = "CSV Files (*.csv)|*.csv|Scope Files(*.sv2)|*.sv2|Scope Files(*.svd)|*.svd|Scope Files(*.tcscope)|*.tcscope|All files (*.*)|*.*";
            openFileDialog1.Title       = "Scope Files hunterladen";

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                filename = openFileDialog1.FileName;

                FileInfo finfo = new FileInfo(filename);
                if (!finfo.Exists)
                {
                    MessageBox.Show(this, "File not found! Please use the 'Add Chart' buttons to create a config.\r\nOnce a config is created and saved it can be load using the 'Load' -Button!", "File not found!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    // alte Konfiguration löschen
                    while (scopeViewControl2.Charts.Count > 0)
                    {
                        scopeViewControl2.DeleteChart(scopeViewControl2.Charts[0]);
                    }

                    // Konfiguration laden
                    scopeViewControl2.LoadScopeConfig(filename);

                    foreach (ScopeViewControlChannel channel in scopeViewControl2.ConnectedChannels)
                    {
                        channel.Acquisition.AmsNetId   = AmsNetId.Parse("192.168.0.2.1.1");
                        channel.Acquisition.TargetPort = 851;
                        //dataGridView1.Columns.Add(channel.Acquisition.SymbolName, channel.Acquisition.SymbolName);
                        //dataGridView1.Columns[channel.Acquisition.SymbolName].DefaultCellStyle.ForeColor = channel.Style.Color;
                    }
                }
            }
        }
        public static async Task <string> QueryValueAsync(AmsNetId target, string subKey, string valueName)
        {
            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));

                var readBuffer = new Memory <byte>(new byte[255]);

                var data = new List <byte>();

                data.AddRange(System.Text.Encoding.UTF8.GetBytes(subKey));
                data.Add(new byte()); // End delimiter
                data.AddRange(System.Text.Encoding.UTF8.GetBytes(valueName));
                data.Add(new byte());

                var writeBuffer = new ReadOnlyMemory <byte>(data.ToArray());

                var result = await client.ReadWriteAsync(200, 0, readBuffer, writeBuffer, CancellationToken.None);

                result.ThrowOnError();
                return(System.Text.Encoding.UTF8.GetString(readBuffer.ToArray(), 0, result.ReadBytes));
            }
        }
Exemplo n.º 23
0
        public static async Task <DeviceInfo> GetDeviceInfoAsync(AmsNetId target, CancellationToken cancel)
        {
            var buffer = new Memory <byte>(new byte[2048]);

            DeviceInfo device = new DeviceInfo();

            using (AdsClient client = new AdsClient())
            {
                client.Connect(new AmsAddress(target, AmsPort.SystemService));
                var result = await client.ReadAsync(700, 1, buffer, cancel);

                result.ThrowOnError();

                String data = System.Text.Encoding.ASCII.GetString(buffer.ToArray());

                device.TargetType       = GetValueFromTag("<TargetType>", data);
                device.HardwareModel    = GetValueFromTag("<Model>", data);
                device.HardwareSerialNo = GetValueFromTag("<SerialNo>", data);
                device.HardwareVersion  = GetValueFromTag("<CPUArchitecture>", data);
                device.HardwareDate     = GetValueFromTag("<Date>", data);
                device.HardwareCPU      = GetValueFromTag("<CPUVersion>", data);

                device.ImageDevice    = GetValueFromTag("<ImageDevice>", data);
                device.ImageVersion   = GetValueFromTag("<ImageVersion>", data);
                device.ImageLevel     = GetValueFromTag("<ImageLevel>", data);
                device.ImageOsName    = GetValueFromTag("<OsName>", data);
                device.ImageOsVersion = GetValueFromTag("<OsVersion>", data);

                var major = GetValueFromTag("<Version>", data);
                var minor = GetValueFromTag("<Revision>", data);
                var build = GetValueFromTag("<Build>", data);
                device.TwinCATVersion = Version.Parse(major + "." + minor + "." + build);
            }

            return(device);
        }
Exemplo n.º 24
0
Arquivo: PLC.cs Projeto: pkelm1/Choreo
 public AdsPlc(AmsNetId amsNetId, AmsPort amsPort)
 {
     adsSession = new AdsSession(amsNetId, (int)amsPort);
 }
Exemplo n.º 25
0
 public PLC(AmsNetId amsNetId)
 {
     Router = new AmsRouter(amsNetId);
     ctor_initialize();
 }
 public NetworkManager(string target)
 {
     Target = new AmsNetId(target);
     Initialize();
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="path"></param>
 /// <param name="amsNetId">server AmsNetId</param>
 /// <param name="port">Server port</param>
 public ReplayExtension(string path, AmsNetId amsNetId, int port)
 {
     ParseNetMonFile(path);
     _amsNetId = amsNetId;
     _port     = port;
 }
Exemplo n.º 28
0
 public bool TryAddRoute(RouteSetting route)
 {
     return(amsTcpIpRouter.TryAddRoute(new Route(route.Name, AmsNetId.Parse(route.AmsNetId), route.IpAddress)));
 }
 public static ConnectedWriteClient CreateAndConnect(AmsNetId id, int port, TimeSpan cycleTime)
 {
     var connectedClient = new ConnectedWriteClient(cycleTime);
     connectedClient.Client.Connect(id, port);
     return connectedClient;
 }
 public NetworkManager(AmsNetId target)
 {
     Target = target;
     Initialize();
 }