Public Sub EnableEvent(ByVal eventtype As Eth32EventType, ByVal port As Long, _ ByVal bit As Long, ByVal id As Long)
This method enables reception of the specified event on this connection to the device. The ETH32 device only sends event notifications to those connections that specifically request them, so this method requests notification for the specified event from the device, as well as internally assigns the event an ID number provided by you.
eventtype - The type of event to enable. This parameter is a Eth32EventType enumerator type, which has the following valid values:
EVENT_DIGITAL - Digital I/O event. This includes port events and bit events.
EVENT_ANALOG - Analog event based on thresholds defined with the SetAnalogEventDef Method.
EVENT_COUNTER_ROLLOVER - Counter rollover event, which occurs when the counter rolls over to zero.
EVENT_COUNTER_THRESHOLD - Counter threshold event, which occurs when the counter passes a threshold defined with the CounterThreshold Property.
EVENT_HEARTBEAT - Periodic event sent by the device to indicate the TCP/IP connection is still good.
port - For digital events, specifies the port number, for analog events, specifies the bank number, and for either counter event, specifies the counter number.
bit - For digital events, this should be -1 for port events or the bit number (0-7) for bit events. For analog events, this specifies the analog channel number (0-7).
id - You may specify any number to be associated with this event.
The id parameter allows you to assign any arbitrary number to this particular event. The ID you assign is included with the event information whenever this event fires. The idea is that you can identify a particular event with a single comparison rather than needing to inspect several pieces of data such as the event type, port number, and bit number. The ID number is completely arbitrary and multiple events may be given the same ID number if desired. The ID numbers are stored within the API and are not sent to the ETH32 device.
One other minor technicality is that the heartbeat event is permanently enabled on the ETH32 device itself for purposes of connection maintenance. Therefore, for the heartbeat event, this method simply enables the event within the API, meaning that when the event comes in, rather than being discarded it will be added to the event queue. The one small side-effect to this fact is that if you have enabled reception of the heartbeat event and another connection calls the ResetDevice Method, you will continue to receive heartbeat events, whereas all other event types will have been disabled on the device itself. Note that if you call ResetDevice on your own connection, it automatically disables the heartbeat event within the API for your connection, so in that case it is not an issue.
This example is a very simple, yet compilable, example of how to utilize events. To compile this example, create a new project, add the Eth32 support files as described in the Getting Started section, and create a button named setup_button on the main form. Then, paste this code into the code window for that form:
Option Explicit Dim WithEvents dev As Eth32 Private Sub dev_EventFired(ByVal id As Long, ByVal eventtype As Long, ByVal port As Long, _ ByVal bit As Long, ByVal prev_value As Long, ByVal value As Long, _ ByVal direction As Long) MsgBox "An event has fired. ID: " & id & " Value: " & value End Sub Private Sub Form_Unload(Cancel As Integer) ' When this form unloads, make sure the connection is closed, otherwise ' it will keep the application running. If Not (dev Is Nothing) Then ' dev is at least instantiated If dev.Connected Then dev.Disconnect End If End If End Sub Private Sub setup_button_Click() ' Assume this button is clicked by the user when he wants to connect to the ' device and configure event handling Set dev = New Eth32 ' Set up error handling for this routine On Error GoTo myerror ' NOTE: Substitute the IP address or DNS name of your device here. dev.Connect "192.168.1.100" ' If there is a pushbutton connected between Port 0, bit 0 and ground, ' then we can provide an internal pullup causing it to float high by ' doing: dev.OutputBit 0, 0, 1 ' Look for events on Port 0, bit 0. dev.EnableEvent EVENT_DIGITAL, 0, 0, 100 Exit Sub myerror: MsgBox "Error communicating with the ETH32: " & dev.ErrorString(Err.Number) End Sub
Event Handler Section, DisableEvent Method