Template:PT3 CSHARP LNX DOTNET: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
 
Line 11: Line 11:
<div class="phd-panel phd-flex-column-align-left phd-flex-column-justify-top">
<div class="phd-panel phd-flex-column-align-left phd-flex-column-justify-top">
<p>Welcome to using Phidgets with C#! By using C#, you will have access to the complete Phidget22 API, including events.</p>
<p>Welcome to using Phidgets with C#! By using C#, you will have access to the complete Phidget22 API, including events.</p>
<p>The dotnet command-line interface is the quickest way to start programming in C# on Linux and MacOS.</p>
<p>The dotnet command-line interface is the quickest way to start programming in C#.</p>
</div>
</div>
</div>
</div>
Line 17: Line 17:


<div class="phd-slide">
<div class="phd-slide">
<h2>Requirements</h2>
<h2>Setting UDev Rules</h2>


<p>
<p>
First, make sure you have the Phidgets Drivers for Linux installed (see <span>[https://www.phidgets.com/docs/OS_-_Linux this page]</span> for installation
By default, Linux will not allow regular users to access physical hardware, as a security feature. This means that you must grant yourself root privileges via
instructions).
sudo to run Phidgets code.
</p>
</p>


<br />
<div class="phd-command-line"><code>sudo dotnet run</code></div>


<p>You also need our C development libraries in order to use dotnet. You can install them with:</p>
<p>
To avoid having to use sudo every time, you can set up a rules file to allow it automatically using the vendor code for Phidget USB devices. Create a text file
called <b>99-libphidget22.rules</b> containing the following line:
</p>
 
<div class="phd-command-line"><code>SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="06c2", ATTRS{idProduct}=="00[3-a][0-f]", MODE="666"</code></div>


<div class="phd-command-line"><code>apt-get install libphidget22-dev</code></div>
<p>
Mode 666 means every user has permission to read and write to the specified devices. Next, move this file to <b>/etc/udev/rules.d</b>. Now you can run code that
uses Phidgets as a regular user without sudo.
</p>
</div>
</div>


Line 40: Line 48:


<p>Usually, this will involve adding Microsoft packages to your repository and then installing dotnet via apt-get.</p>
<p>Usually, this will involve adding Microsoft packages to your repository and then installing dotnet via apt-get.</p>
<p>If you are a Visual Studio Code user, you can get the '.NET Install Tool' extension instead of installing manually.</p>
</div>
</div>



Latest revision as of 21:04, 17 December 2024

Linux with dotnet CLI

Welcome to using Phidgets with C#! By using C#, you will have access to the complete Phidget22 API, including events.

The dotnet command-line interface is the quickest way to start programming in C#.

Setting UDev Rules

By default, Linux will not allow regular users to access physical hardware, as a security feature. This means that you must grant yourself root privileges via sudo to run Phidgets code.

sudo dotnet run

To avoid having to use sudo every time, you can set up a rules file to allow it automatically using the vendor code for Phidget USB devices. Create a text file called 99-libphidget22.rules containing the following line:

SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="06c2", ATTRS{idProduct}=="00[3-a][0-f]", MODE="666"

Mode 666 means every user has permission to read and write to the specified devices. Next, move this file to /etc/udev/rules.d. Now you can run code that uses Phidgets as a regular user without sudo.

Installing dotnet CLI

For installation instructions, visit Microsoft's documentation and select your Linux distro.

Usually, this will involve adding Microsoft packages to your repository and then installing dotnet via apt-get.

If you are a Visual Studio Code user, you can get the '.NET Install Tool' extension instead of installing manually.

Setting up a New Project

Create a new C# project using the following command:

dotnet new console -o helloworld

This will create a new C# console application in a folder named helloworld in your current directory. Next, enter the folder and type:

dotnet run

If everything installed properly, the application should build and you'll see 'Hello, World!' printed in the terminal.

Getting Phidgets Sample Code

Next we'll use this base project to run some Phidgets sample code. Go to the product page for your Phidget and go to the Code Samples tab. Select C# from the language drop-down and use the check boxes to modify the example if you'd like.

Click the copy button in the corner of the code sample and paste it into the Program.cs for your HelloWorld program, overwriting the existing code.

Compile and Run

Once you've pasted the sample code into the .cs file and saved, you need to add the Phidgets .NET package to your project:

dotnet add package Phidget22.NET

Then you can re-build the project using

dotnet run

Success! The project is now using Phidgets.

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

«
»