torfbolt / PyDAQFlex

Python implementation of Measurement Computing's DAQFlex command framework
15 stars 14 forks source link

Example usage? #6

Open IanButterworth opened 9 years ago

IanButterworth commented 9 years ago

Would it be possible to add a brief example script to the readme showing how to use this library? i.e. just connect and grab temp

IanButterworth commented 9 years ago

You can probably guess that this is driven by my own confusion. I'm struggling to start a continuous transfer:

import daqflex

dev = daqflex.USB_2001_TC()

serial_num = dev.find_serial_numbers()
print("USB-2001-TC connected. Serial num %s." % serial_num)

slope, offset = dev.get_calib_data(channel=0)
print("Calibration profile: Slope=%s Offset=%s" % (slope, offset))
packet_size = 512
dev.start_continuous_transfer(1, 1000, packet_size=packet_size)
dev.send_message("AISCAN:START")

Third to last line errors with AttributeError: 'NoneType' object has no attribute 'read' Second to last line does make the LED flash Last line errors with OSError: Send failed, possibly wrong command?

IanButterworth commented 9 years ago

For the sake of others in my situation.. I haven't managed to get continuous transfer to work on my device, but I can grab temperature with this:

import daqflex

dev = daqflex.USB_2001_TC()
serial_num = dev.find_serial_numbers()
print("USB-2001-TC connected. Serial num %s." % serial_num)

slope, offset = dev.get_calib_data(channel=0)
print("Calibration profile: Slope=%s Offset=%s" % (slope, offset))

Value = dev.send_message("?AI{0}:CJC/DEGC")
print(Value)

I am, however finding great temperature inaccuracies with the device, and the calibration instructions in the manual aren't recognised.. but that's a hardware issue, not this library.

adamcircle commented 8 years ago

@ianshmean I'm in the exact same situation as you, trying to use the USB-2001-TC to retrieve temperature measurements. I am using the exact same script that you are using, too: however, I cannot get the device to send me measurements. My script is:

import daqflex

d = daqflex.USB_2001_TC()

def get_temperature(): 
    return float(d.send_message("?AI{0}:CJC/DEGC").encode("utf-8"))

and when I run this, it produces:

u'AI{0}:CJC/DEGC=23.8'

I have no idea why this is happening, and little to no experience in C. Can anyone help me get measurements with this script?

@torfbolt

ixjlyons commented 8 years ago

@sockofleas Here is an excerpt from the DAQFlex User Guide, page 33:

CJC
-    Get the CJC value in the specified format.
     Message    "?AI{ch}:CJC/format"
     Response   "AI{ch}:CJC/format=value"
                format    DEGC, DEGF, KELVIN
                value     The measured temperature.
Example         "?AI{0}:CJC/DEGC"

So you can see the response isn't just the temperature value. Here's one way to parse it:

def get_temperature(device):
    return float(device.send_message("?AI{0}:CJC/DEGC").split('=')[-1])
adamcircle commented 8 years ago

@ixjlyons Thank you for your response. I am really desperate for a solution here. I was looking at the manual earlier, and I had seen the part about the CJC/DEGC. However, that is not the temperature.

See my command script logs here: http://pastebin.com/HjdU4Xig

I perform the command as you instructed:

>>> import daqflex
>>> d = daqflex.USB_2001_TC()
>>> float(d.send_message("?AI:{0}:CJC/DEGC").split('=')[-1])
21.675

But when I move to the built in tester function, you can see that the temperature we retrieved is not the actual temperature, but is instead the CJC value:

CJC value = 21.675000 C
Thermocouple of Type k:  Temperature = 14.909 C   58.837 F

I'm not sure what the CJC is, but I know it's not what I'm looking for. I have tested the temperature that the tester function displays empirically and have found it to be accurate. So how can I access the temperature that the test function displays?

ixjlyons commented 8 years ago

Oh, I was just trying to solve the parsing issue and didn't really think about what you're trying to do. CJC stands for cold junction compensation, and it's one way to get a reference temperature for a thermocouple to work. It appears that DAQFlex has no command for simply retrieving a temperature measurement, so you have to calculate it yourself. Here is an outline of what mcc-libusb does:

  1. Read analog in channel 0 to get value
  2. Apply slope and offset: value = value*slope + offset
  3. Convert to voltage: voltage = ((value - 524288.0)/524288.0) * 73.125)
  4. Read CJC value to get cjc
  5. Calculate the CJC voltage from cjc using NIST polynomials and add it to voltage to get ouput_voltage
  6. Convert output_voltage to temperature using reverse NIST polynomials

Obviously the last two steps are relatively complex compared to the others. You need several sets of coefficients for different thermocouple types and for different temperature ranges. The expectation (or at least my expectation) was that there would be a DAQFlex command to do all of this for you. This seems like a nice thing to include in PyDAQFlex (i.e. implement a read_temperature method for the temperature measurement devices), but I can't really devote that kind of time right now and I don't have the hardware to test it out.

adamcircle commented 8 years ago

@ixjlyons Okay, thanks for your help. I've tried porting that part to Python (just for my type k thermocouple reader) and got most of that down. Would you mind helping me debug the last portion? Everything is working except NISTCalcTemp, I can't figure out where I'm going wrong. http://pastebin.com/2fe2LKjd

ixjlyons commented 8 years ago

Line 26:

for s in range(num):

should be:

for s in range(1, num):

since the first value in the coefficients array is grabbed above the loop. With that change, I get the same input/output as NISTCalcTemp. The same concept is used in NISTCalcVoltage, so you might want to double check your version of that as well.

adamcircle commented 8 years ago

Thanks! That did it :)

akosijun commented 8 years ago

Hi there, Were you able to develop the python code that will allow you to simply get the temperature? I am struggling here a bit, and I didn't quite understand the CJC => temperature solution above.

adamcircle commented 8 years ago

@akosijun Yes, I was able to develop it. It was very difficult and required me to port nearly the entire C code to Python, using the API only for direct measurements. I would be happy to share the code with you but I am not currently at home and will not return for some time. The code is on my home computer and it is not prepared for sharing.

akosijun commented 8 years ago

@sockofleas Please and thank you that would help me a lot.

adamcircle commented 8 years ago

@akosijun I have returned home and will see about uploading the code in the next several days. I am currently extremely busy and ask for your continued patience.

akosijun commented 8 years ago

@sockofleas Thank you for following up. I was able to source the code somewhere else. It is functioning well now. Thank you anyways.