ID : 5525
Receiving the Provider Event
Outline of the Provider Event
Provider event is a mechanism to inform a result and status of a procedure on the provider to the main task at arbitrary timings, if the procedure is not synchronized with the main task.
PacScript provides a dedicated statement to receive the provider event at arbitrary timing.
Preparation to Receive Provider Events
To receive a provider event, set an event name to receive and a target method by AddHandler statement.
Standby for Provider Events Receiving
Once an area to receive the event is entered, the main task keeps waiting by using a loop command, which is shown below, until the event is received.
Do
Delay 10
If termination condition Then Exit Do
Loop
Procedure for Receiving Provider Events
When the provider issues an event, a method which has been entered by AddHandler is automatically called. By using arguments which are passed to this method, carry out the necessary procedure.
Sub OnMessage (ByVal Sender As Object, ByVal Args As Variant)
There are two arguments used in this method.
- Sender
- A reference of an object which has issued an event is inserted.
- Args
- A parameter at the event issue is inserted.
The value of Arg depends on the object which issues event. Refer to your provider manual.
Procedure for Receiving Provider Event and Main Task
The main procedure (other than OnMessage) in the task checks event occurrence at the time of going to next line. If an event has been issued, method to receive events (OnMessage) is called. When the procedure of method to receive events has completed, the next line on the main procedure is carried out.
To run the main procedure in parallel with method to receive event, carry out these procedures in separate tasks.
Stop of Provider Events Receiving
Soon after the event receiving becomes unnecessary, call RemoveHandler to end the procedure.
RemoveHandler object
For an object mentioned here, specify the Object which has been entered by AddHandler.
Sample Program for Receiving Provider Event
This sample program shows how to receive data.
The data is received when the QR code reader reads the QR code by using the QR code provider. This sample completes by reading three codes.
Dim g_Counter As Integer = 0 'Number of data to receive
Sub Main()
Dim ctrl As Object
ctrl = Cao.AddController( "QR", "CaoProv.DENSO.QRCode", "",
"Conn=com:6:38400:N:8:1, Mode=5, Protocol=0:0:0" )
'Specify the event name and area to receive
AddHandler ctrl, "OnMessage", OnMessage
g_Counter = 0
'Loop command to keep waiting event receiving
Do
Delay 10
If g_Counter >= 3 Then Exit Do
Loop
'Stop event receiving
RemoveHandler ctrl
End Sub
'Event Handler for OnMessage
Sub OnMessage( ByVal Sender As Object, ByVal Args As Variant )
Dim msg as object
msg = Args(0) '::= CaoMessage object
PrintDbg "" & Time & " - CODE = [" & msg.Value & "]"
g_Counter = g_Counter + 1
End Sub
ID : 5525