09-27-2019 03:51 AM
Hello everyone,
I have developed a simple VI which acquires 3 digital inputs, corresponding to the states of a machine (000 off, 001 setup, 010 automatic mode etc.). It checks the state every 500 msec and then - if the state has changed - writes the acquired data into an Excel file. It works well. I attach the code.
Now I want to acquire the same digital inputs from multiple machines in parallel (let's say 15 machines). Machines will be connected to different lines. For example, machine 1 is connected to port 0 lines 0 to 2, machine 2 to port 0 lines 3 to 5 etc.
How can I use the same code? Do I need a for loop somewhere?
Solved! Go to Solution.
09-27-2019 04:13 AM
Buon giorno Davide,
How can I use the same code? Do I need a for loop somewhere?
You could just extend that DI task to read more lines, depending on the number of machines.
Then you get an array of 6, 9, 12, … bits instead of the 3 bits right now.
Reshape that array into a 2D array of x rows by 3 columns.
Now use a FOR loop to examine each machine's state row by row.
I would separate DAQmx loop from file writing loop by using a queue and employing a producer-consumer scheme. This way you can organize your code into (kind of) modules dedicated to special jobs (DAQmx, file write)…
09-27-2019 04:26 AM - edited 09-27-2019 04:32 AM
There are a few ways you could approach this problem.
One option is to use Array Subset in a For loop to get each block of 3 values, then process these in the same way that you currently do.
A possible extension is to bundle those 3 values to form an array of clusters, which will retain the grouping more easily for considering handling of the blocks.
Using a 2D boolean array (with the same splitting mechanism, or a Reshape Array node) is also an option. (Edit: GerdW already mentioned Reshape Array while I was creating the example. It's probably a better choice...)
An example image showing this in your VI is below:
A smaller test VI to play with is here as a snippet (without DAQmx etc)
Third example, with Reshape Array added (and a comment to explain the outer For loop purpose in case not clear)
09-27-2019 05:16 AM
Hello Gerd:
You could just extend that DI task to read more lines, depending on the number of machines. Then you get an array of 6, 9, 12, … bits instead of the 3 bits right now.
I was obviously going to do that, sorry not to mention it. It is the next step which I find difficult:
Reshape that array into a 2D array of x rows by 3 columns.
I understand I have to use Reshape array after Daqmx Read (as I did in the attachment) but then I got stuck.. can you give me an example?
Thanks a lot for your reply
09-27-2019 05:22 AM
Hello Gerd,
I added the Reshape array (I attach the code) but then I got stuck:
Now use a FOR loop to examine each machine's state row by row.
I would separate DAQmx loop from file writing loop by using a queue and employing a producer-consumer scheme. This way you can organize your code into (kind of) modules dedicated to special jobs (DAQmx, file write)…
This part is not clear to me. Not even the first: I have a single wire out of Reshape array.
How can i separate the machines ? (line 0-2 machine 2, line 3-5 machine 2 etc.). Can you give me an example?
Thanks a lot
09-27-2019 05:37 AM
09-27-2019 10:48 AM
Hello Gerd
thanks again for your excellent support. I understand the 2D array goes into the for loop.
But I want to write only in case the state of the i-th machine has changed. I used a shift register in the while loop, which is not available in the for loop. How would you do that?
Obviously the machine number (that is the raw number of the 2D array) should be written in the text file as well, as first field of the record).
I attach my attempt so far. Thanks
09-27-2019 11:06 AM
You can, if you wish, use a Shift Register on a For loop (the same as with a While loop).
My example (3rd post in this thread) shows the use of either a 2D array or a cluster of arrays, together with a For loop, to determine in which "blocks" the value (combined from 3 values) has changed. Was it not sufficient?
You should be able to use the values created with the "Block Changed?" indicators to connect to your output case.
09-27-2019 11:19 AM
You are right.. I am missing something.
My while loop acquires the whole sequence. Therefore in the very first iteration I write the state of all machines. But after that, I want to write only the state of the machines which have changed.
I will check your example again. Thanks a lot
09-27-2019 11:22 AM
@DavideB wrote:My while loop acquires the whole sequence. Therefore in the very first iteration I write the state of all machines. But after that, I want to write only the state of the machines which have changed.
Do you want to write all in the first iteration? If not, you can combine the "first call?" node (or i=0, I'm sometimes suspicious about the first call node) with your booleans to prevent in that case.