DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Using Variable Channel Name in ChnFind

Solved!
Go to solution

Big picture - I am finding the zero crossings on a data set so that I can calculate channel to channel phase shift in my data.

 

Small picture - I am starting by finding the 1st zero cross in the data, once I find that I will use the index to find the next and so on

 

Problem

The script posted is just looking for the 1st zero crossing then moving on to the next channel. The problem is that I can not figure out have to make my group and channel vary with for loops.

 

Option Explicit  'Forces the explicit declaration of all the variables in a script.
Dim intCount, intChan 'loop variables 
Dim z  

For intCount = 2 to GroupCount-1 'groups 
Call GROUPDEFAULTSET(intCount) 'change current group 
  For intChan = 1 to ChnNoMax 'iterate through channels 
     Z = ChnFind("Ch(""[intCount]/[intchan]"")<0") 'This does not work, but I'm not sure how to fix it
    msgbox(z)
  next 
next 

I'm sure I'm missing something obvious.

 

MK

0 Kudos
Message 1 of 8
(6,132 Views)

Figured it out.

 

You have to turn the variable into a string using the str function and the fun use of &'s

 

change the "Z=..." line below to 

 

    Z = ChnFind("Ch(""[" & str(intCount) & "]/[" & str(intChan) & "]"")>0")
0 Kudos
Message 2 of 8
(6,103 Views)

Hi MK,

 

I prefer to use an actual variable-- I think it makes the formula much easier to read and the code much easier to debug.  The following approach will work in all recent versions of DIAdem:

 

iMax = Data.Root.ChannelGroups.Count
FOR i = 2 TO iMax-1
  Set Group = Data.Root.ChannelGroups(i)
  FOR Each Channel In Group.Channels
    T1 = Channel.GetReference(eRefTypeNameName)
    Row = ChnFind("Ch(T1) = 0", 1)
    LogFileWrite Group.Name & "/" & Channel.Name & " = " & Row
  NEXT ' Channel
NEXT ' i, Group

In DIAdem 2015 the ChnFind() function has two new parameters at the end that enable you to specify both the symbols in the formula and the channel each symbol refers to.  Even more useful for your task, in DIAdem 2015 there are ChnEvent...() functions to find every instance of an event (such as a zero crossing) and return them all to you in an array from just one function call.  

 

I hope you have access to DIAdem 2015,

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 3 of 8
(6,087 Views)

Great, thanks Brad. This will really help simplify my code. A couple of clarification questions:

 

What is the advantage to using T1 instead of "dim"ing my own variable (say S1)?

 

Channel.GetReference(eRefTypeNameName) returns the "Group Name/ Channel Name" Is there something available that allows me to get just the Channel Name or just the group name? Group.GetReference(eRefTypeNameName) seems to work to get the group name, but I'm not sure if it is the correct way to do so.

 

0 Kudos
Message 4 of 8
(6,080 Views)

I just realized the answer to my second question (Group.Name and Channel.Name) were staring me in the face...

0 Kudos
Message 5 of 8
(6,071 Views)

Hi MK,

 

You can use your own variables, but you have to GlobalDim them, not Dim them.  You have to use a global variable in ChnFind() and Calculate() expressions.  The only advange of T1, R1, etc. is that they are automatically declared as global variables every time DIAdem launches.

 

You can assign T1 = Channel.Name, then the ChnFind() expression will use the first matching channel name it finds in the Data Portal.  You can not just assign T1 = Group.Name, because the ChnFind() function only works on channels, not groups.

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 6 of 8
(6,059 Views)

My reason for asking stems from the fact that I have this elsewhere in the code.

 

    CurrChan = Data.GetChannel("[" & str(intCount) & "]/[" & str(intChan) & "]").Name
    Set Rising = Data.Root.ChannelGroups("Index " & str(CurrGroup) &"").Channels.Add(""& str(CurrChan) & " Rising",DataTypeChnFloat64)'Add new channel for each channel in group
    Set Falling = Data.Root.ChannelGroups("Index " & str(CurrGroup) &"").Channels.Add(""& str(CurrChan) & " Falling",DataTypeChnFloat64)'Add new channel for each channel in group

I use this code to grap the current channel name based on the loops iteration and convert it to a string. The group has already been created at this point and I am creating a channel called "Current Channel Rising".

 

I'm not sure if I could use the methods you described to simplify this.

 

Michael

0 Kudos
Message 7 of 8
(6,034 Views)
Solution
Accepted by topic author MK17

Hi Michael,

 

You don't need global variables to simplify this, but I would definitely use object variables to simplify it.  When indexing Groups or Channels, it's much easier to use the index variable with the Data.Root.ChannelGroups collect or Group.Channels collection directly.  I also prefer to store the Group object and Channel object in a variable.  For instance, you could then use Channel.DataType to Add exactly the same data type channel to the new group that came from the old group.  You can also easily get the name, unit, and all sorts of other properties directly from the object variable.  I prefer to use a separate Group object variable to reduce clutter in the colde, even though it adds 2 lines to your code snippet example.

 

Set FromGroup = Data.Root.ChannelGroups(intCount)
Set FromChannel = FromGroup.Channels(intChan)
Set ToGroup = Data.Root.ChannelGroups("Index " & FromGroup.Name)
Set Rising  = ToGroup.Channels.Add(FromChannel.Name & " Rising",  DataTypeChnFloat64)
Set Falling = ToGroup.Channels.Add(FromChannel.Name & " Falling", DataTypeChnFloat64)

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 8 of 8
(6,003 Views)