Advertisement
j0h

Agilent 54622D O-scope

j0h
Dec 9th, 2018
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.93 KB | None | 0 0
  1. // agilentscope.c - GNU g++ egcs-2.91.66, V. Bancroft  1999
  2. //Fixed to run on ubuntu linux 2018
  3. //src from documentation. --fixed errors
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>     // ISO C Standard: 4.9 INPUT/OUTPUT
  7. #include <time.h>      // ISO C Standard: 4.12 DATE and TIME  
  8. #include <termios.h>   // POSIX Standard: 7.1-2 General Terminal Intf.
  9. #include <unistd.h>    // POSIX Standard: 2.10 Symbolic Constants  
  10. #include <fcntl.h>     // POSIX Standard: 6.5 File Control Operations
  11. #include <sys/types.h> // POSIX Standard: 2.6 Primitive System Data Types
  12. #include <sys/stat.h>  // POSIX Standard: 5.6 File Characteristics
  13.                        
  14. #define DWORD                size_t
  15. // a file handle is an integer descriptor,
  16. #define HANDLE               int
  17. // open failure results in negative one,
  18. #define INVALID_HANDLE_VALUE -1
  19. // default device string
  20. #define DEVICE               "/dev/ttyUSB0"
  21. // define values for iSerOK
  22. #define TRUE                 1            
  23. #define FALSE                0
  24. HANDLE   hSerPort;     // Global variables
  25. int      iSerOK;
  26. void SerMessage(char* msg1, char* msg2)
  27. // Displays a message to the user.
  28. // msg1 = main message
  29. // msg2 = message caption
  30. // Change this to your preferred way of displaying msgs.
  31. {
  32.   printf( "%s ( %s )\n", msg1, msg2);
  33. }
  34. void SerCrash(char* msg1, char* msg2)
  35. // Like SerMessage, but ends the program.
  36. {
  37.   SerMessage(msg1,msg2);
  38.   iSerOK = 0;
  39. }
  40. int SerOK()
  41. // Returns nonzero if most recent operation succeeded,
  42. // or 0 if there was an error
  43. {
  44.   return iSerOK;
  45. }
  46. void SerOpen(char* portname, char* handshake)
  47. // Opens the serial port.
  48. // portname = "COM1", "COM2", etc.
  49. // handshake = "RTS" or "DTR"
  50. {
  51.   //
  52.   // Open the port using a handle
  53.   //
  54.   hSerPort = open( portname, O_RDWR );
  55.   if (hSerPort == INVALID_HANDLE_VALUE) {
  56.     SerCrash("Can't open comm port",portname);
  57.     iSerOK = FALSE;
  58.   }
  59.  
  60.   struct termios term;
  61.   if ( tcgetattr( hSerPort, &term ) ) {
  62.     SerMessage( "Can't get device settings.", "tcgetattr");
  63.     iSerOK = FALSE;
  64.   }
  65.   // Now choose the handshaking mode...
  66.   if ((handshake[0] == 'r') || (handshake[0] == 'R'))
  67.   {
  68.     term.c_cflag =  CRTSCTS;    // RTS/CTS flow control of output
  69.   }
  70.   else
  71.   {
  72.     term.c_cflag &=  ~( CRTSCTS ); // disable CTS flow control of output
  73.   }
  74.   //
  75.   // Set baud rate and other attributes
  76.   //
  77.   term.c_cflag |=   CS8;      // eight bit character set
  78.   term.c_cflag |=  CREAD;    // enable receiver
  79.   term.c_cflag |=  HUPCL;    // lower modem lines on last close
  80.   term.c_cflag |=  CLOCAL;   // ignore modem status lines
  81.   term.c_iflag = 0;          // turn off input processing
  82.   // term.c_iflag = IGNPAR;     // ignore parity errors
  83.   term.c_lflag = 0;          // turn off local processing
  84.   term.c_oflag = 0;          // turn off output processing
  85.  
  86. cfsetispeed( &term, B9600 );
  87.   cfsetospeed( &term, B9600 );
  88.   if (tcsetattr( hSerPort, TCSANOW, &term )) {
  89.     SerCrash("Bad parameters for port",portname);
  90.     iSerOK = FALSE;
  91.   }
  92.   if (!iSerOK) SerCrash("Bad parameters for port",portname);
  93.   //
  94.   // Disable Windows' timeouts; we keep track of our own
  95.   //
  96.   // COMMTIMEOUTS t = {MAXDWORD,0,0,0,0};
  97.   // SetCommTimeouts(hSerPort,&t);
  98. }
  99. void SerClose()
  100. // Closes the serial port.
  101. {
  102.   iSerOK != close(hSerPort);
  103.   if (!iSerOK) SerCrash("Problem closing serial port","");
  104. }
  105. void SerPut(char* data)
  106. // Transmits a line followed by CR and LF.
  107. // Caution!  Will wait forever, without registering an error,
  108. // if handshaking signals tell it not to transmit.
  109. {
  110.   DWORD nBytes;
  111.   iSerOK =  nBytes=write(hSerPort,(const void *)data,strlen(data));
  112.   iSerOK &= nBytes=write(hSerPort,"\r\n",2);
  113.   if (!iSerOK) SerCrash("Problem writing to serial port","");
  114. }
  115. void SerGetChar(char* c, int* success)
  116. // Receives a character from the serial port,
  117. // or times out after 10 seconds.
  118. // success = 0 if timeout, 1 if successful
  119. {
  120.   time_t finish;
  121.   finish = time(NULL) + 10;
  122.   *success = 0;
  123.   DWORD nBytes = 0;
  124.   while ((*success == 0) && (time(NULL) < finish)) {
  125.     nBytes=read(hSerPort,c,(size_t)1);
  126.     *success = nBytes;
  127.  
  128.   }
  129.   if (*success == 0)
  130.     SerCrash("Timed out waiting for serial input","");
  131. }
  132. void SerGet(char* buf, int bufsize)
  133. // Inputs a line from the serial port, stopping at
  134. // end of line, carriage return, timeout, or buffer full.
  135. {
  136.   int bufpos = 0;
  137.   buf[bufpos] = 0;    // initialize empty string
  138.   int bufposmax = bufsize-1;  // maximum subscript
  139.   char c = 0;
  140.   int success = 0;
  141. //
  142.   // Eat any Return or Line Feed characters
  143.   // left over from end of previous line
  144.   //
  145.   do { SerGetChar(&c,&success); }
  146.   while ((success == 1) && (c <= 31));
  147.   //
  148.   // We have now read the first character or timed out.
  149.   // Read characters until any termination condition is met.
  150.   //
  151.   while (1) {
  152.     if (success == 0) return;         // timeout
  153.     if (c == 13) return;              // carriage return
  154.     if (c == 10) return;              // line feed
  155.     if (bufpos >= bufposmax) return;  // buffer full
  156.     buf[bufpos] = c;
  157.     bufpos++;
  158.     buf[bufpos] = 0;  // guarantee validly terminated string
  159.     SerGetChar(&c,&success);
  160.   }
  161. }
  162. void SerGetNum(double* x)
  163. // Reads a floating-point number from the serial port.
  164. {
  165.    char buf[20];
  166.    SerGet(buf,sizeof(buf));
  167.    iSerOK = sscanf(buf,"%lf",x);   // "%lf" = "long float" = double
  168.    if (iSerOK < 1) {
  169.      SerCrash("Problem converting string to number",buf);
  170.      *x = 0.0;
  171.    }
  172. }
  173. // Sample main program to conduct dialogue
  174. // with Agilent 54645D oscilloscope,
  175. // 9600 baud, DTR handshaking
  176. int main()
  177. {
  178.  char b[20];
  179.  double x;
  180.  // Sample dialogue for Agilent 64645D oscilloscope
  181.  SerOpen("/dev/ttyUSB0","RTS");   // note RTS handshaking
  182.  SerPut("*RST");
  183.  SerPut("*CLS");
  184.  SerPut(":SYSTEM:DSP 'Communicating with computer'");
  185.  SerPut(":AUTOSCALE");
  186.  SerPut(":MEASURE:VRMS? ANALOG1");
  187.  SerGetNum(&x);
  188.  printf( "Channel 1 RMS voltage (as number) = %f\n", x );
  189.  // cout << "Channel 1 RMS voltage (as number) = " << x << "\n";
  190.  SerPut(":MEASURE:VPP? ANALOG2");
  191.  SerGet(b,sizeof(b));
  192.  printf( "Channel 2 P-P voltage (as string) = %s\n", b );
  193.  // cout << "Channel 2 P-P voltage (as string) = " << b << "\n";
  194. /*
  195. // Sample dialogue for Agilent 34401A multimeter
  196.  SerOpen("COM1","DTR");   // note DTR handshaking
  197.  SerPut(":SYST:REM");
  198.  SerPut("*RST");
  199.  SerPut("*CLS");
  200.  SerPut(":DISP:TEXT 'OK'");
  201.  SerPut(":MEASURE:VOLT:DC?");
  202.  SerGetNum(&x);
  203.  printf( "Voltage (as number) = %d\n", x );
  204.  // cout << "Voltage (as number) = " << x << "\n";
  205.  SerPut(":MEASURE:VOLT:DC?");
  206.  SerGet(b,sizeof(b));
  207.  printf( "Voltage (as string) = %s\n", b );
  208.  // cout << "Voltage (as string) = " << b << "\n";
  209. */
  210.  // Say goodbye
  211.  printf( "Press Enter...");
  212.  scanf("\r");
  213.   SerClose();
  214.  return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement