Template:Language - C Sharp Write Code: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
Line 1: Line 1:
==Write Code==
{{WriteCode_Intro|C#|CSharp}}
{{WriteCode_Intro|C#|CSharp}}
==== Step One: Initialize and Open ====
You will need to declare your Phidget object in your code. For example, we can declare a digital input object like this:
<syntaxhighlight lang=csharp>
DigitalInput ch;
</syntaxhighlight>
Next, the Phidget object needs to be initialized and opened:
<syntaxhighlight lang=cSharp>
ch = new DigitalInput();
ch.Open();
</syntaxhighlight>
Although we are not including it on this page, you should include error handling for all Phidget functions. Here is an example of the previous code with error handling:
<syntaxhighlight lang=cSharp>
try{
  ch = new DigitalInput();
  ch.Open();
}catch(PhidgetException ex){
  Console.WriteLine("Error: " + ex.Description);
}
</syntaxhighlight>
==== Step Two: Wait for Attachment of the Phidget ====
Simply calling open does not guarantee you can use the Phidget immediately. To use a Phidget, it must be plugged in (attached). We can handle this by using event driven programming and tracking the attach events. Alternatively, we can modify our code so we wait for an attachment:
<syntaxhighlight lang=CSharp>
ch = new DigitalInput();
ch.Open(5000); //wait for attach for 5 seconds, if not time out
</syntaxhighlight>
Waiting for attachment will block indefinitely until a connection is made, or until the timeout value is exceeded.
To use events, we have to modify our code slightly:
<syntaxhighlight lang=CSharp>
ch = new DigitalInput();
ch.Attach += onAttachHandler;
ch.Open();
</syntaxhighlight>
Next, we have to declare the function that will be called when an attach event is fired - in this case the function ''onAttachHandler'' will be called.
<syntaxhighlight lang=CSharp>
void onAttachHandler (object sender, Phidget22.Events.AttachEventArgs e){
  attachedText.Text = "Attached";
}
</syntaxhighlight>
==== Step Three: Do Things with the Phidget ====
We recommend the use of event driven programming when working with Phidgets. In a similar way to handling an attach event as described above, we can also add an event handler for a state change event:
<syntaxhighlight lang=csharp>
ch = new DigitalInput();
ch.Attach += onAttachHandler;
ch.StateChange += onStateChangeHandler;
ch.Open();
</syntaxhighlight>
This code will connect a function and an event. In this case, the ''onStateChangeHandler'' function will be called when there has been a change to the devices input. Next, we need to create the ''onStateChangeHandler'' function:
<syntaxhighlight lang=cSharp>
void onStateChangeHandler(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e) {
    stateText.Text = "State: " + e.State;
}
</syntaxhighlight>
If events do not suit your needs, you can also poll the device directly for data using code like this:
<syntaxhighlight lang=cSharp>
stateText.Text = "State: " + ch.State;
</syntaxhighlight>
==== Step Four: Close and Delete ====
At the end of your program, be sure to close your device.
<syntaxhighlight lang=cSharp>
ch.Close()
</syntaxhighlight>

Revision as of 16:52, 28 June 2018

Write Code

By following the instructions for your operating system and compiler above, you now have working examples and a project that is configured. This teaching section will help you understand how the examples were written so you can start writing your own code.


Remember: your main reference for writing C# code will be the Phidget22 API Manual and the example code.

Step One: Initialize and Open

You will need to declare your Phidget object in your code. For example, we can declare a digital input object like this:

DigitalInput ch;


Next, the Phidget object needs to be initialized and opened:

ch = new DigitalInput();
ch.Open();


Although we are not including it on this page, you should include error handling for all Phidget functions. Here is an example of the previous code with error handling:

try{
  ch = new DigitalInput();
  ch.Open();
}catch(PhidgetException ex){
  Console.WriteLine("Error: " + ex.Description);
}

Step Two: Wait for Attachment of the Phidget

Simply calling open does not guarantee you can use the Phidget immediately. To use a Phidget, it must be plugged in (attached). We can handle this by using event driven programming and tracking the attach events. Alternatively, we can modify our code so we wait for an attachment:

ch = new DigitalInput();
ch.Open(5000); //wait for attach for 5 seconds, if not time out

Waiting for attachment will block indefinitely until a connection is made, or until the timeout value is exceeded.


To use events, we have to modify our code slightly:

ch = new DigitalInput();
ch.Attach += onAttachHandler;
ch.Open();

Next, we have to declare the function that will be called when an attach event is fired - in this case the function onAttachHandler will be called.

void onAttachHandler (object sender, Phidget22.Events.AttachEventArgs e){
  attachedText.Text = "Attached";
}

Step Three: Do Things with the Phidget

We recommend the use of event driven programming when working with Phidgets. In a similar way to handling an attach event as described above, we can also add an event handler for a state change event:

ch = new DigitalInput();
ch.Attach += onAttachHandler;
ch.StateChange += onStateChangeHandler;
ch.Open();

This code will connect a function and an event. In this case, the onStateChangeHandler function will be called when there has been a change to the devices input. Next, we need to create the onStateChangeHandler function:

void onStateChangeHandler(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e) {
    stateText.Text = "State: " + e.State;
}


If events do not suit your needs, you can also poll the device directly for data using code like this:

stateText.Text = "State: " + ch.State;

Step Four: Close and Delete

At the end of your program, be sure to close your device.

ch.Close()