ABB机器⼈教程(3)创建⼀个简单的PCSDK上位机应⽤
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,请查考
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:
MeworkWatcher =New NetworkWatcher(Me.scanner.Controllers)
AddHandler MeworkWatcher.Found,AddressOf Me.HandleFoundEvent
AddHandler MeworkWatcher.Lost,AddressOf Me.HandleLostEvent
MeworkWatcher.EnableRaisingEvents =True
C#:
thisworkwatcher =new NetworkWatcher(scanner.Controllers);
thisworkwatcher.Found +=new EventHandler<NetworkWatcherEventArgs>(HandleFoundEvent);
thisworkwatcher.Lost +=new EventHandler<NetworkWatcherEventArgs>(HandleLostEvent);
thisworkwatcher.EnableRaisingEvents =true;
处理事件
后台线程接收事件后更新⽤户界⾯,因此必须使⽤invoke⽅法。有关通过后台强制更新GUI线程的详细信息, 请看
VB:
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.Controllersdk
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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论