Language - C Sharp: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<metadesc>Communicate over USB with sensors, controllers and relays with Phidgets! Our C# library supports Windows/MacOS/Linux using Visual Studio or Mono.</metadesc>
{{#seo:|description=Learn how to use Phidget USB devices with C#.}}
[[Category:Language]]
[[Category:Language]]
__NOTOC__
We provide support for the C# language in all major operating systems. We also provide instructions on how to get your project started in a number of common development environments. Select your operating system and preferred development environment below, and follow the instructions to get your project running with Phidgets.


If you do not know which development environment you want to use, or your development environment of choice is not listed, we recommend starting with Mono as the simplest path to getting your code running. Visual Studio is the most popular way to build C# projects, but it also has a steep learning curve.
==Get Started==
With the Phidget22.NET library, it's easy to create C# applications that work with Phidget devices.


Once you have set up your development environment to run with Phidgets, we recommend you follow our guide on [[Phidget Programming Basics]]. The guide will showcase the fundamentals of programming with Phidgets, with examples in C#.
==C# Libraries==
===Nuget Package===
The Phidget22.NET library is available as a [https://www.nuget.org/packages/Phidget22.NET/ nuget package]. This is the recommend way to install and manage the Phidget22.NET library.


==Choose Your Development Environment:==
====macOS Considerations====
Phidget devices running a HID USB stack require the installation of the macOS libraries or the standalone Phidget Control Panel ([{{SERVER}}/docs/OS_-_macOS download here]).


{{Language_-_CSharp_Dev_Environment_Table}}
To determine what USB stack your Phidget device is on, navigate to the product page and then to the specification tab, and look for the ''USB Stack'' specification. If you are using a VINT device, navigate to the product page for the VINT Hub you are using.


== Quick Downloads ==
[[Image:Javascript_networkserver_webusb_spec.png|center|600px|link=https://cdn.phidgets.com/docs/images/8/80/Javascript_networkserver_webusb_spec.png]]
If you already know what you're doing and just need the files, you can find them all below.


=== Documentation ===
====Linux Considerations====
Linux restricts access to USB devices to the root user. To run your .NET application as a regular user, you'll need to [{{SERVER}}/docs/OS_-_Linux#Setting_Udev_Rules set up udev rules] on your system.


*{{Phidget22API}} (Select C# from drop-down menu)
===Library Files===
If you would like to manually manage the Phidget22.NET library files, you can download them [https://cdn.phidgets.com/downloads/phidget22/libraries/windows/Phidget22-windevel.zip here].


=== Example Code ===
==Development Environment Configuration==
===Visual Studio===
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed"
|+ '''Instructions'''
| Create your Visual Studio project, and navigate to the '''Solution Explorer'''. From there, right-click on '''Dependencies''' and select '''Manage NuGet Packages...'''|| [[Image:Language_csharp_vs_managenuget.png|center|350px|link=https://cdn.phidgets.com/docs/images/c/cc/Language_csharp_vs_managenuget.png]]
|-
| Next, click '''Browse''' and enter '''Phidget22.NET''' in the search bar. || [[Image:Language_csharp_vs_browsenuget.png|center|350px|link=https://cdn.phidgets.com/docs/images/6/61/Language_csharp_vs_browsenuget.png]]
|-
| Click on the package and press the '''Install''' button. || [[Image:Language_csharp_vs_installnuget.png|center|350px|link=https://cdn.phidgets.com/docs/images/b/b8/Language_csharp_vs_installnuget.png]]
|-
| Finally, add your C# code and run it using the button at the top of the screen. || [[Image:Language_csharp_vs_run.png|center|350px|link=https://cdn.phidgets.com/docs/images/5/5a/Language_csharp_vs_run.png]]
|-
|}


*{{SampleCode|CSharp|C# Examples}}
===Visual Studio Code===
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed"
|+ '''Instructions'''
| Install the [https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit C# Dev Kit extension for Visual Studio Code].|| [[Image:Language_csharp_vscode_installext.png|center|350px|link=https://cdn.phidgets.com/docs/images/b/b8/Language_csharp_vscode_installext.png]]
|-
| Next, create a directory and open it in Visual Studio Code. You can do this by opening Visual Studio Code and selecting '''File > Open Folder...''' || [[Image:Language_csharp_vscode_newfolder.png|center|350px|link=https://cdn.phidgets.com/docs/images/4/43/Language_csharp_vscode_newfolder.png]]
|-
| Open the Command Palette (Ctrl + Shift + P), type '''.NET: New Project''', and press Enter. A list of supported project types will appear. || [[Image:Language_csharp_vscode_netnewproject.png|center|350px|link=https://cdn.phidgets.com/docs/images/f/f7/Language_csharp_vscode_netnewproject.png]]
|-
| Select your preferred project type from the list. For this example, we will select '''Console App'''. || [[Image:Language_csharp_vscode_netconsoleapp.png|center|350px|link=https://cdn.phidgets.com/docs/images/b/bc/Language_csharp_vscode_netconsoleapp.png]]
|-
| Next, we need to add the Phidget22.NET library using NuGet. Open the Command Palette again (Ctrl + Shift + P), and enter '''NuGet: Add NuGet package...'''.
|| [[Image:Language_csharp_vscode_addnugetpackage.png|center|350px|link=https://cdn.phidgets.com/docs/images/2/26/Language_csharp_vscode_addnugetpackage.png]]
|-
| When prompted, enter '''Phidget22.NET''' and install the package.|| [[Image:Language_csharp_vscode_phidget22net.png|center|350px|link=https://cdn.phidgets.com/docs/images/d/d5/Language_csharp_vscode_phidget22net.png]]
|-
|-
| Finally, add your C# code and run it using the button at the top of the screen. || [[Image:Language_csharp_vscode_runcodepng.png|center|350px|link=https://cdn.phidgets.com/docs/images/d/dc/Language_csharp_vscode_runcodepng.png]]
|-
|}


===Libraries===
===.NET Command-Line Interface (CLI)===
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed"
|+ '''Instructions'''
| Download and install the latest version of.NET from [https://dotnet.microsoft.com/en-us/download/dotnet Microsoft's .NET download page].|| [[Image:Language_csharp_cli_download.png|center|350px|link=https://cdn.phidgets.com/docs/images/9/90/Language_csharp_cli_download.png]]
|-
| Next, open a command prompt or terminal in a new folder and enter the following command:


{{AllQuickDownloads}}
<syntaxhighlight lang=bash>
dotnet new console
</syntaxhighlight>
 
|| [[Image:Language_csharp_cli_newconsole.png|center|350px|link=https://cdn.phidgets.com/docs/images/3/3a/Language_csharp_cli_newconsole.png]]
|-
| This previous command will create a new console application at the folder location. || [[Image:Language_csharp_cli_newproject.png|center|350px|link=https://cdn.phidgets.com/docs/images/c/cb/Language_csharp_cli_newproject.png]]
|-
| Navigate back to the command prompt or terminal, and add the Phidget22.NET package with the following command:
 
<syntaxhighlight lang=bash>
dotnet add package Phidget22.NET
</syntaxhighlight>
 
|| [[Image:Language_csharp_cli_addpackage.png|center|350px|link=https://cdn.phidgets.com/docs/images/7/78/Language_csharp_cli_addpackage.png]]
|-
| You can now open the '''Program.cs''' file and add your code. Run the program by entering the following command into your command prompt or terminal:
 
<syntaxhighlight lang=bash>
dotnet run
</syntaxhighlight>
 
|| [[Image:Language_csharp_cli_runcode.png|center|350px|link=https://cdn.phidgets.com/docs/images/f/f3/Language_csharp_cli_runcode.png]]
|-
|}
 
==Example Code==
Navigate to our [https://www.phidgets.com/?view=code_samples&lang=CSharp Code Sample Generator] to view and download code samples that are tailored to your specific device.
 
[[Image:Language_csharp_codesample.png|center|600px|link=https://cdn.phidgets.com/docs/images/0/03/Language_csharp_codesample.png]]
 
===Phidget Programming Basics===
{{PhidgetProgrammingBasicsLink}}
 
==API==
[{{SERVER}}/?view=api&lang=CSharp Phidget22 API]

Latest revision as of 19:10, 11 June 2025


Get Started

With the Phidget22.NET library, it's easy to create C# applications that work with Phidget devices.

C# Libraries

Nuget Package

The Phidget22.NET library is available as a nuget package. This is the recommend way to install and manage the Phidget22.NET library.

macOS Considerations

Phidget devices running a HID USB stack require the installation of the macOS libraries or the standalone Phidget Control Panel (download here).

To determine what USB stack your Phidget device is on, navigate to the product page and then to the specification tab, and look for the USB Stack specification. If you are using a VINT device, navigate to the product page for the VINT Hub you are using.

Javascript networkserver webusb spec.png

Linux Considerations

Linux restricts access to USB devices to the root user. To run your .NET application as a regular user, you'll need to set up udev rules on your system.

Library Files

If you would like to manually manage the Phidget22.NET library files, you can download them here.

Development Environment Configuration

Visual Studio

Instructions
Create your Visual Studio project, and navigate to the Solution Explorer. From there, right-click on Dependencies and select Manage NuGet Packages...
Language csharp vs managenuget.png
Next, click Browse and enter Phidget22.NET in the search bar.
Language csharp vs browsenuget.png
Click on the package and press the Install button.
Language csharp vs installnuget.png
Finally, add your C# code and run it using the button at the top of the screen.
Language csharp vs run.png

Visual Studio Code

Instructions
Install the C# Dev Kit extension for Visual Studio Code.
Language csharp vscode installext.png
Next, create a directory and open it in Visual Studio Code. You can do this by opening Visual Studio Code and selecting File > Open Folder...
Language csharp vscode newfolder.png
Open the Command Palette (Ctrl + Shift + P), type .NET: New Project, and press Enter. A list of supported project types will appear.
Language csharp vscode netnewproject.png
Select your preferred project type from the list. For this example, we will select Console App.
Language csharp vscode netconsoleapp.png
Next, we need to add the Phidget22.NET library using NuGet. Open the Command Palette again (Ctrl + Shift + P), and enter NuGet: Add NuGet package....
Language csharp vscode addnugetpackage.png
When prompted, enter Phidget22.NET and install the package.
Language csharp vscode phidget22net.png
Finally, add your C# code and run it using the button at the top of the screen.
Language csharp vscode runcodepng.png

.NET Command-Line Interface (CLI)

Instructions
Download and install the latest version of.NET from Microsoft's .NET download page.
Language csharp cli download.png
Next, open a command prompt or terminal in a new folder and enter the following command:
dotnet new console
Language csharp cli newconsole.png
This previous command will create a new console application at the folder location.
Language csharp cli newproject.png
Navigate back to the command prompt or terminal, and add the Phidget22.NET package with the following command:
dotnet add package Phidget22.NET
Language csharp cli addpackage.png
You can now open the Program.cs file and add your code. Run the program by entering the following command into your command prompt or terminal:
dotnet run
Language csharp cli runcode.png

Example Code

Navigate to our Code Sample Generator to view and download code samples that are tailored to your specific device.

Language csharp codesample.png

Phidget Programming Basics

To learn more about the structure of the example code, visit our Phidget Programming Basics guide.

API

Phidget22 API