Routing my value to an analog output...


Recommended Posts

Hello,

I am very new to using DAQFactory and LabJack. I have read the manual but somehow I am still missing something...

I am trying to update my 0-5VDC analog output on the LabJack (i.e.: DAC0) with data stored on the computer. How do I automatically route the data from my CSV file to the output of DAC0. My CSV file is automatically updated every 5 minutes with new data.

From reading the manual, it seem to me that the only way to set the DAC outputs is to manually enter these numbers (page 29-30 in the DAQFatory LabJack manual)...

Any help and suggestions will be much appreciated.

Thanks!

Link to comment
Share on other sites

You can output channels from script simply by assigning a value to them:

myoutchannel = 5

See 6.2.1 in the DAQFactory - LabJack Application Guide

The (only slightly) trickier part for you is actually reading the file. Its not hard, but requires several lines of code. What that code is depends a little on the file and which value you want. Perhaps you can post a sample file and exactly which value you want to pull from it and I can tell you exactly what script you need.

Link to comment
Share on other sites

Hello,

Thanks for the prompt reply. Section 6 is very informative. I guess I should have kept reading :) .

My CSV file will only contain 2 columns: Date and Concentration.

I am only concerned with outputting the concentration value which will vary from 0 to 21 as a voltage. My file name is result.csv in the C:/ Drive

Date, Concentration
5/13/2008 6:25, 3
5/13/2008 6:30, 13
5/13/2008 6:35, 4
5/13/2008 6:40, 0
5/13/2008 6:45, 21

Thanks for all your help!

Link to comment
Share on other sites

ok, but that file has lots of concentration values. Are you going to read a line, set the output, delay, then read another line and repeat? It kind of comes down to this:

private handle = file.open("C:\result.csv",1,0,0,1)
try
   private string linein = file.read(handle) // read the title line and ignore
   while(1)
	  linein = file.read(handle)
	  if (linein == "") // done!
		 break
	  endif
	  myoutchannel = strtodouble(parse(linein,1,",")) // set output based on input
	  delay(1)  // wait one second and repeat
   endwhile
catch()
   ? strLastError
endcatch
file.close(handle)

Link to comment
Share on other sites

Hello!

I hope everyone had a great Memorial day week-end.

Thanks a bunch for the coding tips. They helped tremendously! On my last post, I failed to say that we are only interested in outputting the concentration value of the very last line that gets updated every 5 minutes.

Here is how I did that:

private handle = file.open("C:\results.csv",1,0,0,1)
global string linein
global string nextline

try   
   while(1)
	  nextline = file.Read(handle)
	  if (nextline != "")
		 linein = nextline
		// ? nextline
	  else
		 Output = strtodouble(parse(linein,1,",")) // set output based on input
		 file.close(handle)
		 delay(300)
		 private handle = file.open("C:\results.csv",1,0,0,1)
	  endif
   endwhile
catch()
   ? strLastError
endcatch

It seems to be working fine. I just want to be sure that I am as efficient as possible (The file results.csv can go up to 2.5Mb or 10,000+ entries).

Thanks!

Link to comment
Share on other sites

I also have another question for that same project. My concentration values will be between 0 and 21 and to be output on DAC0. The DAC0 outputs between 0-5VDC. Now how do I ensure that 21 corresponds to 5VDC and such? I did try to add a conversion however, I still get the number from the file instead of a converted number between 0-5VDC. Also, How do I make sure that I bound it to DAC0?

Any help is appreciated.

Thanks!

Link to comment
Share on other sites

On your first question: the code looks good, but given the potential size of the file, that loop is going to get quite slow scanning through the same 9,999 lines to find the last one. Since the lines aren't fixed length you can't just jump to the last line, but you could get close. After the open, do File.GetLength() to get the number of bytes in the file, and then use File.Seek() to jump to maybe 1000 bytes short of the end. Then go into your loop. The first iteration of the loop is likely to get a partial line, but who cares as long as its not the last line. The code might be something like this:

private handle = file.open("C:\results.csv",1,0,0,1)
private len = file.getlength(handle)
if (len > 2000)
   file.seek(handle,len-1000)
endif
...

Also, I'd use privates instead of globals for linein and nextline. Did you know that when you are debugging (i.e. have put a breakpoint in the script) you can view privates in the watch or using the print (?) statement in the command line?

As for the second part, create a conversion and in the channel table, select that conversion by name. Its just like using a conversion on an input, but the formula you use converts engineering units to device units (volts). So, for your situation, you want:

Value / 21 * 5

Link to comment
Share on other sites

  • 2 weeks later...

Hello!

I just wanted to take the time to thank you guys for all the help you provided! Everything worked fine and your last improvement for the file size was really smart! Much Thanks again and if anything ever fails, I know where to come for more help :lol: !

Much Thanks again!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.