Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
- #include <math.h>
- #include <stdint.h>
- #include <time.h>
- #include "pmd.h"
- #include "usb-2001-tc.h"
- #define HS_DELAY 2000
- static int wMaxPacketSize; // will be the same for all devices of this type so
- // no need to be reentrant.
- // Globals
- Thermocouple_Data ThermocoupleData[8];
- double TypeKReverseExtra[3];
- void usbGetAll_USB2001TC(libusb_device_handle *udev, TC_data data)
- {
- /*
- This command reads all of the sensor parameters required to make a temperature measurement or
- to ensure a valid RAW reading is performed. The CJC value does not apply the CJC Gradient or
- convert it to degrees C. The following conversions are required:
- CJC Temperature = (CJC Value / 2^15) * 128 - (CJC Gradient)
- */
- uint8_t requesttype = (DEVICE_TO_HOST | VENDOR_TYPE | DEVICE_RECIPIENT);
- int ret;
- uint8_t values[7];
- ret = libusb_control_transfer(udev, requesttype, GET_ALL, 0x0, 0x0, (unsigned char *) values, 7, HS_DELAY);
- if (ret < 0) {
- perror("usbGetAll_USB2001TC Error in reading raw data.");
- }
- memcpy(&data.status, &values[0], 1);
- memcpy(&data.CJC, &values[1], 2);
- memcpy(&data.ADC_Value, &values[3], 4);
- return;
- }
- void cleanup_USB2001TC(libusb_device_handle *udev)
- {
- if (udev) {
- libusb_clear_halt(udev, LIBUSB_ENDPOINT_IN|1);
- libusb_release_interface(udev, 0);
- libusb_close(udev);
- }
- }
- void setVoltageRange_USB2001TC(libusb_device_handle *udev, int range)
- {
- char message[MAX_MESSAGE_LENGTH];
- switch (range) {
- case 3:
- strcpy(message, "AI{0}:RANGE=BIP146.25E-3V");
- break;
- case 4:
- strcpy(message, "AI{0}:RANGE=BIP73.125E-3V");
- break;
- default:
- strcpy(message, "AI{0}:RANGE=BIP146.25E-3V");
- break;
- }
- sendStringRequest(udev, message);
- }
- void getVoltageRange_USB2001TC(libusb_device_handle *udev, int *range)
- {
- char message[MAX_MESSAGE_LENGTH];
- sendStringRequest(udev, "?AI{0}:RANGE");
- getStringReturn(udev, message);
- if (strcmp(message, "AI{0}:RANGE=BIP146.25E-3V") == 0) {
- *range = 3;
- return;
- } else if (strcmp(message, "AI{0}:RANGE=BIP73.125E-3V") == 0) {
- *range = 4;
- } else {
- *range = -1; //invalid range
- printf("%s\n", message);
- }
- }
- void sendSensorType_USB2001TC(libusb_device_handle *udev, char type)
- {
- char message[MAX_MESSAGE_LENGTH];
- int i;
- int ret;
- sprintf(message, "AI{0}:SENSOR=TC/%c", type);
- for (i = 0; i < 5; i++) {
- if ((ret = sendStringRequest(udev, message)) >= 0) break;
- }
- if (ret < 0) {
- // perror("sendSensorType_USB2001TC");
- }
- }
- void getSensorType_USB2001TC(libusb_device_handle *udev, char *type)
- {
- char message[MAX_MESSAGE_LENGTH];
- int i;
- int ret;
- for (i = 0; i < 5; i++) {
- if ((ret = sendStringRequest(udev, "?AI{0}:SENSOR")) >= 0) break;
- }
- if (ret < 0) {
- perror("getSensorType_USB2001TC");
- }
- for (i = 0; i < 5; i++) {
- if ((ret = getStringReturn(udev, message)) >= 0) break;
- }
- if (ret < 0) {
- perror("getSensorType_USB2001TC");
- }
- *type = message[16];
- }
- int getStatus_USB2001TC(libusb_device_handle *udev)
- {
- char message[MAX_MESSAGE_LENGTH];
- int status;
- sendStringRequest(udev, "?AI{0}:STATUS");
- getStringReturn(udev, message);
- if (strcmp(&message[13], "READY") == 0) {
- status = READY;
- } else if (strcmp(&message[13], "BUSY") == 0) {
- status = BUSY;
- } else if (strcmp(&message[13], "ERROR") == 0) {
- status = ERROR;
- } else {
- status = UNKNOWN;
- }
- return status;
- }
- void getCJC_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
- {
- char message[MAX_MESSAGE_LENGTH];
- while(getStatus_USB2001TC(udev) == BUSY) {
- usleep(10000);
- }
- sendStringRequest(udev, "?AI{0}:CJC");
- getStringReturn(udev, message);
- *CJC_temperature = atof(&message[15]);
- }
- void getCJCDegC_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
- {
- char message[MAX_MESSAGE_LENGTH];
- int i;
- int ret;
- for (i = 0; i < 5; i++) {
- if ((ret = sendStringRequest(udev, "?AI{0}:CJC/DEGC")) >= 0) break;
- }
- if (ret < 0) {
- perror("getCJCDegC_USB2001TC");
- }
- for (i = 0; i < 5; i++) {
- if ((ret = getStringReturn(udev, message)) >= 0) break;
- }
- if (ret < 0) {
- perror("getCJCDegC_USB2001TC");
- }
- *CJC_temperature = atof(&message[15]);
- }
- void getCJCDegF_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
- {
- char message[MAX_MESSAGE_LENGTH];
- int i;
- int ret;
- for (i = 0; i < 5; i++) {
- if ((ret = sendStringRequest(udev, "?AI{0}:CJC/DEGF")) >= 0) break;
- }
- if (ret < 0) {
- perror("getCJCDegF_USB2001TC");
- }
- for (i = 0; i < 5; i++) {
- if ((ret = getStringReturn(udev, message)) >= 0) break;
- }
- if (ret < 0) {
- perror("getCJCDegF_USB2001TC");
- }
- *CJC_temperature = atof(&message[15]);
- }
- void getCJCDegKelvin_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
- {
- char message[MAX_MESSAGE_LENGTH];
- sendStringRequest(udev, "?AI{0}:CJC/KELVIN");
- getStringReturn(udev, message);
- *CJC_temperature = atof(&message[17]);
- }
- int getValue_USB2001TC(libusb_device_handle *udev, uint32_t *value)
- {
- char message[MAX_MESSAGE_LENGTH];
- sendStringRequest(udev, "?AI{0}:VALUE");
- getStringReturn(udev, message);
- *value = atoi(&message[12]);
- return 0;
- }
- int tc_temperature_USB2001TC(libusb_device_handle *udev, char tc_type, double *temperature)
- {
- uint32_t value = 0x0; // integer value of the temperature
- double tc_voltage;
- double CJC_Temp;
- double slope, offset;
- // Set the voltage range (Mode = 4, Range = +/- .078125V)
- //setVoltageRange_USB2001TC(udev, 4);
- // Get Slope and Offset
- getSlope_USB2001TC(udev, &slope);
- getOffset_USB2001TC(udev, &offset);
- // Apply calibration slope and offset
- if (getValue_USB2001TC(udev, &value) < 0) return -1;
- value = value*slope + offset;
- // Calculate the TC voltage (in mV) from the corrected values
- tc_voltage = ((value - 524288.)/524288.) * 73.125;
- // Read the CJC value in Celsius
- getCJCDegC_USB2001TC(udev, &CJC_Temp);
- // Calculate the CJC voltage using the NIST polynomials and add to tc_voltage in millivolts
- tc_voltage = NISTCalcVoltage(tc_type, CJC_Temp) + tc_voltage;
- // Calcualate actual temperature using reverse NIST polynomial.
- *temperature = NISTCalcTemp(tc_type, tc_voltage);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement