Help creating Macro

Professional Windows® software to communicate with CAN and CAN FD busses and its add-ins: Plotter, CANdb Import, Instruments Panel, and J1939
Post Reply
Onour
Posts: 4
Joined: Thu 25. Nov 2021, 11:07

Help creating Macro

Post by Onour » Thu 25. Nov 2021, 11:45

Hello,

I'm new to the software and I'm trying to create a simple macro that send back the received data from the CAN bus.
Do you already have an example or tutorial on how to write a macro and run it ?

Thank you

M.Maidhof
Support
Support
Posts: 1366
Joined: Wed 22. Sep 2010, 14:00

Re: Help creating Macro

Post by M.Maidhof » Thu 25. Nov 2021, 14:58

Hi,

please have a look in the help of PCAN-Explorer 6. See Refernce->Standardmacros for examples on the use of Standard Macros. With a Standard Macro you have very simple commands like send, wait or waitid etc. with which you should be able to handle your task. If you like to get some sample for VBmacros, please send us an email to our support email address with your license ID of your PCAN-Explorer 6 license. We will than send you some examples for VB Macros by email.

regards

Michael

Onour
Posts: 4
Joined: Thu 25. Nov 2021, 11:07

Re: Help creating Macro

Post by Onour » Fri 26. Nov 2021, 09:22

Thank you for the reply.
It has helped me understand a lot more.

But I couldn't see a way to "save" received data to send them back. I can check the data but can I store them ? (with a .mcr file)

Thank you

M.Maidhof
Support
Support
Posts: 1366
Joined: Wed 22. Sep 2010, 14:00

Re: Help creating Macro

Post by M.Maidhof » Fri 26. Nov 2021, 14:13

Hi,

with a standard macro (*.mcr) you cannot handle a storage of data. For this purpose please use the VBmacros.

regards

Michael.

Onour
Posts: 4
Joined: Thu 25. Nov 2021, 11:07

Re: Help creating Macro

Post by Onour » Tue 30. Nov 2021, 11:08

Thank you.

I'm trying now to send back the received values. Should I use AutoTransmit function or put the data in the TransmitMessages.
I used one of your code that you posted in another topic:

Code: Select all

'------------------------------------------------------------------------------
'FILE DESCRIPTION: 
'------------------------------------------------------------------------------
Option Explicit

SUB Check_ID_By_ReceiveList()
PrintToOutputWindow("START")

Dim send_msg, msg, OldCount, str, i
Set send_msg = TransmitMessages.Add

OldCount = 0
While TRUE
	For Each msg In Connections.ReceiveMessages
		If msg.ID = 2030 AND (msg.EventCount <> OldCount)Then ' ID and Counter is changed...
 			For i = 0 To msg.DataLength-1
      			str = str & Hex(msg.Data(i)) & " "
   			Next
   			
			PrintToOutputWindow("Counter: " & msg.EventCount & " ID= " & Hex(msg.ID) & "  Data= " & str)
  			OldCount = msg.EventCount ' Save for the next loop
  			
			With send_msg
				If Connections.Count > 0 Then _
					Set .Connection = Connections(1)
				'.BeginUpdate
				.ID = &h755
				.Data(0) = msg.Data(0)
				.Data(1) = msg.Data(1)
				.Data(2) = msg.Data(2)
				.Data(3) = msg.Data(3)
				.Data(4) = msg.Data(4)
				.Data(5) = msg.Data(5)
				.Data(6) = msg.Data(6)
				.Data(7) = msg.Data(7)
			End With
 		End if
 	str=Null
	Next 
Wend
End Sub
Both of the methods won't work. Isn't the data put in TransmitMessages automaticcly transmitted ?
The goal here is to send back the data from msg with another ID.

Thank you for the help

M.Heidemann
Sales & Support
Sales & Support
Posts: 746
Joined: Fri 20. Sep 2019, 13:31

Re: Help creating Macro

Post by M.Heidemann » Tue 30. Nov 2021, 12:27

Hello,

In this case you have created a new message object and allocated
values to its variables, however you'll need to explicitly send the message object.
Auto transmit only works signal based, so if you change data in a newly created message
object, it will have no influence on the signal data, therefore no auto-transmit will occur.

How you proceed is up to your personal preference, if you want to specifically change a certain signal
upon reception of a message, you should work signal based. Changing the signal value is quite easy,
you only need to obtain the signal from the Signals collection and assign a new value to it, if auto-transmit
is enabled this will send the message that the singal is used in with the updated signal data, example:

Code: Select all

Sub ChangeSignalValue()

Dim Signal

'Get signal form the Signals collection.
Set Signal = Signals("MySignal")

'Value is changed, auto-transmit will send message.
Signal.Value = 2

End Sub

Alternatively you could proceed with your code and rewrite the message bytes manually,
you will however have to send the message by using the function "Write":
2021-11-30 12_25_17-Window.png
2021-11-30 12_25_17-Window.png (50.61 KiB) Viewed 243 times
So in that case you would have to append this to your macro:

Code: Select all

'------------------------------------------------------------------------------
'FILE DESCRIPTION: 
'------------------------------------------------------------------------------
Option Explicit

SUB Check_ID_By_ReceiveList()
PrintToOutputWindow("START")

Dim send_msg, msg, OldCount, str, i
Set send_msg = TransmitMessages.Add

OldCount = 0
While TRUE
	For Each msg In Connections.ReceiveMessages
		If msg.ID = 2030 AND (msg.EventCount <> OldCount)Then ' ID and Counter is changed...
 			For i = 0 To msg.DataLength-1
      			str = str & Hex(msg.Data(i)) & " "
   			Next
   			
			PrintToOutputWindow("Counter: " & msg.EventCount & " ID= " & Hex(msg.ID) & "  Data= " & str)
  			OldCount = msg.EventCount ' Save for the next loop
  			
			With send_msg
				If Connections.Count > 0 Then _
					Set .Connection = Connections(1)
				'.BeginUpdate
				.ID = &h755
				.Data(0) = msg.Data(0)
				.Data(1) = msg.Data(1)
				.Data(2) = msg.Data(2)
				.Data(3) = msg.Data(3)
				.Data(4) = msg.Data(4)
				.Data(5) = msg.Data(5)
				.Data(6) = msg.Data(6)
				.Data(7) = msg.Data(7)
				.Write 0
			End With
 		End if
 	str=Null
	Next 
Wend
End Sub

Best Regards

Marvin

Onour
Posts: 4
Joined: Thu 25. Nov 2021, 11:07

Re: Help creating Macro

Post by Onour » Wed 1. Dec 2021, 10:09

Thank you for your help, this is what I needed.
Now I understand how to develop macros.

Post Reply