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.
 
 
 
 

77 lines
1.3 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. float 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, float amps)
  46. {
  47. char s[128];
  48. sprintf(s, ":sour:curr %0.3f", amps);
  49. gput(s);
  50. return 0;
  51. }
  52. float keithley_read(int fd)
  53. {
  54. char s[128];
  55. float 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, "%f,%f,", &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. }