Control Your Way

C# Tutorial


1. GettingStarted
2. Building the Form
3. Adding Code
4. Test

1. Getting Started

The source code for this tutorial is available on GitHub at: https://github.com/controlyourway/CsharpTutorial.

For this tutorial we used Microsoft Visual Studio Express 2013 for Windows Desktop (but most recent versions of Visual Studio should work), which can be downloaded for free from the Microsoft website. Start Visual Studio Express and click on New Project. Select Windows Forms Application under Visual C# -> Windows. At the bottom, in the Name textbox enter: TutorialCsharp and select the location where you would like to store the project. Click OK to create a new Solution. Next we need to install the cywInterface library. Go to Tools -> NuGet Package Manager -> Package Manager Console. In the Package Manager Console type:
Install-Package cywInterface.dll

This should install our library and all the controls.

There are two controls in this library that we can add to our Toolbox. On the left hand side open the Toolbox and right click on the General section in the Toolbox window. Select Choose Items... in the popup menu. In the Choose Toolbox Items window, on the right hand side click the Browse... button and then browse to the TutorialCsharp folder and browse to packages -> cywInterface.dll.x.x.x -> lib -> netxx -> cywInterface.dll. There should be two new controls in the General section: CywControl and SerialPortControl:
New toolbox components

See our C# Library Documentation for a detailed explanation of these controls.

2. Building the Form

Add the following controls to the form and change their details as follows:
Form (Select the form)TextControl Your Way Example
Size420, 460
Location13, 13
UsernameYour email address
NetworkPasswordNetwork password on your account page
NetworkNamesnetwork 1
(or one or more networks on your account page)
LabelTextText to send:
Location12, 159
Size382, 20
Location12, 175
Size101, 28
Location13, 202
Size121, 28
Location273, 202
LabelTextText received:
Location12, 246
Size382, 60
Location12, 262
Size101, 28
Location13, 328
Location12, 371
Size382, 20
Location12, 387
At this point the form should look like this:
Form after adding components

3. Adding Code

Add the following statement to the top of the file:
Next add the following variables at the top of the form class: These will hold the names and session IDs of all your connected devices after a discovery.

The first event handlers we need to complete is for the cywControl. Select the cywControl on the form and then in the Properties window, click the Events icon. Double click each of the following events to create the event handlers:
  • ConnectionStatus
  • DataReceived
  • Error
Complete the code in the event handlers so that it looks as follows:

The next step is to add code to the events of all the buttons. Double click each of the following three buttons to create event handlers for them:
  • buttonSend
  • buttonSendDiscovery
  • buttonClear
Complete the three event handlers so their code looks as follows:

4. Test

The last step is to test the program. Run the program and then click on the Start button. After a short delay the Messages text box should say "Connection Successful" if your user details are correct. We need something to communicate with. We made a test page especially for this purpose. Go to our Test Page and enter your details in the User name and Network password text boxes and click the Start button. Enter "test123" into the Text to send textbox and click the Send button. The message should be displayed on the web page. Next click the Send data button on the web page and the message should be received in the test program.

If both the test program and the web page connected successfully but no data can be sent between them then the most common cause is that the network names don't match. If the devices are not on the same default networks then they cannot exchange data unless the data is specifically sent to the other one's network. The network names can be set after connection by clicking the Set Network Names button.

Click on the Send Discovery button. After a short delay the Messages textbox should display: "Number of devices discovered: 1". The names and session IDs will be stored in the two string lists. We hope this gives tutorial gave enough information to get you going using our C# library. For a detailed explanation of all the classes and controls please go to our C# Library Documentation