Visual C++ was until recently one of the most widely used development platforms for creating Windows applications. But in the meantime, more and more programmers are working with the .Net Framework and creating their applications in C#.
Using the following C# program example you can represent your Web-IO Digital with its inputs and outputs in a Windows application. You can also switch the Web-IO outputs.
You don’t have a Web-IO® yet but would like to try the example out sometime?
No problem: We will be glad to send you the Web-IO Digital 2xInput, 2xOutput at no charge for 30 days. Simply fill out a sample ordering form, and we will ship the Web-IO for testing on an open invoice. If you return the unit within 30 days, we will simply mark the invoice as paid.
assigned it an IP address - which with WuTility is no problem.
1. Combine the various operating elements and display objects in the form
2. Import resources and declarations from member variables
Firstly, all classes required for the network connection and the GUI are imported.
using
System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.Text;
Then the application components and important variables for a TCP connection are declared as member variables, thereby making them accessible to the class methods.
Setting up the operating elements The group with the operating elements for the Web-IO is first blocked from operation. As soon as a connection is established, all elements are enabled which have a meaningful format.
The name of the respective operating element is derived from the element itself depending on the context. The first two characters in the name stand for the element type (cb -> Checkbox, bt -> Button, gb -> Groupbox and tb-> TextBox).
Establishing the connection After entering the IP address for the Web-IO in the text field tb_IP and Port 80 in the text field tb_port, clicking on the bt_connect button allows a connection to be opened. If no IP address or port is entered, a message is displayed in the status bar
Opening the connection Now to be able to open a TCP connection, the already declared socket variable is initialized. It is given a stream and the connection type. In addition, a variable is generated which saves the port and IP address.
So that the program can run asynchronous, it does not wait for events, but rather uses callback routines. Callback methods are initialized when a process is started and invoked when the corresponding event occurs, i.e. for example when a connection is opened, for sending or receiving.
private void bt_Connect_Click(object
sender, System.EventArgs e)
{ try
{ if((tb_IP.Text != "")
&& (tb_Port.Text == "80"))
{
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new
IPEndPoint(IPAddress.Parse(tb_IP.Text), int.Parse(tb_Port.Text));
client.BeginConnect(ipep,
new AsyncCallback(connectCallback), client);
} else
statusBar.Text = "IP
and Port needed!";
} catch(SocketException)
{ statusBar.Text = "Connection not possible!";
}
}
In the following the callback routine is shown which is invoked when the connection is established. After successfully opening a connection, all useful operating elements for the application are enabled and the Connect button deactivated. In addition, the application immediately begins to go into receive ready mode. For this the callback routine "receivecallback" is initialized, which is explained in greater detail below.
Disconnecting The connection remains open until the user clicks on the Disconnect button or until it is ended by the Web-IO. After clicking on the button a message is displayed that the connection has been ended.
When the connection is ended all elements must be returned to their initial state. It should then no longer be possible to actuate the Disconnect button.
Setting the outputs The outputs on the Web-IO can be switched using the two checkboxes IDC_OUTPUT0 and IDC_OUTPUT1. The checkbox notes when it was clicked and then triggers an action. Depending on whether the checkbox is already set, the output is either set to ON or OFF.
Since all counter states can be read or reset using one command, there must be a method implemented which processes the reply string from the Web-IO and assigns each counter in the application its specific state.
All commands and requests to the Web-IO are acknowledged with a reply string. The replies have a specific structure depending on the type:
For the outputs: output;<binary value of the output status in hexadecimal format> For a special output: outputx;<ON or OFF>
For the inputs: input;<binary value of the input status in hexadecimal format> For a special input inputx;<ON or OFF>
Then there is the reply string for a counter, which looks as follows.
Counter: counterx;<decimal counter state>
or counter;<decimal counter state 0 >; <decimal counter state 0 >; ... if you want to read all the counters at one time.
All reply strings are finished off with a 0 byte.
In our application the method receiveCallback() is invoked to receive such a message. In this method the reply string from the Web-IO is read and processed. The unique feature here is the event-driven invoking, which takes place as soon as the Web-IO sends data to the application.
Cyclical polling of particular values It is desirable that the status of an individual component be updated by the component itself, so that the application always shows the current status. For this the program uses a timer which sends cyclical queries to the Web-IO at a user-defined time interval.
The time interval can be specified in the field IDC_POLLINGTEXT.
Of course this also catches cases where the user enters a nonsense value, such as a negative time value.
The sample program supports all common functions of the Web-IO in command string mode, optimized for the Web-IO 2x Digital Input, 2x Digital Output. For the other Web-IO models you may have to adapt the program. Additional program examples for socket programming can be found on the Tool pages for the Web-IO. A detailed description for the socket interface of the Web-IO Digital models can be found in the reference manual.