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)
1Imports Tinkerforge
2
3Module ExampleEnumerate
4 Const HOST As String = "localhost"
5 Const PORT As Integer = 4223
6
7 Sub EnumerateCB(ByVal sender As IPConnection, _
8 ByVal uid As String, _
9 ByVal connectedUid As String, _
10 ByVal position As Char, _
11 ByVal hardwareVersion() As Short, _
12 ByVal firmwareVersion() As Short, _
13 ByVal deviceIdentifier As Integer, _
14 ByVal enumerationType As Short)
15 System.Console.WriteLine("UID: {0}", uid)
16 System.Console.WriteLine("Enumeration Type: {0}", enumerationType)
17
18 If enumerationType = IPConnection.ENUMERATION_TYPE_DISCONNECTED Then
19 System.Console.WriteLine("")
20 Return
21 End If
22
23 System.Console.WriteLine("Connected UID: {0}", connectedUid)
24 System.Console.WriteLine("Position: {0}", position)
25 System.Console.WriteLine("Hardware Version: {0}.{1}.{2}", _
26 hardwareVersion(0), hardwareVersion(1), hardwareVersion(2))
27 System.Console.WriteLine("Firmware Version: {0}.{1}.{2}", _
28 firmwareVersion(0), firmwareVersion(1), firmwareVersion(2))
29 System.Console.WriteLine("Device Identifier: {0}", deviceIdentifier)
30 System.Console.WriteLine("")
31 End Sub
32
33 Sub Main()
34 ' Create connection and connect to brickd
35 Dim ipcon As New IPConnection()
36 ipcon.Connect(HOST, PORT)
37
38 ' Register Enumerate Callback
39 AddHandler ipcon.EnumerateCallback, AddressOf EnumerateCB
40
41 ' Trigger Enumerate
42 ipcon.Enumerate()
43
44 System.Console.WriteLine("Press key to exit")
45 System.Console.ReadLine()
46 ipcon.Disconnect()
47 End Sub
48End Module
Download (ExampleAuthenticate.vb)
1Imports Tinkerforge
2
3Module ExampleAuthenticate
4 Const HOST As String = "localhost"
5 Const PORT As Integer = 4223
6 Const SECRET As String = "My Authentication Secret!"
7
8 Sub ConnectedCB(ByVal sender As IPConnection, _
9 ByVal connectReason As Short)
10 Select Case connectReason
11 Case IPConnection.CONNECT_REASON_REQUEST
12 System.Console.WriteLine("Connected by request")
13 Case IPConnection.CONNECT_REASON_AUTO_RECONNECT
14 System.Console.WriteLine("Auto-Reconnect")
15 End Select
16
17 ' Authenticate first...
18 Try
19 sender.Authenticate(SECRET)
20 System.Console.WriteLine("Authentication succeeded")
21 catch
22 System.Console.WriteLine("Could not authenticate")
23 Exit Sub
24 End Try
25
26 ' ...reenable auto reconnect mechanism, as described below...
27 sender.SetAutoReconnect(true)
28
29 ' ...then trigger enumerate
30 sender.Enumerate()
31 End Sub
32
33 Sub EnumerateCB(ByVal sender As IPConnection, _
34 ByVal uid As String, _
35 ByVal connectedUid As String, _
36 ByVal position As Char, _
37 ByVal hardwareVersion() As Short, _
38 ByVal firmwareVersion() As Short, _
39 ByVal deviceIdentifier As Integer, _
40 ByVal enumerationType As Short)
41 System.Console.WriteLine("UID: {0}, Enumeration Type: {1}", uid, enumerationType)
42 End Sub
43
44 Sub Main()
45 ' Create IPConnection and connect to brickd
46 Dim ipcon As New IPConnection()
47
48 ' Register Connected Callback
49 AddHandler ipcon.Connected, AddressOf ConnectedCB
50
51 ' Register Enumerate Callback
52 AddHandler ipcon.EnumerateCallback, AddressOf EnumerateCB
53
54 ' Disable auto reconnect mechanism, in case we have the wrong secret.
55 ' If the authentication is successful, reenable it.
56 ipcon.SetAutoReconnect(false)
57
58 ' Connect to brickd
59 ipcon.Connect(HOST, PORT)
60
61 System.Console.WriteLine("Press key to exit")
62 System.Console.ReadLine()
63 ipcon.Disconnect()
64 End Sub
65End 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.
Added in version 2.1.0.
Can return the following states:
IPConnection.CONNECTION_STATE_DISCONNECTED = 0: No connection is established.
IPConnection.CONNECTION_STATE_CONNECTED = 1: A connection to the Brick Daemon or the WIFI/Ethernet Extension is established.
IPConnection.CONNECTION_STATE_PENDING = 2: IP Connection is currently trying to connect.
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 currently existing connection is lost.
Therefore, auto-reconnect only does something after a successful
Connect() call.
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 MyCallback(ByVal sender As IPConnection, ByVal value As Short)
Console.WriteLine("Value: {0}", value)
End Sub
AddHandler ipcon.ExampleCallback, AddressOf MyCallback
The available events are described below.
The Event receives seven parameters:
uid: The UID of the device.
connectedUid: UID where the device is connected to. For a Bricklet this
is the UID of the Brick or Bricklet it is connected to. For a Brick it is
the UID of the bottommost Brick in the stack. For the bottommost Brick
in a stack it is "0". With this information it is possible to
reconstruct the complete network topology.
position: For Bricks: '0' - '8' (position in stack). For Bricklets:
'a' - 'h' (position on Brick) or 'i' (position of the Raspberry Pi (Zero) HAT)
or 'z' (Bricklet on Isolator Bricklet).
hardwareVersion: Major, minor and release number for hardware version.
firmwareVersion: Major, minor and release number for firmware version.
deviceIdentifier: A number that represents the device.
enumerationType: Type of enumeration.
Possible enumeration types are:
IPConnection.ENUMERATION_TYPE_AVAILABLE = 0: Device is available
(enumeration triggered by user: IPConnection.Enumerate()).
This enumeration type can occur multiple times for the same device.
IPConnection.ENUMERATION_TYPE_CONNECTED = 1: Device is newly connected (automatically send by Brick after establishing a communication connection). This indicates that the device has potentially lost its previous configuration and needs to be reconfigured.
IPConnection.ENUMERATION_TYPE_DISCONNECTED = 2: Device is disconnected
(only possible for USB connection). In this case only uid and
enumerationType are valid.
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:
IPConnection.CONNECT_REASON_REQUEST = 0: Connection established after request from user.
IPConnection.CONNECT_REASON_AUTO_RECONNECT = 1: Connection after auto-reconnect.
This event is triggered whenever the IP Connection got disconnected from a Brick Daemon or from a WIFI/Ethernet Extension, possible reasons are:
IPConnection.DISCONNECT_REASON_REQUEST = 0: Disconnect was requested by user.
IPConnection.DISCONNECT_REASON_ERROR = 1: Disconnect because of an unresolvable error.
IPConnection.DISCONNECT_REASON_SHUTDOWN = 2: Disconnect initiated by Brick Daemon or WIFI/Ethernet Extension.