Guest User

Untitled

a guest
Feb 15th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. /*
  2. *
  3. * Copyright (c) 2015 Warren J. Jasper <wjasper@tx.ncsu.edu>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <ctype.h>
  25. #include <math.h>
  26. #include <time.h>
  27. #include "pmd.h"
  28. #include "usb-2001-tc.h"
  29.  
  30. #define MAX_COUNT (0xffff)
  31. #define FALSE 0
  32. #define TRUE 1
  33.  
  34. /* Test Program */
  35. int toContinue()
  36. {
  37. int answer;
  38. answer = 0; //answer = getchar();
  39. printf("Continue [yY]? ");
  40. while((answer = getchar()) == '\0' ||
  41. answer == '\n');
  42. return ( answer == 'y' || answer == 'Y');
  43. }
  44.  
  45. int main (int argc, char **argv)
  46. {
  47. libusb_device_handle *udev = NULL;
  48. uint32_t value;
  49. double CJC_Temp, temp;
  50. int ret, i, status;
  51. char ch;
  52. char serial[9];
  53. char version[64];
  54. uint8_t options, tc_type;
  55. double slope, offset;
  56. float slope_f, offset_f;
  57. struct tm date;
  58.  
  59. ret = libusb_init(NULL);
  60. if (ret < 0) {
  61. perror("usb_device_find_USB_MCC: Failed to initialize libusb");
  62. exit(1);
  63. }
  64.  
  65. if ((udev = usb_device_find_USB_MCC(USB_2001_TC_PID, NULL))) {
  66. printf("Success, found a USB-2100-TC!\n");
  67. } else {
  68. printf("Failure, did not find a USB-2100-TC!\n");
  69. return 0;
  70. }
  71.  
  72. printf("bMaxPacketSize = %d\n", usb_get_max_packet_size(udev,0));
  73. while(1) {
  74. printf("\nUSB USB 2001-TC Testing\n");
  75. printf("----------------\n");
  76. printf("Hit 'b' to blink\n");
  77. printf("Hit 'c' to get calibration slope and offset\n");
  78. printf("Hit 'C' to get calibration date\n");
  79. printf("Hit 'd' to set device\n");
  80. printf("Hit 'i' to get CJC and Analog Input readings\n");
  81. printf("Hit 'F' to get the CJC reading in degree F\n");
  82. printf("Hit 'K' to get the CJC reading in degree Kelvin\n");
  83. printf("Hit 'r' to get reset device\n");
  84. // printf("Hit 'R' to restore calibration coefficients\n");
  85. printf("Hit 's' to get serial number\n");
  86. printf("Hit 'S' to get status\n");
  87. printf("Hit 't' to get the temperature\n");
  88. printf("Hit 'v' to get firmware version\n");
  89. printf("Hit 'e' to exit\n");
  90.  
  91. while((ch = getchar()) == '\0' || ch == '\n');
  92. switch(ch) {
  93. case 'b': /* test to see if LED blinks */
  94. printf("Enter number or times to blink: ");
  95. scanf("%hhd", &options);
  96. usbBlink_USB2001TC(udev, options);
  97. break;
  98. case 'c':
  99. printf("Input Thermocouple type [J,K,R,S,T,N,E,B]: ");
  100. while((ch = getchar()) == '\0' || ch == '\n');
  101. switch(ch) {
  102. case 'J':
  103. case 'j':
  104. tc_type = TYPE_J; break;
  105. case 'K':
  106. case 'k':
  107. tc_type = TYPE_K; break;
  108. case 'R':
  109. case 'r':
  110. tc_type = TYPE_R; break;
  111. case 'S':
  112. case 's':
  113. tc_type = TYPE_S; break;
  114. case 'T':
  115. case 't':
  116. tc_type = TYPE_T; break;
  117. case 'N':
  118. case 'n':
  119. tc_type = TYPE_N; break;
  120. case 'E':
  121. case 'e':
  122. tc_type = TYPE_E; break;
  123. case 'B':
  124. case 'b':
  125. tc_type = TYPE_B; break;
  126. default: tc_type = TYPE_J; break;
  127. }
  128. //sendSensorType_USB2001TC(udev, ch);
  129. getSlope_USB2001TC(udev, &slope);
  130. getOffset_USB2001TC(udev, &offset);
  131. printf("Calibration data: Type = %c Slope = %f Offset = %f\n", ch, slope, offset);
  132. break;
  133. case 'C':
  134. getMFGCAL_USB2001TC(udev, &date);
  135. printf("\nLast Calibration date: %s\n\n", asctime(&date));
  136. break;
  137. case 'd':
  138. printf("Input Thermocouple type [J,K,R,S,T,N,E,B]: ");
  139. while((ch = getchar()) == '\0' || ch == '\n');
  140. sendSensorType_USB2001TC(udev, ch);
  141. getSensorType_USB2001TC(udev, &ch);
  142. // read it back
  143. printf("Sensor Type = %c\n", ch);
  144. break;
  145. case 'i':
  146. CJC_Temp = -2000;
  147. getCJCDegC_USB2001TC(udev, &CJC_Temp);
  148. getCJCDegC_USB2001TC(udev, &CJC_Temp);
  149. getCJCDegC_USB2001TC(udev, &CJC_Temp);
  150. printf("CJC = %f degree C. \n", CJC_Temp);
  151.  
  152. // CJC_Temp = -2000;
  153. // getCJC_USB2001TC(udev, &CJC_Temp);
  154. // printf("CJC = %f \n", CJC_Temp);
  155.  
  156. getValue_USB2001TC(udev, &value);
  157. printf("ADC value = %#x\n", value);
  158. break;
  159. case 'F':
  160. CJC_Temp = -2000;
  161. getCJCDegF_USB2001TC(udev, &CJC_Temp);
  162. getCJCDegF_USB2001TC(udev, &CJC_Temp);
  163. getCJCDegF_USB2001TC(udev, &CJC_Temp);
  164. printf("CJC = %f degree F. \n", CJC_Temp);
  165. break;
  166. case 'K':
  167. CJC_Temp = -2000;
  168. getCJCDegKelvin_USB2001TC(udev, &CJC_Temp);
  169. getCJCDegKelvin_USB2001TC(udev, &CJC_Temp);
  170. getCJCDegKelvin_USB2001TC(udev, &CJC_Temp);
  171. printf("CJC = %f degree K. \n", CJC_Temp);
  172. break;
  173. case 'g':
  174. break;
  175. case 'r':
  176. reset_USB2001TC(udev);
  177. break;
  178. case 'R':
  179. printf("Enter slope: ");
  180. scanf("%f", &slope_f);
  181. printf("Enter offset :");
  182. scanf("%f", &offset_f);
  183. usbMemoryUnlock_USB2001TC(udev);
  184. usbSetMemoryAddress_USB2001TC(udev, 0x200);
  185. usbMemoryW_USB2001TC(udev, 4, (uint8_t *) &slope_f);
  186. usbSetMemoryAddress_USB2001TC(udev, 0x200);
  187. usbMemoryW_USB2001TC(udev, 4, (uint8_t *) &offset_f);
  188. usbMemoryLock_USB2001TC(udev);
  189. break;
  190. case 's':
  191. usbGetSerialNumber_USB2001TC(udev, serial);
  192. printf("Serial number = %s\n", serial);
  193. break;
  194. case 'S':
  195. status = getStatus_USB2001TC(udev);
  196. printf("Status = %d\n", status);
  197. break;
  198. case 't':
  199. // put the board in the correct voltage range +/- 73.125mV
  200. setVoltageRange_USB2001TC(udev, 4);
  201. printf("Input Thermocouple type [J,K,R,S,T,N,E,B]: ");
  202. while((ch = getchar()) == '\0' || ch == '\n');
  203. switch(ch) {
  204. case 'J':
  205. case 'j':
  206. tc_type = TYPE_J; break;
  207. case 'K':
  208. case 'k':
  209. tc_type = TYPE_K; break;
  210. case 'R':
  211. case 'r':
  212. tc_type = TYPE_R; break;
  213. case 'S':
  214. case 's':
  215. tc_type = TYPE_S; break;
  216. case 'T':
  217. case 't':
  218. tc_type = TYPE_T; break;
  219. case 'N':
  220. case 'n':
  221. tc_type = TYPE_N; break;
  222. case 'E':
  223. case 'e':
  224. tc_type = TYPE_E; break;
  225. case 'B':
  226. case 'b':
  227. tc_type = TYPE_B; break;
  228. default: tc_type = TYPE_J; break;
  229. }
  230.  
  231. getCJCDegC_USB2001TC(udev, &CJC_Temp);
  232. printf("CJC value = %f C\n", CJC_Temp);
  233.  
  234. for (i = 0; i < 10; i++) {
  235. if (tc_temperature_USB2001TC(udev, tc_type, &temp) >= 0) {
  236. printf("Thermocouple of Type %c: Temperature = %.3f C %.3f F\n", ch, temp, temp*9./5 + 32.);
  237. } else {
  238. printf("Error. Check Thermocouple\n");
  239. }
  240. sleep(1);
  241. }
  242. break;
  243. case 'v':
  244. getFirmwareVersion_USB2001TC(udev, version);
  245. printf("Firmware version = %s\n", version);
  246. break;
  247. case 'e':
  248. cleanup_USB2001TC(udev);
  249. return 0;
  250. default:
  251. break;
  252. }
  253. }
  254. }
Add Comment
Please, Sign In to add comment