This is the description of the Visual Basic .NET API bindings for the IP Connection. The IP Connection manages the communication between the API bindings and the Brick Daemon or a WIFI/Ethernet Extension. Before Bricks and Bricklets can be controlled using their API an IP Connection has to be created and its TCP/IP connection has to be established.
An installation guide for the Visual Basic .NET API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (ExampleEnumerate.vb)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | Imports Tinkerforge
Module ExampleEnumerate
Const HOST As String = "localhost"
Const PORT As Integer = 4223
Sub EnumerateCB(ByVal sender As IPConnection, _
ByVal uid As String, _
ByVal connectedUid As String, _
ByVal position As Char, _
ByVal hardwareVersion() As Short, _
ByVal firmwareVersion() As Short, _
ByVal deviceIdentifier As Integer, _
ByVal enumerationType As Short)
System.Console.WriteLine("UID: {0}", uid)
System.Console.WriteLine("Enumeration Type: {0}", enumerationType)
If enumerationType = IPConnection.ENUMERATION_TYPE_DISCONNECTED Then
System.Console.WriteLine("")
Return
End If
System.Console.WriteLine("Connected UID: {0}", connectedUid)
System.Console.WriteLine("Position: {0}", position)
System.Console.WriteLine("Hardware Version: {0}.{1}.{2}", _
hardwareVersion(0), hardwareVersion(1), hardwareVersion(2))
System.Console.WriteLine("Firmware Version: {0}.{1}.{2}", _
firmwareVersion(0), firmwareVersion(1), firmwareVersion(2))
System.Console.WriteLine("Device Identifier: {0}", deviceIdentifier)
System.Console.WriteLine("")
End Sub
Sub Main()
' Create connection and connect to brickd
Dim ipcon As New IPConnection()
ipcon.Connect(HOST, PORT)
' Register Enumerate Callback
AddHandler ipcon.EnumerateCallback, AddressOf EnumerateCB
' Trigger Enumerate
ipcon.Enumerate()
System.Console.WriteLine("Press key to exit")
System.Console.ReadLine()
ipcon.Disconnect()
End Sub
End Module
|
Download (ExampleAuthenticate.vb)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | Imports Tinkerforge
Module ExampleAuthenticate
Const HOST As String = "localhost"
Const PORT As Integer = 4223
Const SECRET As String = "My Authentication Secret!"
Sub ConnectedCB(ByVal sender As IPConnection, _
ByVal connectReason As Short)
Select Case connectReason
Case IPConnection.CONNECT_REASON_REQUEST
System.Console.WriteLine("Connected by request")
Case IPConnection.CONNECT_REASON_AUTO_RECONNECT
System.Console.WriteLine("Auto-Reconnect")
End Select
' Authenticate first...
Try
sender.Authenticate(SECRET)
System.Console.WriteLine("Authentication succeeded")
catch
System.Console.WriteLine("Could not authenticate")
Exit Sub
End Try
' ...then trigger enumerate
sender.Enumerate()
End Sub
Sub EnumerateCB(ByVal sender As IPConnection, _
ByVal uid As String, _
ByVal connectedUid As String, _
ByVal position As Char, _
ByVal hardwareVersion() As Short, _
ByVal firmwareVersion() As Short, _
ByVal deviceIdentifier As Integer, _
ByVal enumerationType As Short)
System.Console.WriteLine("UID: {0}, Enumeration Type: {1}", uid, enumerationType)
End Sub
Sub Main()
' Create IPConnection and connect to brickd
Dim ipcon As New IPConnection()
' Register Connected Callback
AddHandler ipcon.Connected, AddressOf ConnectedCB
' Register Enumerate Callback
AddHandler ipcon.EnumerateCallback, AddressOf EnumerateCB
' Connect to brickd
ipcon.Connect(HOST, PORT)
System.Console.WriteLine("Press key to exit")
System.Console.ReadLine()
ipcon.Disconnect()
End Sub
End Module
|
Creates an IP Connection object that can be used to enumerate the available devices. It is also required for the constructor of Bricks and Bricklets.
Creates a TCP/IP connection to the given host and port. The host and port can refer to a Brick Daemon or to a WIFI/Ethernet Extension.
Devices can only be controlled when the connection was established successfully.
Blocks until the connection is established and throws an exception if there is no Brick Daemon or WIFI/Ethernet Extension listening at the given host and port.
Disconnects the TCP/IP connection from the Brick Daemon or the WIFI/Ethernet Extension.
Performs an authentication handshake with the connected Brick Daemon or WIFI/Ethernet Extension. If the handshake succeeds the connection switches from non-authenticated to authenticated state and communication can continue as normal. If the handshake fails then the connection gets closed. Authentication can fail if the wrong secret was used or if authentication is not enabled at all on the Brick Daemon or the WIFI/Ethernet Extension.
See the authentication tutorial for more information.
New in version 2.1.0.
Can return the following states:
Enables or disables auto-reconnect. If auto-reconnect is enabled, the IP Connection will try to reconnect to the previously given host and port, if the connection is lost.
Default value is true.
Returns true if auto-reconnect is enabled, false otherwise.
Sets the timeout in milliseconds for getters and for setters for which the response expected flag is activated.
Default timeout is 2500.
Returns the timeout as set by SetTimeout().
Broadcasts an enumerate request. All devices will respond with an enumerate callback.
Stops the current thread until Unwait() is called.
This is useful if you rely solely on callbacks for events, if you want to wait for a specific callback or if the IP Connection was created in a thread.
Wait and Unwait act in the same way as Acquire and Release of a semaphore.
Callbacks can be registered to be notified about events. The registration is done by appending your callback handler to the corresponding event:
Sub Callback(ByVal sender As IPConnection, ByVal value As Short)
Console.WriteLine("Value: {0}", value)
End Sub
AddHandler ipcon.Example, AddressOf Callback
The available events are described below.
The Event receives seven parameters:
Possible enumeration types are:
It should be possible to implement plug-and-play functionality with this (as is done in Brick Viewer).
The device identifier numbers can be found here. There are also constants for these numbers named following this pattern:
<device-class>.DEVICE_IDENTIFIER
For example: BrickMaster.DEVICE_IDENTIFIER or BrickletAmbientLight.DEVICE_IDENTIFIER.
This event is triggered whenever the IP Connection got connected to a Brick Daemon or to a WIFI/Ethernet Extension, possible reasons are:
This event is triggered whenever the IP Connection got disconnected from a Brick Daemon or from a WIFI/Ethernet Extension, possible reasons are: