# full c# source code in chapter 5 of PC SDK manual

**URL:** <https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057>\
**Category:** Developer Tools\
**Created:** [August 30, 2012, 5:43am UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057 "2012-08-30T05:43:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![funwey](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@funwey](https://tech-community.robotics.abb.com/u/funwey)\
**Post date:** [August 30, 2012, 5:43am UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/1 "2012-08-30T05:43:44Z")

</div>

Hi;

I am new to C# but expert in c++. I just need the full source code of the project described in chapter 5 of the PC SDK application manual to get a kick start to learn. Any one can help ?

Rgds

Fun Wey

---

<div class="post-metadata">

**Author:** ![John\_Wiberg](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/john_wiberg/32/1485_2.png) [@John\_Wiberg](https://tech-community.robotics.abb.com/u/John_Wiberg)\
**Post date:** [August 30, 2012, 2:10pm UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/2 "2012-08-30T14:10:35Z")

</div>

Hi Fun Wey,

I’m guessing you are talking [about this link →](http://developercenter.robotstudio.com:80/Index.aspx?DevCenter=RobotCommunication&OpenDocument&Url=../RobotCommunicationAppManual/doc27.html) ???  
If so then the point of that exercise is to do it yourself. So ABB doesn’t have it anywhere online. So unless you can find someone who has already done the exercise and shares it then its faster and better to do it for yourself. It’s mostly copy & paste anyway so for a coding expert it shouldn’t be a biggie. 👍

But you have a different walkthrough on PC SDK over in [the Developer Center](http://developercenter.robotstudio.com) site with a download. Maybe that could help you get started as well?  
[Link to the walkthrough →](http://developercenter.robotstudio.com:80/Index.aspx?DevCenter=RobotCommunication&OpenDocument&Url=html/0cd79969-87c0-4b5e-9352-3a21e7a600b5.htm)

---

<div class="post-metadata">

**Author:** ![Luks](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Luks](https://tech-community.robotics.abb.com/u/Luks)\
**Post date:** [October 13, 2012, 12:04am UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/3 "2012-10-13T00:04:39Z")

</div>

This code should work:

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using ABB.Robotics;  
using ABB.Robotics.Controllers;  
using ABB.Robotics.Controllers.Discovery;  
using ABB.Robotics.Controllers.RapidDomain;

namespace Robot\_SDK  
{  
public partial class Form1 : Form  
{  
private NetworkScanner scanner = null;  
private Controller controller = null;  
private Task tasks = null;  
private NetworkWatcher networkwatcher = null;

public Form1()  
{  
InitializeComponent();  
}

private void Form1\_Load(object sender, EventArgs e)  
{  
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;  
}  
// Networkwatcher (detecteert als controllers worden aangesloten of worden verwijdert)  
this.networkwatcher = new NetworkWatcher(scanner.Controllers);  
this.networkwatcher.Found += new EventHandler(HandleFoundEvent);  
this.networkwatcher.Lost += new EventHandler(HandleLostEvent);  
this.networkwatcher.EnableRaisingEvents = true;  
}

private void Form1\_FormClosed(object sender, FormClosedEventArgs e)  
{  
this.networkwatcher.Lost -= new EventHandler(HandleLostEvent);  
this.networkwatcher.Found -= new EventHandler(HandleFoundEvent);  
}

void HandleFoundEvent(object sender, NetworkWatcherEventArgs e)  
{  
this.Invoke(new EventHandler(AddControllerToListView),  
new Object { this, e });  
}

void HandleLostEvent(object sender, NetworkWatcherEventArgs e)  
{  
this.Invoke(new EventHandler(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);  
this.listView1.Items.Add(item);  
item.Tag = controllerInfo;  
}

private void RemoveControllerFromListView(object sender, NetworkWatcherEventArgs e)  
{  
foreach (ListViewItem item in this.listView1.Items)  
{  
if ((ControllerInfo)item.Tag == e.Controller)  
{  
this.listView1.Items.Remove(item);  
break;  
}  
}  
}

private void listView1\_DoubleClick(object sender, EventArgs e)  
{  
ListViewItem listView1 = this.listView1.SelectedItems[0];  
if (listView1.Tag != null)  
{  
ControllerInfo controllerInfo = (ControllerInfo)listView1.Tag;  
if (controllerInfo.Availability == Availability.Available) [//columnHeader3.Available](https://columnHeader3.Available))  
{  
if (this.controller != null)  
{  
this.controller.Logoff(); this.controller.Dispose();  
this.controller = null;  
}  
this.controller = ControllerFactory.CreateFrom(controllerInfo);  
this.controller.Logon(UserInfo.DefaultUser);  
}  
else  
{  
MessageBox.Show(“Selected controller not available.”);  
}  
}  
}

private void button1\_Click(object sender, EventArgs e)  
{  
try  
{  
if (controller.OperatingMode == ControllerOperatingMode.Auto)  
{  
tasks = controller.Rapid.GetTasks();  
using (Mastership m = Mastership.Request(controller.Rapid))  
{  
//Perform operation  
tasks[0].Start();  
}  
}  
else  
{  
MessageBox.Show(“Automatic mode is required to start execution from a remote client.”);  
}  
}  
catch (System.InvalidOperationException ex)  
{  
MessageBox.Show(“Mastership is held by another client.” + ex.Message);  
}  
catch (System.Exception ex)  
{  
MessageBox.Show("Unexpected error occurred: " + ex.Message);  
}  
}  
}  
}

_Luks_

---

<div class="post-metadata">

**Author:** ![Lin](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/lin/32/2104_2.png) [@Lin](https://tech-community.robotics.abb.com/u/Lin)\
**Post date:** [October 15, 2012, 11:51am UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/4 "2012-10-15T11:51:30Z")

</div>

Hi Luks,

This code will work with Real Robot Controller.

Working with Virtual Controllers you can try this

if (\_currentController != null)  
{  
if (!\_currentController.IsVirtual)  
{  
\_currentController.Logoff();  
}  
\_currentController.Dispose();  
\_currentController = null;  
}

if (!\_currentController.IsVirtual)  
{  
if (\_currentController.CurrentUser != UserInfo.DefaultUser)  
{  
\_currentController.Logon(UserInfo.DefaultUser);  
}  
}

In the above code “\_currentController” is yours “this.controller”

Hope it will work.

---

<div class="post-metadata">

**Author:** ![jesus](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/jesus/32/3196_2.png) [@jesus](https://tech-community.robotics.abb.com/u/jesus)\
**Post date:** [October 18, 2012, 1:22am UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/5 "2012-10-18T01:22:55Z")

</div>

Hi all.

Several question, Lingaa.

Where do you incorporate this code that you have written in the code that Luks to writing?.  
I’m also starting as Funwei, and do not quite understand what you say.  
Luks code is for use with real controllers, right?.  
Adding your code to the Luks, fits the real and virtual controllers, right?.

Thanks in advance.

Jesus. :stuck\_out\_tongue\_winking\_eye:

---

<div class="post-metadata">

**Author:** ![John\_Wiberg](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/john_wiberg/32/1485_2.png) [@John\_Wiberg](https://tech-community.robotics.abb.com/u/John_Wiberg)\
**Post date:** [October 18, 2012, 5:18pm UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/6 "2012-10-18T17:18:48Z")

</div>

I did this as a template when working with supportcases:

PCSDK\_Csharp\_BasicScanApp\_514.zip

Its a Visual Studio 2010 C# project with 5.14.03 components which is like the one in the example. But it has some other logic in it.

I’ll make a 5.15 version as soon as the main 5.15 release topic is finished by Product Management.

---

<div class="post-metadata">

**Author:** ![jesus](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/jesus/32/3196_2.png) [@jesus](https://tech-community.robotics.abb.com/u/jesus)\
**Post date:** [October 18, 2012, 7:13pm UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/7 "2012-10-18T19:13:50Z")

</div>

Thanks John.

This will be a great help to start my project.

Jesus. 👏

---

<div class="post-metadata">

**Author:** ![Lin](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/lin/32/2104_2.png) [@Lin](https://tech-community.robotics.abb.com/u/Lin)\
**Post date:** [October 19, 2012, 9:52am UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/8 "2012-10-19T09:52:33Z")

</div>

Hi Jesus,  
Ya my code will consider both virtual and real controllers.When I was working with virtual controllers the code controller.Logoff() is giving exemption. So I am detecting the controller is virtual or real.  
I have not yet tried my code with real controllers.  
John code is working fine with virtual too.

---

<div class="post-metadata">

**Author:** ![jesus](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/jesus/32/3196_2.png) [@jesus](https://tech-community.robotics.abb.com/u/jesus)\
**Post date:** [October 19, 2012, 10:33am UTC](https://tech-community.robotics.abb.com/t/full-c-source-code-in-chapter-5-of-pc-sdk-manual/5057/9 "2012-10-19T10:33:29Z")

</div>

Thanks for your answer.

Jesus.
