# Variable And Expression Language

## Recommended Posts

I am going to sound very ignorant, because I am. I'm pretty good with the math and electronics, but have little experience with programming. I've read the appropriate sections on expressions and am still confused. I'll probably crawl my way through it by trial and error, however I am sure someone can tell me exactly what I need.

I need to do a calculation with data from two channels and some fixed variables. I would like the result to be posted to the page one data display, the graph, and sent to a log file. I'm using a U3 and for the moment DAQ Express.

1) Where do I put the fixed variable data and the result data?

2) Do I have to type in my coefficient variable data in each expression each time? Or can I name it and pull the number as a tag name?

3) I assume if I can give a name to the result, I can display it on page one. I don't know how to write an expression that writes to a tag? could someone write me an example of such an expression?

4) There are expressions for the display, the channels, and the conversions. do I have to reenter it in each location?

5) How do I write an expression that has channel data from two or more channels?

6) I assume writing the data to a log file is pretty simple, but I haven't read that section. I will have this computer on my wifi network and want to be able to copy the file to an Excel data base on another network computer. Any advice would be appreciated.

Thank you.

##### Share on other sites

Everyone is a beginner at some point, so don't feel bad about the questions. The easiest solution when you want to do a calculation using two channels is to create a third channel and put the result there.

1) create another channel. Call it whatever you want, I'm just going to use "result" for these instructions. Device Type "Test", D# 0, I/O type A/D, Chan # 0, Timing = 0. Rest defaults.

2) in one of the two channels, preferably the one with the higher channel number, click on its name in the workspace under Channels and then select the event tab.

3) now let's assume your two channels are "chan1" and "chan2" and you want to divide chan1 by chan2, multiply the result by 1.348 and add 4.83. In the event tab of "result", type:

result.addValue(chan1[0] / chan2[0] * 1.348 + 4.83)

4) add "result" to your logging set. Use "result[0]" in any components you want to display the latest result, and just "result" for a trend graph.

This is probably the easiest way. If you just had one channel you wanted to do a calc for, it'd be easier to simply use a conversion.

##### Share on other sites

Everyone is a beginner at some point, so don't feel bad about the questions. The easiest solution when you want to do a calculation using two channels is to create a third channel and put the result there.

1) create another channel. Call it whatever you want, I'm just going to use "result" for these instructions. Device Type "Test", D# 0, I/O type A/D, Chan # 0, Timing = 0. Rest defaults.

2) in one of the two channels, preferably the one with the higher channel number, click on its name in the workspace under Channels and then select the event tab.

3) now let's assume your two channels are "chan1" and "chan2" and you want to divide chan1 by chan2, multiply the result by 1.348 and add 4.83. In the event tab of "result", type:

result.addValue(chan1[0] / chan2[0] * 1.348 + 4.83)

4) add "result" to your logging set. Use "result[0]" in any components you want to display the latest result, and just "result" for a trend graph.

This is probably the easiest way. If you just had one channel you wanted to do a calc for, it'd be easier to simply use a conversion.

So by doing this the "result" will be a channel. Therefore it can go out as a logging set item and I won't have to modify raw data in Excel. Brilliant. Thank you.

##### Share on other sites

Exactly. You'll notice I use this technique a lot here in the forums. Channel's have a lot of features built in over variables and even V.Channels, so I tend to use them a lot, even just as containers.

##### Share on other sites

OK I tried this and it didn't work. My result channel is acting just like a test channel, going up and down in a sign wave. If i create another device it ca'n find it and gives an alarm. how do I get around this problem?