| 
				     | 
				
| (10 intermediate revisions by 2 users not shown) | 
| Line 1: | 
Line 1: | 
 | {{NoTitle}}
  |  | #REDIRECT [[Language - C Sharp]]  | 
 | {{Language_-_CSharp_Dev_Environment_Table}}
  |  | 
 | {|
  |  | 
 | |style="vertical-align:middle; width: 60%;"|
  |  | 
 | <font size=6>'''Language - C#'''
  |  | 
 |    |  | 
 | '''Windows with Mono'''</font>
  |  | 
 |    |  | 
 | Welcome to using Phidgets with C#! By using C#, you will have access to the complete Phidget22 API, including events.
  |  | 
 |    |  | 
 | Mono is an open-source programming environment that aims to make Microsoft .NET applications available across all operating systems.
  |  | 
 | |{{TOC limit|2}}
  |  | 
 | |}
  |  | 
 |    |  | 
 | {{Language_-_C_Sharp_Intro|Windows|Visual Studio}}
  |  | 
 |    |  | 
 | ==Use Our Examples==
  |  | 
 | One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install [http://www.mono-project.com/download/ Mono] for Windows.
  |  | 
 |    |  | 
 |    |  | 
 | Now that you have Mono installed, download and unpack the HelloWorld example for C#:
  |  | 
 | *[{{SERVER}}/downloads/phidget22/examples/dotnet/csharp/Manager/Phidget22_HelloWorld_CSharp_Windows_Ex.zip HelloWorld example]
  |  | 
 | '''Note:''' The HelloWorld example is compatible with Mono because it does not use Windows Forms. All other C# examples use Windows Forms and thus cannot be used directly with Mono.
  |  | 
 |    |  | 
 |    |  | 
 | Next, copy Phidget22.NET.dll from type the following location:
  |  | 
 | *C:\Program Files\Phidgets\Phidget22\Phidget22.NET.dll 
  |  | 
 |    |  | 
 |    |  | 
 | Place both the HelloWorld example and the Phidget22.NET.dll file in the same location. Your folder should now look something like this:
  |  | 
 | [[File:Csharp_mono_folder.PNG|link=|center]]
  |  | 
 |    |  | 
 |    |  | 
 | Open the command prompt at the folder location and enter the following command:
  |  | 
 | <syntaxhighlight lang='bash'>
  |  | 
 | mcs /r:Phidget22.NET.dll Program.cs
  |  | 
 | </syntaxhighlight>
  |  | 
 |    |  | 
 | This will create an executable file called ''Program.exe''. Type in the following command to run the example:
  |  | 
 | <syntaxhighlight lang='bash'>
  |  | 
 | mono Program.exe
  |  | 
 | </syntaxhighlight>
  |  | 
 |    |  | 
 |    |  | 
 | [[File:Csharp_mono.PNG|link=|center]]
  |  | 
 |    |  | 
 |    |  | 
 | You should now have the example up and running. When you are ready, the next step is configuring your project and writing your own code!
  |  | 
 |    |  | 
 | ==Editing the Examples==
  |  | 
 |    |  | 
 | With the exception of the HelloWorld example mentioned earlier, the C# examples are what comprise the Windows [[Phidget Control Panel]]. These are all graphical examples, and Mono is a non-graphical environment so you'll need to modify a few things to adapt them for your own purposes. Start with copying the contents of '''Form1_Load''' from one of the examples into your mono program's main function.
  |  | 
 |    |  | 
 | Next, you can remove the following line:
  |  | 
 | <syntaxhighlight lang='CSharp'>
  |  | 
 | commandLineData phidgetParameters = open.parseCmdLine(); //get command line parameters
  |  | 
 | </syntaxhighlight>
  |  | 
 |    |  | 
 | Then, you can modify any line that mentions <code>phidgetParameters</code> by setting it to the desired value instead of using PhidgetParameters object.
  |  | 
 |    |  | 
 | For instance:
  |  | 
 | <syntaxhighlight lang='CSharp'>
  |  | 
 | try
  |  | 
 |             { //set all the values grabbed from command line.  these values have defaults that are set in ExampleUtils.cs, you can check there to see them.
  |  | 
 |                 digout.Channel = phidgetParameters.Channel; //selects the channel on the device to open
  |  | 
 |                 digout.DeviceSerialNumber = phidgetParameters.SerialNumber; //selects the device or hub to open
  |  | 
 |                 digout.HubPort = phidgetParameters.HubPort; //selects the port on the hub to open
  |  | 
 |                 digout.IsHubPortDevice = phidgetParameters.isHubPortDevice; //is the device a port on a VINT hub?
  |  | 
 |    |  | 
 |                 if (phidgetParameters.isRemote) //are we trying to open a remote device?
  |  | 
 |                 {
  |  | 
 |                     digout.IsRemote = true;
  |  | 
 |                     Net.EnableServerDiscovery(ServerType.Device); //turn on network scan
  |  | 
 |                     if (phidgetParameters.Password != null && phidgetParameters.ServerName != null)
  |  | 
 |                         Net.SetServerPassword(phidgetParameters.ServerName, phidgetParameters.Password); //set the password if there is one
  |  | 
 |                 }
  |  | 
 |                 else
  |  | 
 |                     digout.IsLocal = true;
  |  | 
 |    |  | 
 |                 digout.Open(); //open the device specified by the above parameters
  |  | 
 |             }
  |  | 
 |             catch (PhidgetException ex) { errorBox.addMessage("Error opening device: " + ex.Message); }
  |  | 
 | </syntaxhighlight>
  |  | 
 |    |  | 
 | Might become:
  |  | 
 |    |  | 
 | <syntaxhighlight lang='CSharp'>
  |  | 
 | try
  |  | 
 |             { 
  |  | 
 |                 digout.Channel = 0;
  |  | 
 |                 digout.DeviceSerialNumber = 370097;
  |  | 
 |                 digout.HubPort = 0; 
  |  | 
 |                 digout.IsHubPortDevice = true;
  |  | 
 |                 digout.IsRemote = false;    
  |  | 
 |                 digout.Open();
  |  | 
 |             }
  |  | 
 |             catch (PhidgetException ex) { errorBox.addMessage("Error opening device: " + ex.Message); }
  |  | 
 | </syntaxhighlight>
  |  | 
 |    |  | 
 | You'll also have to remove some references to graphical elements such as {{Code|ErrorEventBox}}. If you assign event handler functions, you'll have to define them before your main function, similar to the manager events in the HelloWorld example we covered earlier.
  |  | 
 |    |  | 
 | You can then manipulate the rest of the code as your application requires. A more in-depth description of programming with Phidgets will be covered in the next section.
  |  | 
 |    |  | 
 | {{Language_-_C_Sharp_Write_Code}}
  |  | 
 |    |  | 
 | ==Setting up a New Project==
  |  | 
 |    |  | 
 | When you are building a project from scratch, or adding Phidget functionality to an exisiting project, you'll need to configure your development environment to properly link the Phidget .NET library.
  |  | 
 |    |  | 
 | To include the Phidget .NET library, simply add the following lines to your code:
  |  | 
 | <syntaxhighlight lang='CSharp'>
  |  | 
 | using Phidget22;
  |  | 
 | using Phidget22.Events;
  |  | 
 | </syntaxhighlight>
  |  | 
 |    |  | 
 | Then, proceed to add your code to your main function.
  |  | 
 |    |  | 
 | The project now has access to Phidgets. You may want to revisit the [[#Write Code|Write Code]] section for a reminder of how to define and open a channel.
  |  | 
 |    |  | 
 | {{Language_Further_Reading}}
  |  |