Macro Exiting FOR Loop

Macro Exiting FOR Loop
none 0.0 0
  • HMI Model: cMT2078X Simulation
  • EasyBuilder Pro Version: 6.10.01.359

I’m setting up a Macro to generate an initial test data pattern for a project using the X-Y plot element. Theoretically I’m targetting 1000 points, although we’ll adjust to whatever it can actually handle. When I run the following macro, I get up to the first set of Datasets inside the For loop and past them, but that’s it, it quits after executing the Dataset command, no alerts I can see.

I get the TRACE printout of TRACE(“n_start = %d”, n_start) once and then nothing. I’ve confirmed the 100 word dataset is writing to memory.

short DataTime[100]
float DataCapture[100]
float DataLL[100]
float DataUL[100]
float X_MinMax[2]
//float X_Max
short T_MinMax[2]
//float T_Max
int DataCount = 1000
int Refresh = 3
int StepEnd = 997
int T_Increment = 10
int T_Val = 0
int n
int p = 0
int D_Step = 0
int n_start = 0
float r = 0
float k = 0

//Generates a test pattern to test the display
macro_command main()



X_MinMax[0] = 0
X_MinMax[1] = 0

T_MinMax[0] = 0
T_MinMax[1] = 0

TRACE("StepEnd = %d", StepEnd)

for n = 0 to StepEnd 
	//Generate an graph
	if n <500 then
		r= r + 1
	else
		r= r - 1
	end if
	k = k + r
	DataCapture[p] = k
	if DataCapture[p] > X_MinMax[1] then
		X_MinMax[1] = DataCapture[p]
	end if
	if DataCapture[p] < X_MinMax[0] then
		X_MinMax[0] = DataCapture[p]
	end if
	DataTime[p] = T_Val
	if DataTime[p] > T_MinMax[1] then
		T_MinMax[1] = DataTime[p]
	end if
	T_Val = T_Val + T_Increment
	DataUL[p] = DataCapture[n] * 1.1
	if DataUL[p] > X_MinMax[1] then
		X_MinMax[1] = DataUL[p]
	end if
	if DataUL[p] < X_MinMax[0] then
		X_MinMax[0] = DataUL[p]
	end if
	DataLL[p] = DataCapture[n] * 0.9
	if DataLL[p] > X_MinMax[1] then
		X_MinMax[1] = DataLL[p]
	end if
	if DataLL[p] < X_MinMax[0] then
		X_MinMax[0] = DataLL[p]
	end if
	p = p + 1
	TRACE("n = %d", n)
	if ((p >= 100) or (n == StepEnd)) then

				SetDataEx(DataCapture[0], "Local HMI", LW, 2004 + n_start, p)//"PROFILE_1_DATA"
				//SetData(DataTime[0], "Local HMI", RW, 1000, 100)
				SetDataEx(DataTime[0], "Local HMI", RW, 0002 + n_start, p)//"PROFILE_TIME_1"
				SetDataEx(DataLL[0], "Local HMI", RW, 1004 + n_start, p)//"PROFILE_1_LL"
				SetDataEx(DataUL[0], "Local HMI", RW, 3004 +n_start, p)//"PROFILE_1_HL"

		//Incremental so we don't need to hold all the data at the same time
		
		
		n_start = n_start + 100
		SetDataEx(n_start, "Local HMI", "Profile_LineCount", 1)
		p = 0
		D_Step = D_Step + 1
		TRACE("n_start = %d", n_start)
	end if
next

//Limits
TRACE("X Min = %f", X_MinMax[0])
TRACE("X Max = %f", X_MinMax[1])
TRACE("T Min = %d", T_MinMax[0])
TRACE("X Max = %d", T_MinMax[1])

SetDataEx(X_MinMax[0], "Local HMI", LW, 2000, 2)//"PROFILE_1_DATA"
SetDataEx(T_MinMax[0], "Local HMI", RW, 0000, 2)//"PROFILE_TIME_1"
SetDataEx(X_MinMax[0], "Local HMI", RW, 1000, 2)//"PROFILE_1_LL"
SetDataEx(X_MinMax[0], "Local HMI", RW, 3000, 2)//"PROFILE_1_HL

