Code: Select all
'------------------------------------------------------------------------------
'FILE DESCRIPTION: Keine Beschreibung angegeben.
'------------------------------------------------------------------------------
Sub SimpleProtocol()
'DESCRIPTION: This MACRO send a request to a ECU and wait for n-Times for any answer from the Bus
' The User could use the Input Box to define the value of the retry counter
'
' To view the output messages of this macro, open the Output Window and select the "Macro" tab
Dim UseConn, conn
' Find the first enabled connection in the project that uses the CAN protocol
Set UseConn = Nothing
For Each conn In Connections
If conn.IsEnabled And conn.Protocol = peProtocolCAN Then
Set UseConn = conn
Exit For
End If
Next
If UseConn Is Nothing Then
MsgBox "This Project does not contain any enabled CAN connections!"
MsgBox "Please check your Connection Box if your Hardware is connected."
Exit Sub
End If
' Create a new client and connect it to the same Net that the
' found connection uses
Dim MyClient, PcanConn
Set MyClient = CreateObject("PCAN3.PCANClient")
MyClient.Name = "SimpleProtocol"
MyClient.Device = UseConn.Device
Set PcanConn = MyClient.Connections.Add(UseConn.CommunicationObject.NetName)
' Set Filter, so that we only receive in this script ID from 0x083 to 0x18B
PcanConn.RegisterMsg &H083, &H18B, False, False
' i ist the counter how often we receive the ID 0x083
i = 0
' count is the loop counter - changed by a input box value
count = 0
Dim RcvMsg 'we use this structure to Receive CAN Frames
Dim SendMsg 'we use this structure to Transmit CAN Frames
Set RcvMsg = MyClient.Messages.Add
' Now create and initialize a new transmit message
Set SendMsg = MyClient.Messages.Add
timestamp = MyClient.GetSystemTime
PrintToOutputWindow "Macro running"
strcount = InputBox("Loop Counter", "SimpleProtocol")
If strcount <> "" Then
count = CLng(strcount)
Else
count = 5
End If
' Here we start!
' Now we add the needed DataBytes to the CAN Request Message
' For example Byte0=0x10 and DatByte1=0x11, ID=0x83, DLC = 2, STD CAN Frame (11Bit)
With SendMsg
.ID = &H083
.DLC = 2
.MsgType = pcanMsgTypeStandard
.Data(0) = &H10
.Data(1) = &H11
End With
' here we start the Loop! -- running until the conter reached the limit
Do
' Send the defined CAN ID - also could be done in a SUB - thats up to you
SendMsg.Write PcanConn, timestamp
PrintToOutputWindow "CAN ID send"
' Wait for a received message
' sleep 5 milisec. to give the CAN Bus a chance to transmit the ID and the ECU to answer :-)
Wait(5)
if RcvMsg.Read Then ' Read next CAN Date (keep in mind Filter is set!) from Queue
' check if CAN Frame received
while RcvMsg.LastError = pcanErrorOk
' Check if ID = 0x083
If RcvMsg.ID = &H083 Then
' print out text
PrintToOutputWindow "ID 0x083 Received !"
i = i + 1
' copy the received Databytes to the Output Window
for a=0 to RcvMsg.DLC
PrintToOutputWindow "Data" & CStr(a) & ": " & CStr(RcvMsg.Data(a))
next ' next a
' to stop the loop we set count now to 1 - so next decremt it will be 0
count = 0
End If ' Endif ID OK
' add a delay to wait if more Data come in
Wait(5)
' read the queue again
RcvMsg.Read
Wend ' Endif no Error
End If ' Endif Read OK
' Loop until Message ID 0x101 receiced
count = count -1
Wait(200) ' here we wait 200ms until we try the next resend - without a delay at this point, all CAN Frames will be send within mSeconds
Loop While count > 0
PrintToOutputWindow "Finished, " & CStr(i) & " messages with ID 0x083 received !"
End Sub
