You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

156 lines
2.7 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #ifndef __USE_ISOC99
  4. #define __USE_ISOC99
  5. #endif
  6. #include <math.h>
  7. #include "gpib.h"
  8. #include "serial-util.h"
  9. #define gputv(string, rv) do { \
  10. int ____l = strlen(string); \
  11. if (safewrite(fd, string, ____l) != ____l) return rv; \
  12. if (safewrite(fd, "\r", 1) != 1) return rv; } while(0)
  13. #define gput(string) gputv(string, -1)
  14. int gpib_init(int fd)
  15. {
  16. gput("++mode 1");
  17. gput("++auto 0");
  18. gput("++eos 1");
  19. gput("++eoi 1");
  20. gput("++read_tmo_ms 4000");
  21. gput("++eot_enable 1");
  22. gput("++eot_char 13");
  23. return 0;
  24. }
  25. int gpib_addr(int fd, int addr)
  26. {
  27. char s[128];
  28. sprintf(s, "++addr %d", addr);
  29. gput(s);
  30. return 0;
  31. }
  32. int keithley_init(int fd)
  33. {
  34. double i;
  35. gput(":syst:beep:stat 1");
  36. gput(":sour:func:mode curr");
  37. gput(":sour:del 0");
  38. gput(":sour:curr 0");
  39. gput(":sour:curr:range:auto on");
  40. gput(":outp on");
  41. drain(fd);
  42. i = keithley_read(fd);
  43. if (isnan(i))
  44. return -1;
  45. return 0;
  46. }
  47. int keithley_current(int fd, double amps)
  48. {
  49. char s[128];
  50. sprintf(s, ":sour:curr %0.12f", amps);
  51. gput(s);
  52. return 0;
  53. }
  54. double keithley_read(int fd)
  55. {
  56. char s[128];
  57. double i, dummy;
  58. gputv("read?", NAN);
  59. gputv("++read", NAN);
  60. if (fdgets(s, 128, fd, 2000) == NULL)
  61. return NAN;
  62. if (sscanf(s, "%lf,%lf,", &dummy, &i) != 2)
  63. return NAN;
  64. return i;
  65. }
  66. int keithley_off(int fd)
  67. {
  68. gput(":outp off");
  69. return 0;
  70. }
  71. /* this function is for Keithley2002 Digital multimeter */
  72. int keithley2002_init(int fd)
  73. {
  74. double i;
  75. gput(":trac:cle");
  76. gput(":sens:curr:dc:rang:auto on");
  77. gput(":sens:curr:dc:dig 8");
  78. gput(":form:elem read");
  79. gput(":init:cont off");
  80. drain(fd);
  81. i = keithley2002_read(fd);
  82. if (isnan(i))
  83. return -1;
  84. return 0;
  85. }
  86. int keithley2002_init2(int fd)
  87. {
  88. double i;
  89. gput(":trac:cle");
  90. gput(":sens:curr:dc:rang:auto on");
  91. gput(":sens:curr:dc:dig 8");
  92. gput(":form:elem read, time");
  93. gput(":syst:tst:type rel");
  94. gput(":syst:tst:rel:res");
  95. gput(":init:cont off");
  96. drain(fd);
  97. i = keithley2002_read(fd);
  98. if (isnan(i))
  99. return -1;
  100. return 0;
  101. }
  102. int keithley2002_init_volts(int fd)
  103. {
  104. double i;
  105. gput(":trac:cle");
  106. gput(":sens:func 'volt:dc'");
  107. gput(":sens:volt:dc:rang 5");
  108. gput(":sens:volt:dc:dig 8");
  109. gput(":form:elem read");
  110. gput(":init:cont off");
  111. drain(fd);
  112. i = keithley2002_read(fd);
  113. if (isnan(i))
  114. return -1;
  115. return 0;
  116. }
  117. double keithley2002_read(int fd)
  118. {
  119. char s[128];
  120. double i;
  121. gputv("read?", NAN);
  122. gputv("++read", NAN);
  123. if (fdgets(s, 128, fd, 2000) == NULL)
  124. return NAN;
  125. if (sscanf(s, "%lf", &i) != 1)
  126. return NAN;
  127. return i;
  128. }
  129. double keithley2002_read2(int fd, double *tx)
  130. {
  131. char s[128];
  132. double i;
  133. gputv("read?", NAN);
  134. gputv("++read", NAN);
  135. if (fdgets(s, 128, fd, 2000) == NULL)
  136. return NAN;
  137. if (sscanf(s, "%lf,%lf", &i, tx) < 1)
  138. return NAN;
  139. return i;
  140. }