SetData(Refresh, "Local HMI", "Profile_Refresh", 1)


end macro_command

Hi @grodberg

I hope you’re doing well. Could you please share some screenshots of your project, especially the X-Y Plot element and any other key details that would help us understand your setup?

Here’s the configuration of the X Y plot. The Macro code is triggered by the Graph button press then I hit Refresh to update the data (I’m taking some manual control while in initial development). This way I can make sure I don’t call the macros multiple times.



I was also having XY graph issues (1st 2 entries, the HL and LL data series in RW memory were acting like they were on some weird log graph while the data series in LW memory was acting correctly) but I moved them (tried LW memory and a different area of RW memory) and now have reset them back to their original memory locations and it’s not repeating right now.

the entire trace log is this. If I change the Step End value to 97, it will complete the loop without issue. If I remove the DataSet/DataSetEX instead, same result on loop completion. I’ve tried both DataSet types.

[11:04:15] [ID 005, Line 32] StepEnd = 997

[11:04:15] [ID 005, Line 71] n = 0

[11:04:15] [ID 005, Line 71] n = 1

[11:04:15] [ID 005, Line 71] n = 2

[11:04:15] [ID 005, Line 71] n = 3

[11:04:15] [ID 005, Line 71] n = 4

[11:04:15] [ID 005, Line 71] n = 5

[11:04:15] [ID 005, Line 71] n = 6

[11:04:15] [ID 005, Line 71] n = 7

[11:04:15] [ID 005, Line 71] n = 8

[11:04:15] [ID 005, Line 71] n = 9

[11:04:15] [ID 005, Line 71] n = 10

[11:04:15] [ID 005, Line 71] n = 11

[11:04:15] [ID 005, Line 71] n = 12

[11:04:15] [ID 005, Line 71] n = 13

[11:04:15] [ID 005, Line 71] n = 14

[11:04:15] [ID 005, Line 71] n = 15

[11:04:15] [ID 005, Line 71] n = 16

[11:04:15] [ID 005, Line 71] n = 17

[11:04:15] [ID 005, Line 71] n = 18

[11:04:15] [ID 005, Line 71] n = 19

[11:04:15] [ID 005, Line 71] n = 20

[11:04:15] [ID 005, Line 71] n = 21

[11:04:15] [ID 005, Line 71] n = 22

[11:04:15] [ID 005, Line 71] n = 23

[11:04:15] [ID 005, Line 71] n = 24

[11:04:15] [ID 005, Line 71] n = 25

[11:04:15] [ID 005, Line 71] n = 26

[11:04:15] [ID 005, Line 71] n = 27

[11:04:15] [ID 005, Line 71] n = 28

[11:04:15] [ID 005, Line 71] n = 29

[11:04:15] [ID 005, Line 71] n = 30

[11:04:15] [ID 005, Line 71] n = 31

[11:04:15] [ID 005, Line 71] n = 32

[11:04:15] [ID 005, Line 71] n = 33

[11:04:15] [ID 005, Line 71] n = 34

[11:04:15] [ID 005, Line 71] n = 35

[11:04:15] [ID 005, Line 71] n = 36

[11:04:15] [ID 005, Line 71] n = 37

[11:04:15] [ID 005, Line 71] n = 38

[11:04:15] [ID 005, Line 71] n = 39

[11:04:15] [ID 005, Line 71] n = 40

[11:04:15] [ID 005, Line 71] n = 41

[11:04:15] [ID 005, Line 71] n = 42

[11:04:15] [ID 005, Line 71] n = 43

[11:04:15] [ID 005, Line 71] n = 44

[11:04:15] [ID 005, Line 71] n = 45

[11:04:15] [ID 005, Line 71] n = 46

[11:04:15] [ID 005, Line 71] n = 47

