### Sample IO-16

An example of the Class to set or get the state of an IO-16 Bricklet pin (0-7) from port (a or b).
The TinkerForge API Reference for the IO-16 Bricklet:
http://www.tinkerforge.com/en/doc/Software/Bricklets/IO16_Bricklet_Java.html#io16-bricklet-java-api
`Private Sub Class_Globals
Dim joInline As JavaObject = Me
Public PortValueMask As String
End Sub`
`Public Sub Initialize
' NO ACTION - but must be called prior using the class
End Sub`
' Set the state to true or false of a IO-16 port pin.
`Public Sub SetPinState(HOST As String, Port As Int, UID As String, IOPort As Char, IOPin As Int, State As Boolean)
joInline.RunMethod("SetPinState", Array(HOST, Port, UID, IOPort, IOPin, State))
End Sub`
' Get the configuration of a port as 8 bit value mask. The global PortValueMask is set.
`Public Sub GetPortConfig(HOST As String, Port As Int, UID As String, IOPort As Char)
joInline.RunMethod("GetPortConfig", Array(HOST, Port, UID, IOPort))
End Sub`
' Get the value mask of a port. each port has 8 pins 0 - 7. the value mask has 8 bits.
' Examples: 255 = 11111111, 239 = 11101111
' Hint: to get the value mask as string array use:
' Dim bits() As String = Regex.Split("", PortValueMask)
' For Each s As String In bits
' Log(s)
' Next
`Private Sub GetPortValueMask(IOPort As Char, ValueMask As Short) 'ignore
PortValueMask = ToBinaryString(ValueMask, 8)
' Log($"${ValueMask} ${ToBinaryString(ValueMask, 8)}"$)
End Sub`
' Converts a number as into a binary string
' Parameter: number to convert, length of the binary being 8 or 32 bit
' Return the nunber as a binary string
`Private Sub ToBinaryString(number As Int, length As Int) As String
length = length - 1
Dim sb As StringBuilder
sb.Initialize
Dim x As Int = Bit.ShiftLeft(1, length)
For i = 0 To length
Dim ii As Int = Bit.And(number, x)
If ii <> 0 Then
sb.Append("1")
Else
sb.Append("0")
End If
x = Bit.UnsignedShiftRight(x, 1)
Next
Return sb.ToString
End Sub`
If Java
import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletIO16;
// Set the output state of a pin for a port. After setting the pin, the connection is disconnected.
public void SetPinState(String HOST, int PORT, String UID, char IOPort, int IOPin, Boolean IOState) throws Exception {
IPConnection ipcon = new IPConnection(); // Create IP connection
BrickletIO16 io = new BrickletIO16(UID, ipcon); // Create device object
ipcon.connect(HOST, PORT); // Connect to brickd
io.setPortConfiguration(IOPort, (short)(1 << IOPin), 'o', IOState);
ipcon.disconnect();
}
// Get the configuration of the port as 8 bit string
public void GetPortConfig(String HOST, int PORT, String UID, char IOPort) throws Exception {
IPConnection ipcon = new IPConnection(); // Create IP connection
BrickletIO16 io = new BrickletIO16(UID, ipcon); // Create device object
ipcon.connect(HOST, PORT); // Connect to brickd
short valueMask = io.getPort(IOPort);
// raise a ba event to set the value of the global PortValueMask
ba.raiseEvent(this, "getportvaluemask", IOPort, valueMask);
ipcon.disconnect();
}
End If

Changed: 2018-06-07 09:24, Category: TinkerForge Inline Java, File: T6C265M1972.htm, ID: 1972