Help writing 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
RiccardoTa
Posts: 4
Joined: Mon 24. Jun 2024, 15:01

Help writing Macro

Post by RiccardoTa » Tue 2. Jul 2024, 11:26

Hello everyone, I need help solving a problem with a macro I wrote:

Code: Select all

Sub automationTest2() 'Automation for bus 1
    Dim msg, msg1
    ' Initialize the CAN message
    For i = 0 To 1
        Set msg = TransmitMessages.Add

        With msg
            If Connections.Count > 0 Then _
                Set .Connection = Connections(1)
            .ID = &Hxxxxxxxxx 'ID transmit
            .DataLength = 8
            '(DATA)
            msg.CycleTime = 500 'Cycle time
            msg.IsPaused = False  
        End With

        Set msg1 = TransmitMessages.Add

        With msg1
            If Connections.Count > 0 Then _
                Set .Connection = Connections(2)
            .ID = &Hxxxxxxxx 'ID transmit
            .DataLength = 8
            '(DATA)
            msg1.CycleTime = 500 'Cycle time
            msg1.IsPaused = False
        End With 

        Wait 25000

        With msg
            If Connections.Count > 0 Then _
                Set .Connection = Connections(1)
            .ID = &Hxxxxxxxx 'ID transmit
            .DataLength = 8
            '(DATA)
            msg.CycleTime = 500 'Cycle time
            msg.IsPaused = False  
        End With

        With msg1
            If Connections.Count > 0 Then _
                Set .Connection = Connections(2)
            .ID = &Hxxxxxxxx 'ID transmit
            '(DATA)
            msg1.CycleTime = 500 'Cycle time
            msg1.IsPaused = False
        End With 

        Wait 25000
    Next
    msg.IsPaused = True 'Stops cycle
    msg1.IsPaused = True
End Sub
Now, the problem is the following:
During the second "for" cycle, the script re-sends the instructions with data, and in the PCAN receive/transmit section, I see the previous two frames (still running) along with the current two frames. In the last cycle, it repeats again, queuing the same two frames repeatedly like this:

```
frame 1 bus 1 (running)
frame 2 bus 2 (running)
frame 1 bus 1 (running)
frame 2 bus 2 (running)
frame 1 bus 1 (running)
frame 2 bus 2 (running)
```
How can I solve this problem? What am I doing wrong? i am still inexperienced with macros and I need each "for" cycle to only modify the first two frames, and not add the same two instructions again, maintaining the previous ones as active.
I hope I've explained well. I'm available for additional information. Thanks.

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

Re: Help writing Macro

Post by M.Heidemann » Tue 2. Jul 2024, 12:32

Hello,

You are adding the messages each loop, if you need to get rid of them, use TransmitMessages.Clear()

Otherwise youll just fill up the Tx-Messages queue

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

Post Reply