Toggle bit in macro

Comprehensive CAN monitor for Windows® and its add-ins: Plotter, CANdb Import, Instruments Panel, and J1939
Post Reply
Stefano Mossina
Posts: 1
Joined: Wed 18. Nov 2020, 11:06

Toggle bit in macro

Post by Stefano Mossina » Wed 18. Nov 2020, 11:15

Hi all,

I am controlloing an inverter with a CAN message. The message should include a toggle bit (which is toggled at every new message sent). I tried to this in two ways:

1) Define a message in my TransmitList with 100ms timer and use a macro to insert the toggle bit.
2) Use a macro toggling the bit and sending the message automatically.

Both my macros use the instruction "Wait 100" in an endless while loop to time, respectively, the bit toggling and the message sending.

With method 1) I encounter the issue that the macro in not synched with the transmit list and this results in the bit not getting toggled at some random point in time
With method 2) I encounter the problem that if I do something else on my laptop the macro gets stuck for a while and the message is not sent in 100ms (and timeout occurs on inverter).

My question is: how can I synch the macro with the transmit list? I mean, when the transmit list is "building" the message to be sent, I want my macro to jump into there and insert the correct toggle bit.
Alternatively, I can I perfectly time the macro and its message sending with my laptop system time?

Basically I would like my macro to be executed as "compiled code" by my laptop's microprocessor and not as an "interpreted" code run over system time..

Is that possible?

Thanks for your answers!

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

Re: Toggle bit in macro

Post by PEAK-Support » Wed 18. Nov 2020, 14:19

A combination of sending with a Transmit List and a script is not possible because you could not sync both events.
You need to send the message only from the VBS script - that should work. In PE6 we offer a callback lib which is the better solution. But PE5 is out of maintance.
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

bkiepke
Posts: 17
Joined: Fri 22. Oct 2021, 13:31

Re: Toggle bit in macro

Post by bkiepke » Fri 22. Oct 2021, 13:57

Hi,

as I stuck to PE5 can you give an example of how to achieve this with a .pem?

I need to provide a Control Panel to a collegue of mine and need to send a command, containing a toggle bit, to a drive periodically. I have a .sym-file created for the motor controller.
I also created a message in the transfer list for the command.

How is the .pem now triggered every cycle to change the toggle-bit?

Here is the script I have so far:

Code: Select all

'------------------------------------------------------------------------------
'FILE DESCRIPTION: Send DriveCommand-messages
'------------------------------------------------------------------------------

Option Explicit

Const DriveCommandID = &H160

Sub DriveCommandSend()
	' Toggle security-bit in every DriveCommand-message
	Dim IsRunning
	Dim SecurityToggleBit

	Dim DriveCommand
	Set DriveCommand = Signals("DriveCommand_DC_0")

	IsRunning = True
	SecurityToggleBit = 0

	Do While IsRunning
		If SecurityToggleBit = 0 Then
			SecurityToggleBit = 1
		Else
			SecurityToggleBit = 0
		End If
		Signals("DriveCommand_DC_0.Bit_0_Toggle_Security_Bit").Value = SecurityToggleBit
		SymbolsManager("Controller").Item("DriveCommand_DC_0").Send(1)
		' wait 20 ms, before next cycle
		WScript.Sleep 20
	Loop
End Sub
My Controller.sym contains:

Code: Select all

[DriveCommand_DC_0]
ID=160h	// Node 0
DLC=8
CycleTime=20 -p
Var=AD1_Throttle_Torque_Speed unsigned 0,16 -m /u:% /f:0.0244140625
Var=AD2_BrakePedal unsigned 16,16 -m /u:% /f:0.0244140625
Var=AD3 signed 32,16 -m /u:% /f:0.0244140625
Var=DigIn_1 bit 48,1
Var=DigIn_2 bit 49,1
Var=DigIn_3 bit 50,1
Var=DigIn_4 bit 51,1
Var=DigIn_5 bit 52,1
Var=DigIn_6 bit 53,1
Var=DigIn_7 bit 54,1
Var=Bit_0_Toggle_Security_Bit bit 56,1
My Controller.xmt contains:

Code: Select all

; Columns descriptions:
; ~~~~~~~~~~~~~~~~~~~~~
;+Bus
;|  +Message ID
;|  |         +Reserved
;|  |         |  +Cycle time in ms (0=manual)
;|  |         |  |     +Length of message
;|  |         |  |     |    +Frame type: D)ata or R)emote request
;|  |         |  |     |    | +Message data
;|  |         |  |     |    | |
 1  160h      -  20    8    D 00h 00h 00h 00h 00h 00h 00h 00h  Paused
Thanks in advance

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

Re: Toggle bit in macro

Post by PEAK-Support » Fri 22. Oct 2021, 14:11

With the PE5 this will not possible in this way. As long as you need to send the ID because of changing some values, you will not be able to sync this with the toggle of the BIT.
For this case we have implemented the CallBack Libs which are availbale in PE6 (which was released over 5 years ago)
Your used PE5 was released in 2009 - time to upgrade....
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

bkiepke
Posts: 17
Joined: Fri 22. Oct 2021, 13:31

Re: Toggle bit in macro

Post by bkiepke » Fri 22. Oct 2021, 15:15

Thank you for your answer - I know we use a plain old version.

However I basically managed it. But I got a cycle time of 500ms which I have to evaluate if this matters for my use case. Anyway to speed things up?

Here is my "solution", which can be started via a button within the Control Panel

Code: Select all

'------------------------------------------------------------------------------
'FILE DESCRIPTION: Send DriveCommand-messages
'------------------------------------------------------------------------------
Option Explicit

Dim IsRunning

Sub DriveCommandSend()
	' Toggle security-bit in every DriveCommand-message
	' Can be stopped by "Stop Macro Button of PCANExplorer"

	Dim SecurityToggleBit

	Dim DriveCommand
	Set DriveCommand = Signals("DriveCommand") ' Enter name of symbol here, defined as in .sym-file
	
	Dim Symbols
	Set Symbols = SymbolsManager("DriveProtocol") ' Use Title as defined in .sym-file here
	
	If Symbols.Count = 0 Then
		PrintToOutputWindow "Item not in SymbolsManager! Load .sym file first"
	Else
		IsRunning = True
		SecurityToggleBit = 0

		Do While IsRunning
			If SecurityToggleBit = 0 Then
				SecurityToggleBit = 1
			Else
				SecurityToggleBit = 0
			End If
			Signals("DriveCommand.Bit_0_Toggle_Security_Bit").Value = SecurityToggleBit
			Symbols.Item("DriveCommand").DefaultMultiplexer.Send(1)
			' delay message for some time, something about 0.5s - Anyway to speed it up?
			Wait 20
		Loop
	End If
End Sub

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

Re: Toggle bit in macro

Post by PEAK-Support » Fri 22. Oct 2021, 15:21

Could work, if you change the PC, have differnt Busload etc. it will not work exact in the same timing anymore.
Problem is that you are not able to synchronize it and so you will run into problems after a while (drifting..)
The CallBack Lib simply "hook" into the outgoing queue and allow to change single data bit / bytes when they "leave" the PE6 - before they were pushed to the driver queue.
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

bkiepke
Posts: 17
Joined: Fri 22. Oct 2021, 13:31

Re: Toggle bit in macro

Post by bkiepke » Fri 22. Oct 2021, 23:22

Understood thank you.

I trace it for a while and I keep wondering while the message is send twice within 5ms? Also I looks like I misunderstood the timing. I claims that it sends the message every 0.5ms. Busload is less than 5% when only this message is send in a loop of two PCANUSB.

Changed the send line to

Code: Select all

SymbolsManager("DriveCommandProtocol").Item("DriveCommand").DefaultMultiplexer.Send Bus, peSendOnce
but that didn't change the "two messages" issue

Any idea?

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

Re: Toggle bit in macro

Post by PEAK-Support » Mon 25. Oct 2021, 08:22

As we do not have your complete project, it is hard to say. If you add a signal to the transmit list, it will be send with the cycle time that is defined by the setup.
But you also could add this 2 or 3 times - then you send it x times. You could send us the complete project to our support and we could talke a look at it.

We recommend to switch to PE6 and use the CallBack DLL - this feature was build exact for this post processing of CAN Frames to implement rolling counter (your Bit toggeling is exact this ..) or needed crc calculation setc.
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

bkiepke
Posts: 17
Joined: Fri 22. Oct 2021, 13:31

Re: Toggle bit in macro

Post by bkiepke » Tue 26. Oct 2021, 07:06

Good morning,

I found that last issue. It looks like that setting a "Value" using Signals already send the message once. So no need to additionally call "Send" using the SymbolsManager.
No need to define a .xmt-file because the script already handle this.

My project now consists of the following files:
.ipf to have some controls
.sym to have the communication interpreted
.pem to have the toggling and message sending

Here is my complete .pem-script:

Code: Select all

'------------------------------------------------------------------------------
'FILE DESCRIPTION: Send DriveCommand-messages
'------------------------------------------------------------------------------
Option Explicit

Sub DriveCommandSend()
	' Toggle security-bit in every DriveCommand-message
	' Can be stopped by "Stop Macro Button of PCANExplorer"
	' Can be started by "Start Macro Button of PCANExplorer"

	Dim SecurityToggleBit
	Dim Bus
	
	SecurityToggleBit = 0
	Bus = 1
	
        Do While True
    	        Wait 10
    	        If SecurityToggleBit = 0 Then
			SecurityToggleBit = 1
		Else
			SecurityToggleBit = 0
		End If
		Signals("DriveCommand_DC_0.Bit_0_Toggle_Security_Bit").Value = SecurityToggleBit
		Wait 10
	Loop
End Sub
Here is the trace (I am totally wrong when it came to interpret the timing :oops: )

Code: Select all

Zeit (diff.) | Bus | Typ | ID / Symbol                | DLC | Daten
0,0000       | 1    | Tx   | DriveCommand_DC_0 | 8     | 00 00 00 00 00 00 00 00
0,0008       | 2    | Rx   | 160h                          | 8     | 00 00 00 00 00 00 00 00
0,0203       | 1    | Tx   | DriveCommand_DC_0 | 8     | 00 00 00 00 00 00 00 01 
0,0008       | 2    | Rx   | 160h                          | 8     | 00 00 00 00 00 00 00 01
...
0,0217       | 1    | Tx   | DriveCommand_DC_0 | 8     | 00 00 00 00 00 00 00 01 
0,0008       | 2    | Rx   | 160h                          | 8     | 00 00 00 00 00 00 00 01
...
0,0197       | 1    | Tx   | DriveCommand_DC_0 | 8     | 00 00 00 00 00 00 00 00 
0,0008       | 2    | Rx   | 160h                          | 8     | 00 00 00 00 00 00 00 00
...
There is some jitter, as expected, but it is totally ok for this project as the message needs to be send within 150ms.

Thank you for staying with me and the help anyway.
I am triggering the process to update to PE6 for some time now, but as I am not the decision-maker it may will take some more time ...

Post Reply