Skip to content

Instantly share code, notes, and snippets.

@Cat-Ion
Created April 10, 2017 02:53
Show Gist options
  • Save Cat-Ion/673f8ffcd1af2a06d32743682b39c92d to your computer and use it in GitHub Desktop.
Save Cat-Ion/673f8ffcd1af2a06d32743682b39c92d to your computer and use it in GitHub Desktop.
Some code to get waveforms from my Tektronix TDS 2012
import serial
import struct
import time
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=19200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
def cmd(s):
ser.write(s)
ser.write(b"\r\n")
time.sleep(0.1) # *shrug*
def setup():
cmd("DATA:WIDTH 1")
cmd("DATA:ENCDG SRI")
cmd("DATA:START 1")
cmd("DATA:STOP 2500")
def get_ch(ch=1):
ser.timeout = 2
ser.flushInput()
cmd(b"DATA:SOURCE CH"+str(ch).encode('utf-8'))
cmd(b"WFMPRE?")
preamble = ser.read_until(b'\r').decode('utf-8').strip().split(';')
cmd(b"CURVE?")
ser.read(1)
len = int(ser.read(1))
len = int(ser.read(len))
data = ser.read(len)
ser.read(1)
return {
'pairs': preamble[7] == 'ENV',
'xincr': preamble[8],
'xzero': preamble[10],
'xunit': preamble[11][1:-1],
'ymult': preamble[12],
'yzero': preamble[13],
'yoffs': preamble[14],
'yunit': preamble[15][1:-1],
'data': struct.unpack(str(len)+'b', data)
}
def pairs(d):
out = "# " + d['xunit'] + ", " + d['yunit'] + "\n"
x0 = float(d['xzero'])
dx = float(d['xincr'])
y0 = float(d['yzero'])
ys = float(d['ymult'])
yo = float(d['yoffs'])
if d['pairs']:
for index, value in enumerate(zip(*[iter(d['data'])]*2)):
x = x0 + 2 * dx * index
y1 = (value[0] - yo) * ys + y0
y2 = (value[1] - yo) * ys + y0
out += '{:< #10.4g} {:< #10.4g} {:< #10.4g}\n'.format(x, y1, y2)
else:
for index, value in enumerate(d['data']):
x = x0 + dx * index
y = (value - yo) * ys + y0
out += '{:< #10.4g} {:< #10.4g}\n'.format(x, y)
return out
if __name__ == '__main__':
print(pairs(get_ch()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment