Receiving Messages and reading their data

Comprehensive CAN monitor for Windows® and its add-ins: Plotter, CANdb Import, Instruments Panel, and J1939
User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: Receiving Messages and reading their data

Post by PEAK-Support » Tue 19. May 2015, 17:13

Here, how would implement this:

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
Problem could be if a lot of other Messages are in the queue (all that pass the filter), the while wend run forever :(
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

jnota
Posts: 20
Joined: Mon 18. May 2015, 17:32

Re: Receiving Messages and reading their data

Post by jnota » Tue 19. May 2015, 17:35

I'll try your script.
Meanwhile I answered your previous post.
My external node just sends a message with ID 0x083 when some error occurs (CAN Heartbeat Error, etc...) and that's what I'm trying to check out:
To what point can my external device support receiving my messages without answering with this ID 0x083.
But it is weird because If the delay between the messages I send is higher, more received messages I miss...
CAN2.jpg
CAN2.jpg (21.13 KiB) Viewed 6496 times
Just 4 messages were detected.

CAN.jpg
CAN.jpg (22.86 KiB) Viewed 6496 times
... but in fact 36 messages were received.

jnota
Posts: 20
Joined: Mon 18. May 2015, 17:32

Re: Receiving Messages and reading their data

Post by jnota » Tue 19. May 2015, 17:53

The problem remains with this new script.
jnota wrote:I'll try your script.
Meanwhile I answered your previous post.
My external node just sends a message with ID 0x083 when some error occurs (CAN Heartbeat Error, etc...) and that's what I'm trying to check out:
To what point can my external device support receiving my messages without answering with this ID 0x083.
But it is weird because If the delay between the messages I send is higher, more received messages I miss...
CAN2.jpg
Just 4 messages were detected.

CAN.jpg
... but in fact 36 messages were received.

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

Re: Receiving Messages and reading their data

Post by PEAK-Support » Wed 20. May 2015, 14:20

This could be - i do not know how often you unit send the CAN Frames out - if you study my sample, you will see that if RcvMsg.ID = &H083 , the outer loop will stop if within the next 5 ms no other answer follows...
(because count is set to 0!)

Code: Select all

 ' add a delay to wait if more Data come in 
 Wait(5)
Play with the timing, or optimize for time outs....
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

jnota
Posts: 20
Joined: Mon 18. May 2015, 17:32

Re: Receiving Messages and reading their data

Post by jnota » Wed 20. May 2015, 15:22

Ok, I'll try to play with timing and to see what happens.
Thanks!

Post Reply