LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to populate 2D array

Hi everyone. I am using LabView 2022 and I've been trying to update an array in the form of a 2d array.

 

Each time the while loop is run, the columns should update before jumping to the next iteration which will be the next row. I have seen many forum posts about this, and I've tried just about everything. The issue is that my program will take in strings using a while loop before the numerical/decimal values will be extracted, so the array updating should be in a for-loop (I think).

Here is a short portion of my code as attached.

 

It works by first extracting values from a PLC via TCP connect and using regex, I take only the decimal values from the string sent out by the PLC. The string sent out is something like "A:1.11, 1.22, 1.33". From there, I intend on extracting the decimal numbers and adding them to an ever-expanding array - every second, a new line will be sent by the PLC and every second another row should be added to the array. However, what's been happening is that only the first row will be overwritten, as shown here:

 

eenjeruu_0-1669612051874.png

 

 

There are also multiple points plotted per reading taken, as shown in the image below - how do I make it only show one point per reading? 

 

eenjeruu_0-1669611728390.png

 

0 Kudos
Message 1 of 38
(1,321 Views)

I guess you didn't program it correctly.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 38
(1,295 Views)

Long ramblings about a while loop and the need of a for loop based on vague guesses are useless. Obviously, you did not try "everything".

 

Showing a section of an array control and a graph without labels is not "code". Do you forget to attach it? (Make sure to "save for previous, e.g. 2020)

0 Kudos
Message 3 of 38
(1,286 Views)

Hi alterbach,

 

I believe I attached the code but it did not get uploaded. Either way, I managed to populate the array, but there are some issues - after awhile, the values received are not what was specified by the PLC providing these values. The code should be attached below. Else, do refer to the snippet which I have tried attaching.

Download All
0 Kudos
Message 4 of 38
(1,263 Views)

Hi eenjeruu,

 


@eenjeruu wrote:

Either way, I managed to populate the array, but there are some issues


Why is this outer FOR loop dependent on the [i]of the WHILE loop? Do you expect zero samples in the first execution and 1000 samples in the 1001st iteration???

Why do you prepend new samples to an evergrowing string?

Why do you need to parse that evergrowing string again and again? Why don't you parse the new data only/once?

 


@eenjeruu wrote:

after awhile, the values received are not what was specified by the PLC providing these values.


Unfortunately your "data" string is empty. It would help to set typical PLC response data as default data in this string indicator so we can examine it too…

Do you think it would be easier to parse a spreadsheet string using SpreadsheetStringToArray instead of using a loop with MatchRegEx?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 38
(1,233 Views)

Hi GerdW, 

 


@GerdW wrote:

 

Why is this outer FOR loop dependent on the [i]of the WHILE loop? Do you expect zero samples in the first execution and 1000 samples in the 1001st iteration???

 


I wanted the number of rows of the array (N of for loop) to be equal to that of the while loop (i.e. while the PLC is outputting data, the array shall expand accordingly). I managed to do this by taking the i of the while loop to be connected to the for loop - this might be a wrong approach. 

 


@GerdW wrote:

Hi eenjeruu,


Unfortunately your "data" string is empty. It would help to set typical PLC response data as default data in this string indicator so we can examine it too…

Do you think it would be easier to parse a spreadsheet string using SpreadsheetStringToArray instead of using a loop with MatchRegEx?


I'm not familiar with parsing a spreadsheet string - am I supposed to save the values from the PLC to a spreadsheet? I'm not sure if it'll be easier.

It is a bit difficult for me to attach the data string, but I can write it down in pseudocode.

 

Every second, the PLC will send a string in this order, each string per second, with the values being current, voltage, and power respectively:
"A:0.00,14.00,0.00" 

"A:1.00,13.00,13.00"

"A:2.00,12.00,24.00"

"A:3.00,11.00,33.00"

"A:4.00,10.00,40.00"

"A:5.00,9.00,45.00"

"A:6.00,8.00,48.00"

"A:7.00,7.00,49.00"

 

and this cycle repeats. It will look like this but preferably without the "steps":

eenjeruu_0-1669629037749.png

 



0 Kudos
Message 6 of 38
(1,011 Views)

I agree that this code makes absolutely no sense. I would recommend to start with a few basic tutorials about dataflow.

 

In additions to what has been said:

  • There is no obvious way to stop the while loop.
  • Your event structure can only run once and the loop will start running first. If you would ever press the "connect" button a second time, the VI will lock up forever, because the event is set to lock the panel until it can complete, but it can never complete. after the second press.
  • Prepending a new string to an ever-growing existing string will eventually cause you to run out of memory. Are you fully aware how feedback nodes work?
  • The FOR loop pyramid makes no sense.
  • Why would you form a 2D array, throw everything away except the first row, the build that back into a 2D array???
  • What's the point of the get function that produces no output?
  • Why is port number orange?
  • Are you sure you are using the right TCP mode?

 

To help you, we need very little information:

  • How does the received string look like? (how much data per iteration of the while loop (one? three? more?)
  • Your 2D array container has three rows and and many columns. Do you want three columns?

As a first step, create a hardware free simulation where you generate typical data in a slow while loop and then build up the 2D array the way you want. Once that is worked out, add your PLC communication in a proper state machine with at least four states (idle, connect, disconnect, read, etc.)

0 Kudos
Message 7 of 38
(1,184 Views)

@altenbach wrote:

As a first step, create a hardware free simulation where you generate typical data in a slow while loop and then build up the 2D array the way you want.


See if this can give you some ideas.

 

altenbach_0-1669648529341.png

 

0 Kudos
Message 8 of 38
(1,176 Views)

@eenjeruu wrote:

There are also multiple points plotted per reading taken, as shown in the image below - how do I make it only show one point per reading? 

 

eenjeruu_0-1669611728390.png

 


We can assume that you read stale data if the received string is identical to the previously received string (why that can happen is a different discussion and can probably be avoided). So just update the chart if the new string differs from the previous. Since this is a chart, doing conditional updates will cause your time axis to become nonlinear.

0 Kudos
Message 9 of 38
(1,175 Views)

@altenbach wrote:

I agree that this code makes absolutely no sense. I would recommend to start with a few basic tutorials about dataflow.

 

I will look into this, thanks for the recommendation! If you have any useful links, I would take a look at them.

 

In additions to what has been said:

  • There is no obvious way to stop the while loop.

In the full program, there is a stop button and a timer that the user can input so that the program can run for however long the user wants it to! 

 

  • Your event structure can only run once and the loop will start running first. If you would ever press the "connect" button a second time, the VI will lock up forever, because the event is set to lock the panel until it can complete, but it can never complete. after the second press.

I understand, what can I do for this to be run properly? 

 

  • Prepending a new string to an ever-growing existing string will eventually cause you to run out of memory. Are you fully aware how feedback nodes work

I see... I did not know about this. I was given that as a "skeleton" code to work with at first, thank you.

 

  • The FOR loop pyramid makes no sense.
  • Why would you form a 2D array, throw everything away except the first row, the build that back into a 2D array???

I had two separate problems: if I could form the 2D array, I did not know how to plot it properly in a chart - so I took the first row only just for the chart alone, and the "full" 2D array was to be plotted in an excel sheet - I attached the full program for your reference (which is quite extensive, and might be a bit rubbish - I apologise.

 

  • What's the point of the get function that produces no output?

The GET function closes the HTTP connection so that the next time the program is run, it can connect to the PLC (it did not manage to work without the GET function)

 

  • Why is port number orange

I have no idea, but all I know is that the port number just needs to be a consistent "2000" as that is the port number for my PLC 🙂

 

  • Are you sure you are using the right TCP mode?

 

This was also provided as the "skeleton" code by my colleague, who admittedly is also unfamiliar with labview as I am. 

 

To help you, we need very little information:

  • How does the received string look like? (how much data per iteration of the while loop (one? three? more?)

The PLC should send codes for A like this:

 

A:0.00,14.00,0.00

A=0

 

if A=0

A:0.00,14.00,0.00

A=1

 

if A=1

A:1.00,13.00,13.00

A=2

 

if A=2

A:2.00,12.00,24.00

A=3

 

this repeats until A=7, and reinitializes A=0, and the same loop happens again. The three values in A represents current, voltage, and power respectively.

 

  • Your 2D array container has three rows and and many columns. Do you want three columns?

 

Yes, it should have three columns instead (current, voltage, power), sorry for the confusion I generated. In the full code, the PLC sends A-E, with each being different and having a different number of columns. I used regex to extract just the decimal numbers from the strings.

 

As a first step, create a hardware free simulation where you generate typical data in a slow while loop and then build up the 2D array the way you want. Once that is worked out, add your PLC communication in a proper state machine with at least four states (idle, connect, disconnect, read, etc.)


Thank you for the feedback, I understand more of the issues that lay within the program - let me try this out with the recommended starting point you sent! I attached the full code so you can see what's going on. It will be a bit messy (tried to tidy up) and some parts will be inconsistent as I was testing.

0 Kudos
Message 10 of 38
(1,012 Views)