Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <math.h>
  8. #include <stdint.h>
  9. #include <time.h>
  10.  
  11. #include "pmd.h"
  12. #include "usb-2001-tc.h"
  13.  
  14. #define HS_DELAY 2000
  15.  
  16. static int wMaxPacketSize; // will be the same for all devices of this type so
  17. // no need to be reentrant.
  18.  
  19. // Globals
  20. Thermocouple_Data ThermocoupleData[8];
  21. double TypeKReverseExtra[3];
  22.  
  23.  
  24. void usbGetAll_USB2001TC(libusb_device_handle *udev, TC_data data)
  25. {
  26. /*
  27. This command reads all of the sensor parameters required to make a temperature measurement or
  28. to ensure a valid RAW reading is performed. The CJC value does not apply the CJC Gradient or
  29. convert it to degrees C. The following conversions are required:
  30.  
  31. CJC Temperature = (CJC Value / 2^15) * 128 - (CJC Gradient)
  32. */
  33.  
  34. uint8_t requesttype = (DEVICE_TO_HOST | VENDOR_TYPE | DEVICE_RECIPIENT);
  35. int ret;
  36. uint8_t values[7];
  37.  
  38. ret = libusb_control_transfer(udev, requesttype, GET_ALL, 0x0, 0x0, (unsigned char *) values, 7, HS_DELAY);
  39. if (ret < 0) {
  40. perror("usbGetAll_USB2001TC Error in reading raw data.");
  41. }
  42.  
  43. memcpy(&data.status, &values[0], 1);
  44. memcpy(&data.CJC, &values[1], 2);
  45. memcpy(&data.ADC_Value, &values[3], 4);
  46.  
  47. return;
  48. }
  49.  
  50.  
  51. void cleanup_USB2001TC(libusb_device_handle *udev)
  52. {
  53. if (udev) {
  54. libusb_clear_halt(udev, LIBUSB_ENDPOINT_IN|1);
  55. libusb_release_interface(udev, 0);
  56. libusb_close(udev);
  57. }
  58. }
  59.  
  60. void setVoltageRange_USB2001TC(libusb_device_handle *udev, int range)
  61. {
  62. char message[MAX_MESSAGE_LENGTH];
  63.  
  64. switch (range) {
  65. case 3:
  66. strcpy(message, "AI{0}:RANGE=BIP146.25E-3V");
  67. break;
  68. case 4:
  69. strcpy(message, "AI{0}:RANGE=BIP73.125E-3V");
  70. break;
  71. default:
  72. strcpy(message, "AI{0}:RANGE=BIP146.25E-3V");
  73. break;
  74. }
  75. sendStringRequest(udev, message);
  76. }
  77.  
  78. void getVoltageRange_USB2001TC(libusb_device_handle *udev, int *range)
  79. {
  80. char message[MAX_MESSAGE_LENGTH];
  81.  
  82. sendStringRequest(udev, "?AI{0}:RANGE");
  83. getStringReturn(udev, message);
  84.  
  85. if (strcmp(message, "AI{0}:RANGE=BIP146.25E-3V") == 0) {
  86. *range = 3;
  87. return;
  88. } else if (strcmp(message, "AI{0}:RANGE=BIP73.125E-3V") == 0) {
  89. *range = 4;
  90. } else {
  91. *range = -1; //invalid range
  92. printf("%s\n", message);
  93. }
  94. }
  95.  
  96. void sendSensorType_USB2001TC(libusb_device_handle *udev, char type)
  97. {
  98. char message[MAX_MESSAGE_LENGTH];
  99. int i;
  100. int ret;
  101.  
  102. sprintf(message, "AI{0}:SENSOR=TC/%c", type);
  103. for (i = 0; i < 5; i++) {
  104. if ((ret = sendStringRequest(udev, message)) >= 0) break;
  105. }
  106. if (ret < 0) {
  107. // perror("sendSensorType_USB2001TC");
  108. }
  109. }
  110.  
  111. void getSensorType_USB2001TC(libusb_device_handle *udev, char *type)
  112. {
  113. char message[MAX_MESSAGE_LENGTH];
  114. int i;
  115. int ret;
  116.  
  117. for (i = 0; i < 5; i++) {
  118. if ((ret = sendStringRequest(udev, "?AI{0}:SENSOR")) >= 0) break;
  119. }
  120. if (ret < 0) {
  121. perror("getSensorType_USB2001TC");
  122. }
  123.  
  124. for (i = 0; i < 5; i++) {
  125. if ((ret = getStringReturn(udev, message)) >= 0) break;
  126. }
  127. if (ret < 0) {
  128. perror("getSensorType_USB2001TC");
  129. }
  130.  
  131. *type = message[16];
  132. }
  133.  
  134. int getStatus_USB2001TC(libusb_device_handle *udev)
  135. {
  136. char message[MAX_MESSAGE_LENGTH];
  137. int status;
  138.  
  139. sendStringRequest(udev, "?AI{0}:STATUS");
  140. getStringReturn(udev, message);
  141. if (strcmp(&message[13], "READY") == 0) {
  142. status = READY;
  143. } else if (strcmp(&message[13], "BUSY") == 0) {
  144. status = BUSY;
  145. } else if (strcmp(&message[13], "ERROR") == 0) {
  146. status = ERROR;
  147. } else {
  148. status = UNKNOWN;
  149. }
  150. return status;
  151. }
  152.  
  153. void getCJC_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
  154. {
  155. char message[MAX_MESSAGE_LENGTH];
  156.  
  157. while(getStatus_USB2001TC(udev) == BUSY) {
  158. usleep(10000);
  159. }
  160. sendStringRequest(udev, "?AI{0}:CJC");
  161. getStringReturn(udev, message);
  162. *CJC_temperature = atof(&message[15]);
  163. }
  164.  
  165. void getCJCDegC_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
  166. {
  167. char message[MAX_MESSAGE_LENGTH];
  168. int i;
  169. int ret;
  170.  
  171. for (i = 0; i < 5; i++) {
  172. if ((ret = sendStringRequest(udev, "?AI{0}:CJC/DEGC")) >= 0) break;
  173. }
  174. if (ret < 0) {
  175. perror("getCJCDegC_USB2001TC");
  176. }
  177.  
  178. for (i = 0; i < 5; i++) {
  179. if ((ret = getStringReturn(udev, message)) >= 0) break;
  180. }
  181. if (ret < 0) {
  182. perror("getCJCDegC_USB2001TC");
  183. }
  184.  
  185. *CJC_temperature = atof(&message[15]);
  186. }
  187.  
  188. void getCJCDegF_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
  189. {
  190. char message[MAX_MESSAGE_LENGTH];
  191. int i;
  192. int ret;
  193.  
  194. for (i = 0; i < 5; i++) {
  195. if ((ret = sendStringRequest(udev, "?AI{0}:CJC/DEGF")) >= 0) break;
  196. }
  197. if (ret < 0) {
  198. perror("getCJCDegF_USB2001TC");
  199. }
  200.  
  201. for (i = 0; i < 5; i++) {
  202. if ((ret = getStringReturn(udev, message)) >= 0) break;
  203. }
  204. if (ret < 0) {
  205. perror("getCJCDegF_USB2001TC");
  206. }
  207.  
  208. *CJC_temperature = atof(&message[15]);
  209. }
  210.  
  211. void getCJCDegKelvin_USB2001TC(libusb_device_handle *udev, double *CJC_temperature)
  212. {
  213. char message[MAX_MESSAGE_LENGTH];
  214.  
  215. sendStringRequest(udev, "?AI{0}:CJC/KELVIN");
  216. getStringReturn(udev, message);
  217. *CJC_temperature = atof(&message[17]);
  218. }
  219.  
  220.  
  221.  
  222. int getValue_USB2001TC(libusb_device_handle *udev, uint32_t *value)
  223. {
  224. char message[MAX_MESSAGE_LENGTH];
  225.  
  226. sendStringRequest(udev, "?AI{0}:VALUE");
  227. getStringReturn(udev, message);
  228.  
  229. *value = atoi(&message[12]);
  230. return 0;
  231. }
  232.  
  233. int tc_temperature_USB2001TC(libusb_device_handle *udev, char tc_type, double *temperature)
  234. {
  235. uint32_t value = 0x0; // integer value of the temperature
  236. double tc_voltage;
  237. double CJC_Temp;
  238. double slope, offset;
  239.  
  240.  
  241. // Set the voltage range (Mode = 4, Range = +/- .078125V)
  242. //setVoltageRange_USB2001TC(udev, 4);
  243.  
  244. // Get Slope and Offset
  245. getSlope_USB2001TC(udev, &slope);
  246. getOffset_USB2001TC(udev, &offset);
  247.  
  248. // Apply calibration slope and offset
  249. if (getValue_USB2001TC(udev, &value) < 0) return -1;
  250. value = value*slope + offset;
  251.  
  252. // Calculate the TC voltage (in mV) from the corrected values
  253. tc_voltage = ((value - 524288.)/524288.) * 73.125;
  254.  
  255. // Read the CJC value in Celsius
  256. getCJCDegC_USB2001TC(udev, &CJC_Temp);
  257.  
  258. // Calculate the CJC voltage using the NIST polynomials and add to tc_voltage in millivolts
  259. tc_voltage = NISTCalcVoltage(tc_type, CJC_Temp) + tc_voltage;
  260.  
  261. // Calcualate actual temperature using reverse NIST polynomial.
  262. *temperature = NISTCalcTemp(tc_type, tc_voltage);
  263.  
  264. return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement