VisionBox .NET Library  1.7.10.0 (2024-04-16)
DigInOut_Example.vb

Visual Basic example for using the DigitalInput and DigitalOutput device

1 Public Class DigInOut_Example
2  ' Connect all outputs with the inputs Out 0 to In 0 and so on
3  Public Shared Sub DigitalInputOutput()
4  Console.Clear()
5  'acquire a new factory
6  Dim pVIBSystem As VIB_NET.VIBSystem = New VIB_NET.VIBSystem()
7 
8  If pVIBSystem IsNot Nothing Then
9  Try
10  ' Open digital input device 0
11  Dim pDigitalInput As VIB_NET.DigitalInput = DirectCast(pVIBSystem.OpenDevice(VIB_NET.eDEVICE_TYPE.DIGITAL_INPUT, 0), VIB_NET.DigitalInput)
12  ' Open digital output device 0
13  Dim pDigitalOutput As VIB_NET.DigitalOutput = DirectCast(pVIBSystem.OpenDevice(VIB_NET.eDEVICE_TYPE.DIGITAL_OUTPUT, 0), VIB_NET.DigitalOutput)
14 
15  If (pDigitalInput IsNot Nothing) And (pDigitalOutput IsNot Nothing) Then
16  Try
17  ' Set the debounce time: edge high to low = 200 ns and edge low to high = 200 ns
18  pDigitalInput.ConfigureDebounceTime(200, 200)
19  ' Get the number of digital inputs and outputs
20  Dim NumberOfInputs As UInt32 = pDigitalInput.NumberOfInputs
21  Dim NumberOfOutputs As UInt32 = pDigitalOutput.NumberOfOutputs
22  Console.WriteLine(vbNewLine + "Number of digital outputs: " + NumberOfOutputs.ToString() + vbNewLine + "Number of digital inputs: " + NumberOfInputs.ToString())
23 
24  ' Set state of digital outputs syncroniously
25  pDigitalOutput.Output = 255
26  ' Get state of digital inputs syncroniously
27  Console.WriteLine(vbNewLine + "Input state: " + pDigitalInput.Input.ToString())
28  System.Threading.Thread.Sleep(1000)
29  ' Get state of one digital input
30  Console.WriteLine(vbNewLine + "Input state Bit 0: " + pDigitalInput.GetBit(0).ToString())
31  ' Set state of one digital output
32  pDigitalOutput.SetBit(0, False)
33  Console.WriteLine(vbNewLine + "Input state Bit 0: " + pDigitalInput.GetBit(0).ToString())
34 
35  ' Turn all digital outputs off
36  pDigitalOutput.Output = 0
37 
38  ' Configure event filter to input 0 and 1 (2^0 + 2^1 = 3)
39  pDigitalInput.SensitivityMask = 3
40 
41  ' Start waiting for input event with a timeout of 1 s
42  Try
43  Dim bWaitForInputEvent As Boolean = False
44  If (bWaitForInputEvent) Then
45  Console.WriteLine("Input Event, new Input State: " + pDigitalInput.WaitForInputEvent(1000).ToString())
46  End If
47  Catch e As System.Exception
48  Console.WriteLine("WaitForInputEvent: TimeOut")
49  End Try
50 
51  ' Abort waiting for input event
52  pDigitalInput.AbortWaitingForInputEvent()
53  Catch e As System.Exception
54  Console.WriteLine(e.Message)
55  Finally
56  ' Close the digital output
57  If pDigitalOutput IsNot Nothing Then
58  pVIBSystem.CloseDevice(pDigitalOutput)
59  pDigitalOutput = Nothing
60  End If
61  ' Close the digital input
62  If pDigitalInput IsNot Nothing Then
63  pVIBSystem.CloseDevice(pDigitalInput)
64  pDigitalInput = Nothing
65  End If
66  End Try
67  End If
68  Catch e As System.Exception
69  Console.WriteLine(e.Message)
70  Finally
71  'release factory
72  pVIBSystem = Nothing
73  End Try
74  End If
75  Console.WriteLine(vbNewLine + "Press enter to proceed...")
76  System.Console.ReadLine()
77  End Sub
78 End Class