PS前言
这可能是当前网络上能找到的最详细的ABB SDK上位机开发资料了:)
一、概述
动手来开始编程吧,先创建一个简单的应用程序,显示网络上所有的虚拟和真实控制器。然后就可以登录到控制器并开始执行RAPID程序.
二、建立项目
使用下列流程来创建PC SDK项目:
1、步骤操作
1 用Visual Studio创建一个 Windows应用项目.
2 在项目中添加PC SDK组件引用, ABB.Robotics.Controllers.PC.dll
、RobotStudio.Services.RobApi.Desktop.dll
、RobotStudio.Services.RobApi.dll
.这三个组件在安装目录中可找到,默认在C:\Program Files (x86)\ABB Industrial IT\Robotics IT\SDK\PCSDK 6.08
目录下.
需要安装PCSDK,请查考ABB机器人教程 (1)学习准备
3 打开Form1.cs
并在添加代码文件头部添加下列命名空间语句:VB:
Imports ABB.Robotics
Imports ABB.Robotics.Controllers
Imports ABB.Robotics.Controllers.Discovery
Imports ABB.Robotics.Controllers.RapidDomain
C#:
using ABB.Robotics;
using ABB.Robotics.Controllers;
using ABB.Robotics.Controllers.Discovery;
using ABB.Robotics.Controllers.RapidDomain;
4 在 解决方案资源管理器 中右击 Form1.cs并选择 查看设计器,根据下一章节说明创建可视化用户界面。
三、创建用户界面
下面的截图展示了我们将要创建的PC SDK应用。你可以看到网络中可扫描到的虚拟以及真实的控制器。.
1、创建用户应用界面:
步骤操作
1 修改form的Text 属性为Network scanning window
.
2 调整界面大小Size为 850*480.
3 添加一个 ListView
组件. 设置下面属性来得到与上图相似的布局:
- FullRowSelect - True
- GridLines - True
- View - Details
4 添加列IPAdress
, ID
, Availability
, Virtual
, System name
, RobotWare Version
和 Controller name
并调整其宽度.
5 在listview下方添加一个 Panel组件并放置一个Button按钮,设置按钮的Text
属性为Start RAOID Program
.
2、实现网络扫描
要找到所有控制器,我们首先在Form1代码里声明一些变量:VB:
Private scanner As NetworkScanner = Nothing
Private controller As Controller = Nothing
Private tasks As Task() = Nothing
Private networkWatcher As NetworkWatcher = Nothing
C#:
private NetworkScanner scanner = null;
private Controller controller = null;
private Task[] tasks = null;
private NetworkWatcher networkwatcher = null;
为了让扫描程序在启动应用时就开始执行, 我们将代码放在 Form1_Load 事件函数中, 代码如下:VB:
Me.scanner = New NetworkScanner Me.scanner.Scan()
Dim controllers As ControllerInfoCollection = Me.scanner.Controllers
Dim controllerInfo As ControllerInfo = Nothing
Dim item As ListViewItem
For Each controllerInfo In controllers
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)
Me.listView1.Items.Add(item)
item.Tag = controllerInfo Next
C#:
this.scanner = new NetworkScanner(); this.scanner.Scan();
ControllerInfoCollection controllers = scanner.Controllers; ListViewItem item = null;
foreach (ControllerInfo controllerInfo in controllers)
{
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);
this.listView1.Items.Add(item);
item.Tag = controllerInfo;
}
3、添加一个网络监控器
为了实现一个网络监控器,应用程序可以监控网络并能检测到控制器掉线或什么时候连接上。这个案例展示了如何进行网络监控编程以及如何将检测到的控制器添加到listview组件里。
添加一个 NetworkWatcher 对象到 FormLoad 事件函数里后, 我们在其Found
事件添加了一个订阅.VB:
Me.networkWatcher = New NetworkWatcher(Me.scanner.Controllers)
AddHandler Me.networkWatcher.Found, AddressOf Me.HandleFoundEvent
AddHandler Me.networkWatcher.Lost, AddressOf Me.HandleLostEvent
Me.networkWatcher.EnableRaisingEvents = True
C#:
this.networkwatcher = new NetworkWatcher(scanner.Controllers);
this.networkwatcher.Found += new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
this.networkwatcher.Lost += new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
this.networkwatcher.EnableRaisingEvents = true;
处理事件
后台线程接收事件后更新用户界面,因此必须使用invoke
方法。有关通过后台强制更新GUI线程的详细信息, 请看Invoke methodVB:
Private Sub HandleFoundEvent(ByVal sender As Object, ByVal e As NetworkWatcherEventArgs)
Me.Invoke(New EventHandler(Of NetworkWatcherEventArgs)(AddControllerToListView), New [Object]() {Me, e})
End Sub
C#:
void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)
{
this.Invoke(new
EventHandler<NetworkWatcherEventArgs>(AddControllerToListView), new Object[] { this, e });
}
这个事件更新用户界面:VB:
Private Sub AddControllerToListView(ByVal sender As Object, ByVal e As NetworkWatcherEventArgs)
Dim controllerInfo As ControllerInfo = e.Controller
Dim item As 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)
Me.listView1.Items.Add(item)
item.Tag = controllerInfo End Sub
C#:
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);
this.listView_robot.Items.Add(item);
item.Tag = controllerInfo;
}
【转载请注明出处: http://blog.****.net/leytton/article/details/90288472】
PS:如果本文对您有帮助,请点个赞让我知道哦~微笑
( ̄▽ ̄)/