B
BeeKinect
Guest
I'm working on a project with the ABB PC SDK for C#. I'm connecting to a robot controller in my network and want to display information about the robot and want to read the value of a variable. Currently I'm able to scan for the robot controllers and read the desired information by double clicking on one displayed robot controller. The problem is, that the data shown, aren't refreshed. So to refresh the information I have to click again on the controller. The goal now is to use the double click once to connect to the robot controller and then automatically refresh the data, for example every second or so. Is this possible using C# and if could you help me achieving that? Till now i found that this could be possible by using DataGridView, but since I'm totally new to C# I got a lot of problems implementing this functionality.
The code I'm using:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.RapidDomain;
using ABB.Robotics.Controllers.IOSystemDomain;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private NetworkScanner networkScanner = null;
private Controller ctrl = null;
private Controller controller = null;
private Task[] tasks = null;
private NetworkWatcher networkwatcher = null;
private Num rapidNum;
private double VarValue = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
networkScanner = new NetworkScanner();
networkScanner.Scan();
ControllerInfoCollection controllers = networkScanner.Controllers;
networkwatcher = new NetworkWatcher(networkScanner.Controllers);
networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
networkwatcher.EnableRaisingEvents = true;
foreach (ControllerInfo controller in controllers)
{
ListViewItem item = new ListViewItem(controller.IPAddress.ToString());
item.SubItems.Add(controller.Id);
item.SubItems.Add(controller.Availability.ToString());
item.SubItems.Add(controller.IsVirtual.ToString());
item.SubItems.Add(controller.SystemName);
item.SubItems.Add(controller.Version.ToString());
item.SubItems.Add(controller.ControllerName);
item.Tag = controller;
this.listControllersView.Items.Add(item);
}
}
private void Form1_Closed(object sender, FormClosedEventArgs e)
{
this.networkwatcher.Lost -= new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
this.networkwatcher.Found -= new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
}
private void ListControllersView_DoubleClick(object sender, EventArgs e)
{
ListViewItem itemView = listControllersView.SelectedItems[0];
if(itemView.Tag != null)
{
ControllerInfo controllerInf = (ControllerInfo)itemView.Tag;
if(controllerInf.Availability == ABB.Robotics.Controllers.Availability.Available)
{
if(this.controller != null)
{
this.controller.Logoff();
this.controller.Dispose();
this.controller = null;
}
this.controller = ControllerFactory.CreateFrom(controllerInf);
this.controller.Logon(UserInfo.DefaultUser);
RapidData rd = this.controller.Rapid.GetRapidData("T_ROB1", "RoboDK_Driver", "TESTVAR");
if (rd.Value is Num)
{
rapidNum = (Num)rd.Value;
VarValue = rapidNum.Value;
}
ListViewItem item = new ListViewItem(controller.RobotWare.ToString() + " " + controller.State.ToString() + " " + controller.OperatingMode.ToString() + " " + "VarValue" + " " + VarValue);
this.listOutput.Items.Add(item);
}
else
{
MessageBox.Show("Selected controller not available");
}
}
}
void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
{
this.Invoke(new
EventHandler<NetworkWatcherEventArgs>(AddControllerToListView),
new Object[] { this, e });
}
void HandleLostEvent(object sender, NetworkWatcherEventArgs e)
{
this.Invoke(new EventHandler<NetworkWatcherEventArgs>(RemoveControllerFromListView),
new Object[] { sender, e });
}
private void AddControllerToListView(object sender, NetworkWatcherEventArgs e)
{
ControllerInfo controllerInfo = e.Controller;
ListViewItem item = new ListViewItem(controllerInfo.IPAddress.ToString());
item.SubItems.Add(controllerInfo.Id);
item.SubItems.Add(controllerInfo.Availability.ToString());
item.SubItems.Add(controllerInfo.IsVirtual.ToString());
item.SubItems.Add(controllerInfo.SystemName);
item.SubItems.Add(controllerInfo.Version.ToString());
item.SubItems.Add(controllerInfo.ControllerName);
item.Tag = controllerInfo;
this.listControllersView.Items.Add(item);
}
private void RemoveControllerFromListView(object sender, NetworkWatcherEventArgs e)
{
foreach(ListViewItem item in this.listControllersView.Items)
{
if((ControllerInfo)item.Tag == e.Controller)
{
this.listControllersView.Items.Remove(item);
break;
}
}
}
}
}
Continue reading...
The code I'm using:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.RapidDomain;
using ABB.Robotics.Controllers.IOSystemDomain;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private NetworkScanner networkScanner = null;
private Controller ctrl = null;
private Controller controller = null;
private Task[] tasks = null;
private NetworkWatcher networkwatcher = null;
private Num rapidNum;
private double VarValue = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
networkScanner = new NetworkScanner();
networkScanner.Scan();
ControllerInfoCollection controllers = networkScanner.Controllers;
networkwatcher = new NetworkWatcher(networkScanner.Controllers);
networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
networkwatcher.EnableRaisingEvents = true;
foreach (ControllerInfo controller in controllers)
{
ListViewItem item = new ListViewItem(controller.IPAddress.ToString());
item.SubItems.Add(controller.Id);
item.SubItems.Add(controller.Availability.ToString());
item.SubItems.Add(controller.IsVirtual.ToString());
item.SubItems.Add(controller.SystemName);
item.SubItems.Add(controller.Version.ToString());
item.SubItems.Add(controller.ControllerName);
item.Tag = controller;
this.listControllersView.Items.Add(item);
}
}
private void Form1_Closed(object sender, FormClosedEventArgs e)
{
this.networkwatcher.Lost -= new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
this.networkwatcher.Found -= new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
}
private void ListControllersView_DoubleClick(object sender, EventArgs e)
{
ListViewItem itemView = listControllersView.SelectedItems[0];
if(itemView.Tag != null)
{
ControllerInfo controllerInf = (ControllerInfo)itemView.Tag;
if(controllerInf.Availability == ABB.Robotics.Controllers.Availability.Available)
{
if(this.controller != null)
{
this.controller.Logoff();
this.controller.Dispose();
this.controller = null;
}
this.controller = ControllerFactory.CreateFrom(controllerInf);
this.controller.Logon(UserInfo.DefaultUser);
RapidData rd = this.controller.Rapid.GetRapidData("T_ROB1", "RoboDK_Driver", "TESTVAR");
if (rd.Value is Num)
{
rapidNum = (Num)rd.Value;
VarValue = rapidNum.Value;
}
ListViewItem item = new ListViewItem(controller.RobotWare.ToString() + " " + controller.State.ToString() + " " + controller.OperatingMode.ToString() + " " + "VarValue" + " " + VarValue);
this.listOutput.Items.Add(item);
}
else
{
MessageBox.Show("Selected controller not available");
}
}
}
void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
{
this.Invoke(new
EventHandler<NetworkWatcherEventArgs>(AddControllerToListView),
new Object[] { this, e });
}
void HandleLostEvent(object sender, NetworkWatcherEventArgs e)
{
this.Invoke(new EventHandler<NetworkWatcherEventArgs>(RemoveControllerFromListView),
new Object[] { sender, e });
}
private void AddControllerToListView(object sender, NetworkWatcherEventArgs e)
{
ControllerInfo controllerInfo = e.Controller;
ListViewItem item = new ListViewItem(controllerInfo.IPAddress.ToString());
item.SubItems.Add(controllerInfo.Id);
item.SubItems.Add(controllerInfo.Availability.ToString());
item.SubItems.Add(controllerInfo.IsVirtual.ToString());
item.SubItems.Add(controllerInfo.SystemName);
item.SubItems.Add(controllerInfo.Version.ToString());
item.SubItems.Add(controllerInfo.ControllerName);
item.Tag = controllerInfo;
this.listControllersView.Items.Add(item);
}
private void RemoveControllerFromListView(object sender, NetworkWatcherEventArgs e)
{
foreach(ListViewItem item in this.listControllersView.Items)
{
if((ControllerInfo)item.Tag == e.Controller)
{
this.listControllersView.Items.Remove(item);
break;
}
}
}
}
}
Continue reading...