From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Strange ChnFindReverse behaviour

Solved!
Go to solution

Hi,

 

here is a minimum example of the strange behaviour, usually the predefined R11-value would be a much more complex structure:

 

'========================================

Call Data.Root.Clear()
Call ChnLinGen("XXX", 10, -10, 21)

'prior definition of comparison variable
R11 = -11
R1 = ChnFindReverse("Ch(""XXX"")>=("""& R11 &""")")

'direct input of comparison variable
R2 = ChnFindReverse("Ch(""XXX"")>=-12")

Call MsgBox(R1 & " should be the same as " & R2)

'========================================

 

Am I doing something wrong?

 

Phex

0 Kudos
Message 1 of 7
(4,596 Views)

If you change teh following two lines

R11 = -11
R1 = ChnFindReverse("Ch(""XXX"")>=("""& R11 &""")")

to

R11 = -12
R1 = ChnFindReverse("Ch(""XXX"")>=("& R11 &")")

 

it should be the same. The main difference is in the second line where I changed teh code to handle R12 as a number instead of a string

0 Kudos
Message 2 of 7
(4,571 Views)

Hello Phex,

 

This has nothing to do with the ChnFindReverse function. You are comparing two different things with ChnFindReverse function in the two lines in your script. The results DIAdem delivers have something to do with the way you are comparing things in the parenthesis behind the ChnFindReverse statement.

 

This line is comparing a string ("-11") and not a number (-11): R1 = ChnFindReverse("Ch(""XXX"")>=("""& R11 &""")")

The result of the string comparison is that the string "-11" is greater than "-10" - because the first two characters of that string are identical, and the third character ("1" vs "0") has the result that 1 is greater than 0 (based on the ASCII code values), thus 20 (the line number of the value -9 -and the second character "9" is larger than 1) is the result of that comparison.

 

In the line R2 = ChnFindReverse("Ch(""XXX"")>=-12")

you are comparing an actual number to the channel values and thus receive the line number 21 (which is the first value of the channel where your condition is true).

 

If you change the syntax as suggested by Andreas in his post above mine, you will receive the expected result in both cases ...

 

     Otmar

 

Otmar D. Foehner
0 Kudos
Message 3 of 7
(4,563 Views)

Thank you Andreas and Otmar for your reply and explanation. I have tried that, and it works fine for integer numbers, but as soon as I want to compare it against a real number, like R11 = -11.1, then I get the error message:

 

Expected ')'

 

What do I need to change now?

 

Phex

0 Kudos
Message 4 of 7
(4,553 Views)

Can anyone please explain why this works:

 

R1 = ChnFindReverse("Ch(""XXX"")>=-12.1")

 

but this not:

 

R11 = -12.1

R1 = ChnFindReverse("Ch(""XXX"")>=("& R11 &")")

 

?

 

 

 

0 Kudos
Message 5 of 7
(4,535 Views)
Solution
Accepted by topic author Phex

Not sure what ends up in teh string but the way you scripted it, you rely on teh VBscript runtime to format the value in R11.

I recommend to do this

 

R1 = ChnFindReverse("Ch(""XXX"")>=("& str(R11) &")")

 

This uses DIAdem's built in formatting function "str"

 

And just as an FYI: the return value of ChnFIndReverse is an Integer number. Using R1 is not wrong but either using a local VBScript variable or L1 would be more appropriate

Message 6 of 7
(4,533 Views)

Thanks for the correction. It works!

 

Phex

0 Kudos
Message 7 of 7
(4,528 Views)