[11:04:15] [ID 005, Line 71] n = 48

[11:04:15] [ID 005, Line 71] n = 49

[11:04:15] [ID 005, Line 71] n = 50

[11:04:15] [ID 005, Line 71] n = 51

[11:04:15] [ID 005, Line 71] n = 52

[11:04:15] [ID 005, Line 71] n = 53

[11:04:15] [ID 005, Line 71] n = 54

[11:04:15] [ID 005, Line 71] n = 55

[11:04:15] [ID 005, Line 71] n = 56

[11:04:15] [ID 005, Line 71] n = 57

[11:04:15] [ID 005, Line 71] n = 58

[11:04:15] [ID 005, Line 71] n = 59

[11:04:15] [ID 005, Line 71] n = 60

[11:04:15] [ID 005, Line 71] n = 61

[11:04:15] [ID 005, Line 71] n = 62

[11:04:15] [ID 005, Line 71] n = 63

[11:04:15] [ID 005, Line 71] n = 64

[11:04:15] [ID 005, Line 71] n = 65

[11:04:15] [ID 005, Line 71] n = 66

[11:04:15] [ID 005, Line 71] n = 67

[11:04:15] [ID 005, Line 71] n = 68

[11:04:15] [ID 005, Line 71] n = 69

[11:04:15] [ID 005, Line 71] n = 70

[11:04:15] [ID 005, Line 71] n = 71

[11:04:15] [ID 005, Line 71] n = 72

[11:04:15] [ID 005, Line 71] n = 73

[11:04:15] [ID 005, Line 71] n = 74

[11:04:15] [ID 005, Line 71] n = 75

[11:04:15] [ID 005, Line 71] n = 76

[11:04:15] [ID 005, Line 71] n = 77

[11:04:15] [ID 005, Line 71] n = 78

[11:04:15] [ID 005, Line 71] n = 79

[11:04:15] [ID 005, Line 71] n = 80

[11:04:15] [ID 005, Line 71] n = 81

[11:04:15] [ID 005, Line 71] n = 82

[11:04:15] [ID 005, Line 71] n = 83

[11:04:15] [ID 005, Line 71] n = 84

[11:04:15] [ID 005, Line 71] n = 85

[11:04:15] [ID 005, Line 71] n = 86

[11:04:15] [ID 005, Line 71] n = 87

[11:04:15] [ID 005, Line 71] n = 88

[11:04:15] [ID 005, Line 71] n = 89

[11:04:15] [ID 005, Line 71] n = 90

[11:04:15] [ID 005, Line 71] n = 91

[11:04:15] [ID 005, Line 71] n = 92

[11:04:15] [ID 005, Line 71] n = 93

[11:04:15] [ID 005, Line 71] n = 94

[11:04:15] [ID 005, Line 71] n = 95

[11:04:15] [ID 005, Line 71] n = 96

[11:04:15] [ID 005, Line 71] n = 97

[11:04:15] [ID 005, Line 71] n = 98

[11:04:15] [ID 005, Line 71] n = 99

[11:04:15] [ID 005, Line 84] X Min = 0.000000

[11:04:15] [ID 005, Line 85] X Max = 5555.000000

[11:04:15] [ID 005, Line 86] T Min = 0

[11:04:15] [ID 005, Line 87] T Max = 990

[11:04:15] [ID 005, Line 102] n_start = 100

Hi @grodberg

After reviewing the project, I believe the issue is with the array sizes initialized at the beginning of the macro. The outer for loop runs up to StepEnd (997), placing values into the four arrays. However, when the count reaches 100, the arrays go out of bounds, which causes the for loop to terminate unexpectedly.

Our suggestion is to introduce a temporary variable within the for loop that counts up to the [max array size] - 1 of the data arrays. When this temporary variable reaches its maximum, all the values within the arrays can be sent to the X-Y plot. This way, you can clear the arrays and reset the temporary variable, then continue until the end of the for loop.

Please let me know if this helps or if you have any other questions.