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.
 
 
 
 

138 lines
2.4 KiB

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