|
|
Line 14: |
Line 14: |
| {{Language_-_C_Sharp_Intro|Linux|Mono}} | | {{Language_-_C_Sharp_Intro|Linux|Mono}} |
|
| |
|
| ==Use Our Examples==
| | *You will also need to download and install Mono, if you haven't already. You can do this by entering the following command in the terminal: |
| 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 Mono. You can do this by entering the following command in the terminal:
| |
|
| |
|
| <syntaxhighlight lang='bash'> | | <syntaxhighlight lang='bash'> |
Line 21: |
Line 20: |
| </syntaxhighlight> | | </syntaxhighlight> |
|
| |
|
| You will also need a copy of [{{SERVER}}/downloads/phidget22/libraries/windows/Phidget22-windevel.zip Phidget22.NET.dll].
| | *Lastly, you will need a copy of [{{SERVER}}/downloads/phidget22/libraries/windows/Phidget22-windevel.zip Phidget22.NET.dll]. |
|
| |
|
| | {{Finding Code Samples|C#|CSharp}} |
|
| |
|
| Now that you have Mono installed and Phidget22.NET.dll on hand, download and unpack the HelloWorld example for C#:
| | ==Using the Code Samples== |
| *[{{SERVER}}/downloads/phidget22/examples/dotnet/csharp/Manager/Phidget22_HelloWorld_CSharp_Windows_Ex.zip HelloWorld example download]
| | To use the code sample from the [{{server}}/?view=code_samples&lang=CSharp Code Samples] page, you can click the '''Download Example''' button to download a CS file with the sample code. |
| Note: The HelloWorld example is compatible with Mono because it does not use Windows Forms. All other C# examples use Windows Forms.
| |
|
| |
|
| | [[Image:CSharp_Sample_Code_Download.png|link=|center|800px]] |
|
| |
|
| Place the example and Phidget22.NET.dll in the same folder. Your project folder should now look like this:
| |
|
| |
|
| [[Image:Csharp_linux_folder.PNG|link=|center]]
| | Once you have the example, you can compile and run your code: |
| | |
| | |
| Finally, to compile the program, enter the following command in the terminal:
| |
| <syntaxhighlight lang='bash'>
| |
| mcs Program.cs -r:Phidget22.NET.dll
| |
| </syntaxhighlight>
| |
| | |
| An executable file will be created. Run the program using mono with the following command:
| |
| | |
| <syntaxhighlight lang='bash'>
| |
| mono Program.exe
| |
| </syntaxhighlight>
| |
| | |
| | |
| [[Image:Csharp_linux_mono_run.PNG|link=|center|850px]]
| |
| | |
| | |
| You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, you can edit the examples and add 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.
| |
|
| |
|
| ==Setting up a New Project== | | ==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.
| | Whether you are running our examples or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget .NET library. |
| | |
| To include the Phidget22.NET library, simply add the following lines to your code:
| |
| <syntaxhighlight lang='CSharp'>
| |
| using Phidget22;
| |
| using Phidget22.Events;
| |
| </syntaxhighlight>
| |
|
| |
|
| Place a copy of Phidget22.NET.dll in the same folder as your program. Your project folder should now look like this:
| | The easiest way to allow Mono to access the Phidgets .NET library is to place a copy of [{{SERVER}}/downloads/phidget22/libraries/windows/Phidget22-windevel.zip Phidget22.NET.dll] in the same folder as your program. Your project folder should now look like this: |
|
| |
|
| [[Image:Csharp_linux_folder.PNG|link=|center]] | | [[Image:Csharp_linux_folder.PNG|link=|center]] |
|
| |
|
| Finally, to compile the program, enter the following command in the terminal:
| | ==Compile and Run== |
| | Once you are ready to run your program, open the ''Terminal'' and navigate to your project folder. Next, enter the following command: |
| <syntaxhighlight lang='bash'> | | <syntaxhighlight lang='bash'> |
| mcs Program.cs -r:Phidget22.NET.dll | | mcs Program.cs -r:Phidget22.NET.dll |
Line 129: |
Line 51: |
| </syntaxhighlight> | | </syntaxhighlight> |
|
| |
|
| Success! The project now has access to Phidgets. | | Success! The project is now using Phidgets. |
|
| |
|
| {{Language Page What's Next}} | | {{Language Page What's Next}} |
Language - C#
Linux with Mono
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.
|
|
Install Phidget Drivers for Linux
Before getting started with the guides below, ensure you have the following components installed on your machine:
- You will need the Phidgets Linux Drivers
- You will also need to download and install Mono, if you haven't already. You can do this by entering the following command in the terminal:
apt-get install mono-complete
Finding Code Samples
To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.
Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.
Using the Code Samples
To use the code sample from the Code Samples page, you can click the Download Example button to download a CS file with the sample code.
Once you have the example, you can compile and run your code:
Setting up a New Project
Whether you are running our examples or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget .NET library.
The easiest way to allow Mono to access the Phidgets .NET library is to place a copy of Phidget22.NET.dll in the same folder as your program. Your project folder should now look like this:
Compile and Run
Once you are ready to run your program, open the Terminal and navigate to your project folder. Next, enter the following command:
mcs Program.cs -r:Phidget22.NET.dll
An executable file will be created. Run the program using mono with the following command:
Success! The project is now using Phidgets.
What's Next?
Now that you have set up Phidgets to work with your programming environment, we recommend you read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.