/  11
 
Visual C# Tutorial 1Phidgets Inc.
1
Visual C# Tutorial 1
A Single RFID Reader in Visual C#
Overview 
In this tutorial we will cover:•Initial setup•Creating the Graphical User Interface•Writing code to operate the Graphical User Interface and control a Phidget RFID reader This tutorial provides a starting point for learning how to use the Phidget RFID Reader and develop afeel for the way all Phidgets work. This tutorial is targeted towards those who are new to C# Program-ming and creating Graphical User Interfaces. However, this tutorial assumes the reader possessesan understanding of basic programming concepts. Knowledge of object oriented programming is alsoassumed.The source code for this application is provided with the tutorial. It is suggested though that you follow
the steps outlined here to gain a rm grasp of how Visual C# works and the steps used to create the
application. More examples are available in the Downloads section of the web site.
Initial Setup
Please ensure that you have a working copy of Visual Studio (Visual Studio or Visual C# Express)installed on your system, and that it is up to date.
Important:
It is necessary to download and install the Phidget21 .NET API from the Phidgets web
site, Phidget21.msi. Without this le, it is not possible to use Phidgets.
Creating the Graphical User Interface (GUI)
The GUI is what you actually see when running your program. It allows you to interact with the codeand make changes that can affect your Phidget.
Create a New Project
To begin, open Visual Studio and create a new project. Select “Windows Application”, name the project‘RFIDReader’ and click “Ok”.
 
Visual C# Tutorial 1
 
Phidgets Inc.
2
GUI Layout
A program with a GUI is composed of one or more “forms”. The forms contain all of the ele-ments necessary to interact with the computer in a graphical environment. The form for thisprogram is the grey box on the screen. Mostprograms that you will be writing for Phidgets,at least to begin with, will only use one form.Applications that make use of more than oneare often substantially more complicated toprogram. The screen you see now is calledthe “Form Designer”.
It is useful to rst set up your workspace. If 
the “Toolbox” panel is not already visible, open the “View” menu ( 1 ) and select “Toolbox”. To ensurethe Toolbox panel does not cover the form you are going to be working on, toggle the “pin” button ( 2).The various elements of the GUI, known as controls, can be placed in whatever order you wish and in
any location you wish within the form. For this example, we will rst place the “Labels”( 3 ) which will beused to display information. Under the “All Windows Forms” rollout, select Label. Click somewhere in
the middle and upper region of the form. Repeat this action to create three labels. ‘
label1
’ will be usedto display the tag code whenever an RFID tag is brought into the scanning distance of the antenna.
label2
’ will be used to represent the connection status of the RFID reader, and ‘
label3
’ will be used todisplay the serial number of the connected Phidget. Note the names of your labels and the order theyappear on the form, they should follow the order shown here.When working with the labels and controls in general, it is often useful to open the layout toolbar by
selecting View ( 1 ) >> Toolbars >> Layout. The layout toolbar provides many helpful tools for aligningelements on the form. Select the three labels you created and perform a “Left Align” ( 4 ). If your labels
are spread out vertically you can also try clicking “Make Vertical Spacing Equal” ( 5 ). You can movethe labels as a group into whatever position you wish.Finally, select the ”CheckBox” tool ( 6 ) and as in the same manner as the labels, create a checkbox un-der the labels. Repeat this process to create a second checkbox under the previously created check-box. Select the two checkboxes and click “Center Horizontally” ( 7 ) on the layout toolbar.At this stage, experimentation is encouraged to create a satisfactory look.
123345676
 
Visual C# Tutorial 1Phidgets Inc.
3
Modify Properties
In this step, we’ll modify the labels attached to the two checkboxeswe created to display something a little bit more meaningful to our ex-ample. To do this we will have to modify those controls’ ‘Properties’.
To do this, rst click on
checkBox1
to select it and look to the bottomright hand side of the screen. The properties panel should be visiblewith the name of the selected control at the top of the panel. Here thename is “checkBox1”. We will be changing the following properties:1. Once that is completed, the same process can be repeated for 
check-Box2
. However this time, set the Text property to “LED”.
Text - Set this to “Antenna”. This checkbox is going to beused to display the status of the RFID Reader’s antenna.
Writing the Code
Visual C# and all visual programming languages are “event driven”, as such, any action you want theprogram to respond to calls a subroutine. As a general rule, every event requires it’s own block of code. There are three categories of action that this application must be designed to handle.1. Program Activities - These are events such as opening or closing the program.
2. User Activities - Such as clicking the Antenna or LED checkboxes.3.
Phidget Events - Phidget events include attaching or detaching the Phidget and handlingany errors that might occur.As is obvious, there are many different properties that can be set. A fairly good explanation can be
found by clicking on each and looking to the very bottom of the screen or by searching the help le that
comes with Visual Studio.

Share & Embed

More from this user

Add a Comment

Characters: ...