This guide will be using Visual Studio in visual C#. It will be coding for 2 ED input lines which are named "weekly counter" and "daily counter". You can name them whatever you like, these lines are not connected in any way. Each ED input line has its own counter so you can code for as many ED lines as you wish. This guide only shows how to do 2 but the principle and the coding is the exact same, just add more buttons/lines of code for however many input lines you wish to use the counter for.

I will be going through everything in visual studio and the coding step by step so no knowledge of C# or visual studio is needed.

This guide only goes through the software element to counting boxes with an ED device. It does not go through the hardware element of connecting a sensor, setting jumpers etc.

Step 1: You Will Need

You will need a Brainboxes Remote IO device that has digital input lines such as:

  • ED-008
  • ED-004
  • ED-204
  • ED-038
  • ED-588
  • ED-516
  • ED-504
  • ED-538

You will also need to get Visual studio. You can get this from the visual studio website.

Step 2: Making a New Project

When you have opened up visual studio you will need to make a new project File>New project. On the drop down list for templates you will see a section called "Visual C#", click on Windows Forms application and name the project. When naming a project it is best to use no spaces and a capital letter for the next word instead. I have called mine "EdCounter".

When you press "OK" you will be on the page where you will be designing the form. To get to the code you will need to look in your solution explorer on the right hand side of your screen and click on the arrow next to form1.cs then double click on form 1.

Step 3: Adding the Brainboxes.IO API

Before we write any code we want to get the Brainboxes.IO API to make coding easier. Go to tools, NuGet package manager then manage packages for solution.

Click on "Online" on the left hand side and type in the search bar "Brainboxes.IO" then install it. Now our project has the Brainboxes.IO API installed however, we haven't told it to use the API. To do this we must add to the "using" block of text that is at the top of our code. Underneath the last line of the "using" block add "using Brainboxes.IO;".

Step 4: Designing the Form

Click on the Form1.cs[Design] tab to open the form designer. On the left hand side there is a bar called "toolbox", click on this if it isn't already open to open the toolbox side bar. Click the drop down arrow next to "all windows forms" to open the menu add objects to your form. You can also click the pin icon next to the x icon to pin the toolbox bar so it doesn't overlap your form designer.

This will be counting for daily and weekly so we are going to add 2 buttons and 4 labels. The 2 buttons are going to be reset buttons, 2 labels will be labels for the daily and weekly count and 2 labels will be displaying the counter.

Step 5: Labelling the Form

Now we need to label our objects on our form so we know what we are referring to when we are wiring our code. By the solution explorer there should be a tab called properties, click on it and pin it so it stays under the solution explorer.

Click on the top left label, this is the one we are going to start with. The only properties we are concerned with is the "Text" property and the "(Name)" property. The "Text" property is what is shown in the label or on the button. The "(Name)" property is what we will be referring to in our code. Change the "Text" property of the top left label to "Daily count".

Now we need to change the "(Name)" property to something that tells us what it is and what it is used for. As we will be using this in our code to refer to this label we use camel case. We will change the "(Name)" property to "dailyCountText" as this will just be standard text. This label doesn't need the "(Name)" property changing as we will not be referring to it in our code however it is good practice to label all of your objects in your form.

The top middle label will we displaying our daily counter number. Change the "Text" property to 0 so we start up our program the counter will be displayed as 0 until the counter has changed. Then change the "(Name)" property to "dailyCountLabel".

The buttons will be used to allow the user to reset the counter. Change the "Text" property of the top button to "Reset". Then change the "(Name)" property of the top button to "Reset". Then change the "(Name)" property to "dailyResetButton".

You will now need to follow these steps again for the next line of labels and the other button. This will be for the weekly count so instead just change where we have put "daily" to "weekly".

Step 6: Starting to Code

We will now start writing our code, go back to the Form1.cs page. First we need to make sure that our program will be able to use our ED device. We do this by adding the ED device before the "public Form1()" line. Underneath the parentheses under "public partial class" write "EDDevice ed;". This will allow us to reference the ED device as "ed" in our code.

Now, we need to connect our ED device. You will need to know your ED device's IP address, you can find this out through Boost.IO. Underneath "InitializeComponent();" you will need to write "try" then open parentheses {. Press enter once then in-between the parentheses write "ed = new ED588(or whatever ED device you have)(new TCPConnection("Your ED's IP address"));" Underneath that line you need to write "ed.Connect();".

Underneath this we are going to write the code for an error to come up when the ED device cannot connect. Under the 2nd parentheses under try write "catch(Exception e)" then open parentheses. Within the parentheses write "MessageBox.Show(e.Message, "Error! Could not connect");" then add another parentheses underneath. This means that the user will know that the ED has not connected so the counter application will not work when this error box appears.

Step 7: Adding a Function

We are now going to set up a function which we will refer to instead of having to re-write the function out again. The function will include which input lines of the ED device you are using, the 1st input line is referred to as 0, the 2nd input line is referred to as 1 etc. You will need to first write "private void setCount()" before the 2nd to last parentheses in your code. Then go on to a new line and open parentheses. Go onto another new line and write "dailyCountLabel.Text = ed.Inputs[what number input line you are using].Count.ToString();". Do the same on the line underneath for the weeklyCountLabel.

Step 8: Coding for the Buttons

Now we are going to start coding for the objects that are on the form. Go to the tab where you can see your form layout and double click on the dailyResetButton. You should be taken to your code and visual studio will have by default placed some code for the dailyResetButton. The default use for a button is to be clicked so what we are coding for is for what happens when that button is clicked.

In-between the parentheses write "ed.Inputs[the number which was used for your daily.CountLabel].ClearCount();". Underneath that then write "setCount();" so the ED device continues counting after it has been reset. Do this for your weeklyResetButton also.

Step 9: Adding a Timer

The last object we are going to code for and add is the timer. Go onto your form designer and drag a timer from the toolbox onto your form. It should now be displayed at the bottom of your page. Right click it and click on properties, the properties tab should open on the left hand side of your page. We will be changing the "Interval" field. This is the time take before the time updates again. Enter the amount of time (in milliseconds) you wish the count to update, if your box only passes the sensor every half a second then maybe you want to set it to 500 milliseconds.

Now we just need to make sure that the timer starts counting when we open the program. Double click the timer at the bottom of the form to start coding for it.

In-between the parentheses write "setCount();". This is all we are going to write for our timer code.

Now we just need to make sure everything comes on when we start the program. We do this by adding "setCount();" and "timer1.start();" underneath where the "ed.Connect();" is.

Step 10: Trying and Publishing Your Application

You can now press the start button at the top of visual studio to run your code and see if it works. When you are happy with your code you can publish it so you can run it as a standalone application. Go to build and go to publish "your project name". You then just need to choose a place to save it. It will be published as an installer and an exe, you can simply then save the installer onto any computer you wish to use your application on.

<p>Great tutorial, thank you!</p>

About This Instructable




More by TashaLou:Coding a Brainboxes Remote IO device to count 
Add instructable to: