Tutorial for the Web-IO Digital:
Accessing the Web-IO Digital with
Visual Basic.Net using
binary sockets
Succeeding MS Visual Basic 5 and 6 is VB.Net in various versions up to VG 2019. The current Visual Basic version also offers everything needed for programming TCP/IP applications - including support of binary structures. This makes Visual Basic a popular aid for creating applications that communicate with the Web-IO Digital, especially since the Express version of Visual Basic is offered from Microsoft free for downloading. Additional drivers or DLLs are not needed.
Using the following program example you can represent your Web-IO Digital with its inputs and outputs in a Windows application using binary socket mode.
Preparations
- Provide power to the Web-IO and connect the IOs
- Connect the Web-IO to the network
- Assign IP addresses
On the Web-IO in Communication channels >> Socket API activate BINARY1 sockets, check Input Trigger and enable the outputs for switching
Combining the various operating elements and display objects in the VB.net form
In addition to the objects shown here, the program also needs a timer for polling (timer_polling).
When naming the individual objects it is helpful to use logical names. In this example the first part of the name describes the type of object and the second part the function.
Basics
While constructing graphical user interfaces in Visual Basic Express is just as simple to accomplish as with the previous versions, other tasks may at first take some getting used to.
The Winsock control element familiar from VB5 and VB6 is no longer available for VB in the current version.
Instead, network access requires that the Import statement be used in the header of the source text to instance the namespace for the socket classes used. In addition, the socket over which you want communication to be handled must be created and you must define a buffer for the input data.
Furthermore, encapsulation of individual objects in VB.Net was advanced so far that objects which are created within a thread do not permit access from another thread. For example, a checkbox which was created in Thread A cannot be set from Thread B.
To still make object properties accessible for other threads, sub-procedures for object changing need to be created in a separate thread. Delegates are then formed for these sub-procedures through which the other threads can access.
For example in order to enable control elements in the form after successfully opening a connection or to block them after the connection is closed, but also in order to adjust the input/output checkboxes to the actual I/O states after data is received.
Imports System.Net.Sockets
Imports System.Net
Public Class Form1
Public Structure IO_State
Dim outputstate0 As Boolean
Dim outputstate1 As Boolean
Dim inputstate0 As Boolean
Dim inputstate1 As Boolean
Dim countervalue0 As Long
Dim countervalue1 As Long
End Structure
Private Delegate Sub DelegateSub()
Private connectenable As New DelegateSub(AddressOf connect_enable)
Private disconnectenable As New DelegateSub(AddressOf disconnect_enable)
Private formupdate As New DelegateSub(AddressOf form_update)
Dim TCP_client As Socket
Dim connection_ar As IAsyncResult
Dim receivebuffer(511) As Byte
Dim IOState As IO_State
Private Sub connect_enable()
bt_connect.Enabled = False
gb_io.Enabled = True
bt_disconnect.Enabled = True
Timer_polling.Enabled = True
ToolStripStatusLabel1.Text = "Connected to " + tb_ip.Text + " : " + tb_port.Text
End Sub
Private Sub disconnect_enable()
bt_connect.Enabled = True
gb_io.Enabled = False
bt_disconnect.Enabled = False
timer_polling.Enabled = False
ToolStripStatusLabel1.Text = "No Connection"
End Sub
Private Sub form_update()
If IOState.inputstate0 = True Then
cb_input0.Checked = True
Else
cb_input0.Checked = False
End If
If IOState.inputstate1 = True Then
cb_input1.Checked = True
Else
cb_input1.Checked = False
End If
If IOState.outputstate0 = True Then
cb_output0.Checked = True
Else
cb_output0.Checked = False
End If
If IOState.outputstate1 = True Then
cb_Output1.Checked = True
Else
cb_Output1.Checked = False
End If
tb_counter0.Text = IOState.countervalue0
tb_counter1.Text = IOState.countervalue1
End Sub
The binary structures
For binary access the necessary binary structures used for communicating with the Web-IO must be defined. A detailed description of these structures can be found in the binary brief overview or in the programming manual for the Web-IO.
The IODriver structure
With its four 16-bit variables IODriver is the basic structure which is also part of all other structures.
Public Structure structEADriver
Dim Start_1 As UInt16
Dim Start_2 As UInt16
Dim StructType As UInt16
Dim StructLength As UInt16
End Structure
<StructLayout(LayoutKind.Explicit)> Public Structure structOptions
<FieldOffset(0)> Dim EADriver As structEADriver
<FieldOffset(8)> Dim Version As UInt32
<FieldOffset(12)> Dim Options As UInt32
End Structure
Public Structure structSetBit
Dim EADriver As structEADriver
Dim Mask As UInt16
Dim Value As UInt16
End Structure
Public Structure structReadCounter
Dim EADriver As structEADriver
Dim CounterIndex As UInt16
End Structure
Public Structure structWriteRegister
Dim EADriver As structEADriver
Dim Amount As UInt16
Dim Value As UInt16
End Structure
Public Structure structRegisterState
Dim EADriver As structEADriver
Dim DriverID As UInt16
Dim InputValue As UInt16
Dim OutputValue As UInt16
End Structure
<StructLayout(LayoutKind.Explicit)> Public Structure structCounter
<FieldOffset(0)> Dim EADriver As structEADriver
<FieldOffset(8)> Dim CounterIndex As UInt16
<FieldOffset(10)> Dim CounterValue As UInt32
End Structure
<StructLayout(LayoutKind.Explicit)> Public Structure structAllCounter
<FieldOffset(0)> Dim EADriver As structEADriver
<FieldOffset(8)> Dim CounterNoOf As UInt16
<FieldOffset(10)> Dim CounterValue0 As UInt32
<FieldOffset(14)> Dim CounterValue1 As UInt32
End Structure
For the structures it is important that the individual variables be stored in memory in their exact sequence. Visual Basic does not automatic handle this, especially when variables of different sizes are combined in a structure. To still ensure ordered storage in memory, <StructLayout(LayoutKind.Explicit)>
is used to specify that each individual variable uses <FieldOffset(14)>
to have a fixed offset to the first memory location of the structure assigned.
Connection control
Establishing the connectionThe connection is opened by entering the IP address of the Web-IO in the text field ed_ip and clicking on the bt_connect button.
Private Sub bt_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_connect.Click
Dim WebIOep As New IPEndPoint(IPAddress.Parse(tb_ip.Text), Val(tb_port.Text))
If tb_ip.Text <> "" And tb_port.Text <> "" Then
TCP_client = New Socket(AddressFamily.InterNetwork, SocketType.Stream, _ ProtocolType.Tcp)
bt_connect.Enabled = False
Try
TCP_client.BeginConnect(WebIOep, New AsyncCallback(AddressOf callback_connect), TCP_client)
Catch ex As Exception
End Try
End If
End Sub
Opening the connection
For TCP/IP handling first an IPEndPoint is defined from IP address and TCP port and used to initialize the TCP_client socket. As part of the connection request a reference to a callback procedure is created.
Connection is madeAs soon as the Web-IO accepts the connection, the callback procedure is run. An invoke is used to invoke the delegate for the procedure, which enables the control elements and displays the connect status in the status line. In addition a reference to a callback routine for data reception is created.
By sending the structure Options to the Web-IO, the latter is instructed to send the changed state when an output is set, using the structure RegisterState.
Private Sub callback_connect(ByVal ar As IAsyncResult)
Invoke(connectenable)
connection_ar = ar
Try
TCP_client.EndConnect(ar)
TCP_client.BeginReceive(receivebuffer, 0, 512, SocketFlags.None, New AsyncCallback(AddressOf callback_readdata), TCP_client)
Dim BufPtr As IntPtr
Dim Options As structOptions
Options.EADriver.Start_1 = 0
Options.EADriver.Start_2 = 0
Options.EADriver.StructType = &H1F0
Options.EADriver.StructLength = 16
Options.Version = 1
Options.Options = 1
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(Options))
Marshal.StructureToPtr(Options, BufPtr, True)
sendstructure(BufPtr, Options.EADriver.StructLength)
Catch ex As Exception
closeconnections()
End Try
End Sub
Disconnecting
The connection remains open until it is ended by the user clicking on the Disconnecting button or by the Web-IO.
Private Sub bt_disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_disconnect.Click
Invoke(disconnectenable)
closeconnections()
End Sub
In this case a corresponding procedure is invoked.
Private Sub closeconnections()
Try
TCP_client.EndReceive(connection_ar)
Catch ex As Exception
End Try
Try
TCP_client.Shutdown(SocketShutdown.Both)
Catch ex As Exception
End Try
Try
TCP_client.Close()
Catch ex As Exception
End Try
Invoke(disconnectenable)
End Sub
Connection error
All actions affecting TCP/IP communication are executed within the Try instruction. If errors occur, the CloseConnection procedure is also called.
Operation and communication from the client side
Sending commandsAs soon as a connection is made with the Web-IO, the user can use the corresponding program elements to send binary structures to the Web-IO.
Private Sub sendstructure(ByVal BufPtr As IntPtr, Bufsize As Integer)
Dim senddata As Byte()
ReDim senddata(Bufsize - 1)
Marshal.Copy(BufPtr, senddata, 0, Bufsize)
Try
TCP_client.Send(senddata)
Catch ex As Exception
closeconnections()
End Try
Marshal.FreeHGlobal(BufPtr)
End Sub
Visual Basic does not provide a method for direct sending of binary structures. Binary data can only be sent as a byte array. Therefore when the sendstructure function is called a pointer is sent. This point references the memory location starting at which the contents of the structure to be sent can be found. Here it is apparent why the structure variables must be in sequence in memory. The second parameter sent is the length of the structure.
Using Marshal.Copy(BufPtr, senddata, 0, Bufsize)
the structure data are then copied to a byte array. This byte array is then sent to the Web-IO.
The user sets the outputs by using the two check boxes cb_outputx. For this the program uses the MouseUP event of this object. If a MouseUP, i.e. releasing the output checkbox is registered, the program runs the corresponding procedure and passes the correspondingly filled in Set-Bit structure to the Web-IO.
Private Sub cb_output0_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cb_output0.MouseUp
Dim BufPtr As IntPtr
Dim SetBit As structSetBit
SetBit.EADriver.Start_1 = 0
SetBit.EADriver.Start_2 = 0
SetBit.EADriver.StructType = 9
SetBit.EADriver.StructLength = 12
SetBit.Mask = 1
If cb_output0.Checked Then
SetBit.Value = 1
Else
SetBit.Value = 0
End If
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(SetBit))
Marshal.StructureToPtr(SetBit, BufPtr, True)
sendstructure(BufPtr, SetBit.EADriver.StructLength)
End Sub
Private Sub cb_Output1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cb_Output1.MouseUp
Dim BufPtr As IntPtr
Dim SetBit As structSetBit
SetBit.EADriver.Start_1 = 0
SetBit.EADriver.Start_2 = 0
SetBit.EADriver.StructType = 9
SetBit.EADriver.StructLength = 12
SetBit.Mask = 2
If cb_Output1.Checked Then
SetBit.Value = 2
Else
SetBit.Value = 0
End If
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(SetBit))
Marshal.StructureToPtr(SetBit, BufPtr, True)
sendstructure(BufPtr, SetBit.EADriver.StructLength)
End Sub
Querying output/input status
The user can request the status of the outputs and inputs by clicking on the corresponding button.
Private Sub bt_outputs_read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_outputs_read.Click
Dim BufPtr As IntPtr
Dim RegisterRequest As structEADriver
RegisterRequest.Start_1 = 0
RegisterRequest.Start_2 = 0
RegisterRequest.StructType = &H21
RegisterRequest.StructLength = 8
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(RegisterRequest))
Marshal.StructureToPtr(RegisterRequest, BufPtr, True)
sendstructure(BufPtr, RegisterRequest.StructLength)
End Sub
By sending the structure RegisterRequest the switching states of inputs and outputs are requested. The Web-IO replies to this request with the structure RegisterState.
To query only the inputs, click on the bt_inputs button. Here the structure ReadRegister is sent, to which the Web-IO replies with the structure WriteRegister.
Private Sub bt_inputs_read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_inputs_read.Click
Dim BufPtr As IntPtr
Dim ReadRegister As structEADriver
ReadRegister.Start_1 = 0
ReadRegister.Start_2 = 0
ReadRegister.StructType = 1
ReadRegister.StructLength = 8
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadRegister))
Marshal.StructureToPtr(ReadRegister, BufPtr, True)
sendstructure(BufPtr, ReadRegister.StructLength)
End Sub
Read/clear counters
You can also query or clear the counter states of the input counters. Here the structure ReadCounter or ReadClearCounter is sent, whereby CounterIndex is used to send the number of the counter. The Web-IO replies with the structure Counter.
Private Sub bt_counter_read0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_counter_read0.Click
Dim BufPtr As IntPtr
Dim ReadCounter As structReadCounter
ReadCounter.EADriver.Start_1 = 0
ReadCounter.EADriver.Start_2 = 0
ReadCounter.EADriver.StructType = &HB0
ReadCounter.EADriver.StructLength = 10
ReadCounter.CounterIndex = 0
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadCounter))
Marshal.StructureToPtr(ReadCounter, BufPtr, True)
sendstructure(BufPtr, ReadCounter.EADriver.StructLength)
End Sub
Private Sub bt_counter_read1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_counter_read0.Click
Dim BufPtr As IntPtr
Dim ReadCounter As structReadCounter
ReadCounter.EADriver.Start_1 = 0
ReadCounter.EADriver.Start_2 = 0
ReadCounter.EADriver.StructType = &HB0
ReadCounter.EADriver.StructLength = 10
ReadCounter.CounterIndex = 1
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadCounter))
Marshal.StructureToPtr(ReadCounter, BufPtr, True)
sendstructure(BufPtr, ReadCounter.EADriver.StructLength)
End Sub
Private Sub bt_counter_clear0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_counter_clear0.Click
Dim BufPtr As IntPtr
Dim ReadClearCounter As structReadCounter
ReadClearCounter.EADriver.Start_1 = 0
ReadClearCounter.EADriver.Start_2 = 0
ReadClearCounter.EADriver.StructType = &HC0
ReadClearCounter.EADriver.StructLength = 10
ReadClearCounter.CounterIndex = 0
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadClearCounter))
Marshal.StructureToPtr(ReadClearCounter, BufPtr, True)
sendstructure(BufPtr, ReadClearCounter.EADriver.StructLength)
End Sub
Private Sub bt_counter_clear1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_counter_clear0.Click
Dim BufPtr As IntPtr
Dim ReadClearCounter As structReadCounter
ReadClearCounter.EADriver.Start_1 = 0
ReadClearCounter.EADriver.Start_2 = 0
ReadClearCounter.EADriver.StructType = &HC0
ReadClearCounter.EADriver.StructLength = 10
ReadClearCounter.CounterIndex = 1
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadClearCounter))
Marshal.StructureToPtr(ReadClearCounter, BufPtr, True)
sendstructure(BufPtr, ReadClearCounter.EADriver.StructLength)
End Sub
The structure type RegisterState ReadAllCounter or ReadAllCounter can be used to read or clear all the counters at the same time. The Web-IO replies with the structure AllCounter.
Private Sub bt_counter_readall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_counter_readall.Click
Dim BufPtr As IntPtr
Dim ReadAllCounter As structEADriver
ReadAllCounter.Start_1 = 0
ReadallCounter.Start_2 = 0
ReadAllCounter.StructType = &HB1
ReadAllCounter.StructLength = 10
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadAllCounter))
Marshal.StructureToPtr(ReadAllCounter, BufPtr, True)
sendstructure(BufPtr, ReadAllCounter.StructLength)
End Sub
Private Sub bt_counter_clearall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_counter_clearall.Click
Dim BufPtr As IntPtr
Dim ReadClaerAllCounter As structEADriver
ReadClaerAllCounter.Start_1 = 0
ReadClaerAllCounter.Start_2 = 0
ReadClaerAllCounter.StructType = &HC1
ReadClaerAllCounter.StructLength = 10
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadClaerAllCounter))
Marshal.StructureToPtr(ReadClaerAllCounter, BufPtr, True)
sendstructure(BufPtr, ReadClaerAllCounter.StructLength)
End Sub
Receiving data from the Web-IO
Process and display the received dataThe Web-IO returns the appropriate structure depending on the query or triggering event. When data are received the corresponding callback procedure is invoked. Processing involves filling the first 8 bytes of the received byte array using a pointer operation first with an IODriver structure. The application recognizes which structure type it is using the variable IODriver.StructType.
-
IODriver.StructType = 8
structure WriteRegister for the status of the inputs
-
IODriver.StructType = 31 (hex)
structure RegisterState for the status of the inputs and outputs
-
IODriver.StructType = B4 (hex)
structure Counter for the value of individual counters
-
IODriver.StructType = B5 (hex)
structure AllCounter for the value of all counters
For processing the received byte array now uses a pointer operation to fill the appropriate structure.
The values thus sent are then processed and displayed. For the inputs and outputs WriteRegisterValue, RegisterStae.InputValue and RegisterStae.outputValue are used to send the bit pattern of all inputs and outputs.
Private Sub callback_readdata(ByVal ar As IAsyncResult)
If TCP_client.Connected Then
Dim bytesread As Integer
Try
bytesread = TCP_client.EndReceive(ar)
Catch ex As Exception
End Try
If bytesread = 0 Then
closeconnections()
Else
Try
TCP_client.BeginReceive(receivebuffer, 0, 512, SocketFlags.None, New AsyncCallback(AddressOf callback_readdata), TCP_client)
Catch ex As Exception
closeconnections()
End Try
Dim MyGC As GCHandle = GCHandle.Alloc(receivebuffer, GCHandleType.Pinned)
Dim EADriver As structEADriver = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, EADriver.GetType)
Select Case EADriver.StructType
Case 8
Dim WriteRegister As structWriteRegister = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, WriteRegister.GetType)
If (WriteRegister.Value And 1) = 1 Then
IOState.inputstate0 = True
Else
IOState.inputstate0 = False
End If
If (WriteRegister.Value And 2) = 2 Then
IOState.inputstate1 = True
Else
IOState.inputstate1 = False
End If
Case &H31
Dim RegisterState As structRegisterState = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, RegisterState.GetType)
If (RegisterState.InputValue And 1) = 1 Then
IOState.inputstate0 = True
Else
IOState.inputstate0 = False
End If
If (RegisterState.InputValue And 2) = 2 Then
IOState.inputstate1 = True
Else
IOState.inputstate1 = False
End If
If (RegisterState.OutputValue And 1) = 1 Then
IOState.outputstate0 = True
Else
IOState.outputstate0 = False
End If
If (RegisterState.OutputValue And 2) = 2 Then
IOState.outputstate1 = True
Else
IOState.outputstate1 = False
End If
Case &HB4
Dim Counter As structCounter = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, Counter.GetType)
If Counter.CounterIndex = 0 Then IOState.countervalue0 = Counter.CounterValue.ToString
If Counter.CounterIndex = 1 Then IOState.countervalue1 = Counter.CounterValue.ToString
Case &HB5
Dim AllCounter As structAllCounter = Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, AllCounter.GetType)
IOState.countervalue0 = AllCounter.CounterValue0.ToString
IOState.countervalue1 = AllCounter.CounterValue1.ToString
End Select
Invoke(formupdate)
MyGC.Free()
End If
End If
End Sub
Polling
Cyclical polling of particular valuesIn order to enable automatic refreshing of the display, a timer is used.
Depending on the check boxes for output, input and counter polling, the corresponding information is obtained from the Web-IO at a set interval.
Private Sub timer_polling_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer_polling.Elapsed
If ((cb_input_polling.Checked Or cb_output_polling.Checked) And TCP_client.Connected) Then
Dim BufPtr As IntPtr
Dim RegisterRequest As structEADriver
RegisterRequest.Start_1 = 0
RegisterRequest.Start_2 = 0
RegisterRequest.StructType = 1
RegisterRequest.StructLength = &H21
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(RegisterRequest))
Marshal.StructureToPtr(RegisterRequest, BufPtr, True)
sendstructure(BufPtr, RegisterRequest.StructLength)
End If
If (cb_counter_polling.Checked And TCP_client.Connected) Then
Dim BufPtr As IntPtr
Dim ReadAllCounter As structEADriver
ReadAllCounter.Start_1 = 0
ReadAllCounter.Start_2 = 0
ReadAllCounter.StructType = &HB1
ReadAllCounter.StructLength = 10
BufPtr = Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(ReadAllCounter))
Marshal.StructureToPtr(ReadAllCounter, BufPtr, True)
sendstructure(BufPtr, ReadAllCounter.StructLength)
End If
End Sub
The desired interval can be entered in the corresponding text field. When changes are made the timer interval is automatically adjusted.
Private Sub tb_interval_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_interval.TextChanged
timer_polling.Interval = Val(tb_interval.Text)
End Sub
The sample program supports the common functions of the Web-IO in binary socket mode, optimized for the Web-IO 2x Digital Input, 2x Digital Output. For the other Web-IO models you may have to make changes to the program. A detailed description of the binary structures can be found in the binary brief overview or in the programming manual for the Web-IO.