<< Click to Display Table of Contents >> Navigation: Working with CanEasy > Automation in CanEasy > VBA > Tutorials > Events Tutorial > Event Example: Send Message |
The following tutorial shows how the sending of a message is identified. It is based on the data base created in the tutorial.
Create a new VBA project by selecting New from the context menu of the VBA tree node. Save the project as “Send message”.
Next, create a class module in the VBA editor.
Enter the following code:
Public WithEvents Msg as message
Public Sub Assign(oMsg as message) Set Msg = oMsg 'Set filter for transmission confirmation Msg.FilterTxConfirm "", "", PositionNeutral, False Msg.TxConfirmEvents = True End Sub |
Msg.TxConfirmEvents = True signals the COM interface to activate the event.
Next, select Msg in the left selection list and OnTxConfirm in the right to create the body of the event handling routine:
Add the following code in the created routine Msg_OnTxConfirm:
Private Sub Msg_OnTxConfirm(ByVal data As CanEasy.ITransmissionData) Debug.Print "message sent" End Sub |
Next, enter the following code in the standard module:
Public Pedal As Class1
Sub Testrun() Dim ECU As ControlUnit Set Pedal = New Class1
Set ECU = Database.Busses("CAN1").ControlUnits("ECU1") Pedal.Assign ECU.Messages("MSG1")
StartSimulation 'start simulation Pedal.Msg.Send 'send message CanEasyApplication.Sleep 1000 'wait for 1 second Pedal.Msg.Send 'send message CanEasyApplication.Sleep 1000 'wait for 1 second StopSimulation 'stop simulation End Sub |
It is important, to create an instance of the Class1 class with the keyword New.
Press CTRL+G to display the direct area and run function Testrun. The instructions in the event handling routine will be executed and displayed in the direct area window (the message "Message sent" will appear twice).