kylemanna
5/14/2017 - 8:06 PM

Read DCV from a Keithley 2015 over RS-232 + SCPI

Read DCV from a Keithley 2015 over RS-232 + SCPI

#!/usr/bin/env python3
import serial
import io
import time


def scpi_write(ser, s, term = '\r'):
    ser.write(str.encode(s))
    if term:
        ser.write(b'\r')
    ser.flush()

def scpi_read(ser):
    
    buf = []

    while True:
        c = ser.read(1)
        #print('read: ', repr(c))
        if c == b'\r':
            s = b''.join(buf).decode('ascii')
            #print("returning", repr(s))
            return s
        else:
            buf.append(c)


if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyUSB2', 19200, timeout=2)
    print(ser)

    while True:
        print('canceling')
        scpi_write(ser, '\x03', None)
        resp = scpi_read(ser)
        print(repr(resp))
        if resp == 'CL\x11':
            break
        if resp == 'DCL\x11':
            break

    scpi_write(ser, ':SYSTem:VERSion?')
    resp = scpi_read(ser)
    print('response: {}'.format(repr(resp)))

    scpi_write(ser, ':SENSe:VOLTage')

    while True:
        scpi_write(ser, ':SENSe:DATA?')
        resp = scpi_read(ser)
        now = time.time()
        print('{:0.6f},{}'.format(now, resp))