Skip to content

Instantly share code, notes, and snippets.

@anthonyclays
Created September 11, 2015 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyclays/1e86ad11972b5f9719a3 to your computer and use it in GitHub Desktop.
Save anthonyclays/1e86ad11972b5f9719a3 to your computer and use it in GitHub Desktop.
SR830 repl.
#!/usr/bin/env python
# encoding: utf-8
"""
Usage:
rlwrap ./srs_test.py
"""
import os
from serial import Serial
def connect(path=None):
if path is None:
ports = list(filter(lambda f: 'ttyUSB' in f, os.listdir('/dev')))
if not ports:
raise Exception("No serial connection detected")
path = '/dev/' + ports[0]
return Serial(path, baudrate=9600, timeout=1)
def test_connection(ser):
ser.write('*idn?\n')
line = readuntil(ser, '\r')
print line
return line.startswith('Stanford_Research_Systems')
def send_command(ser, command, output=False):
ser.write(command + '\n')
if output:
return readuntil(ser, '\r')
def readuntil(ser, ch):
buf = ser.read(1)
if not buf:
return buf
while buf[-1] != ch:
buf += ser.read(1)
return buf
def repl(ser):
while True:
print '>>>',
try:
cmd = raw_input()
except EOFError:
exit()
output = send_command(ser, cmd, '?' in cmd)
if output:
print output
if __name__ == '__main__':
ser = connect()
print 'Testing connection...'
assert test_connection(ser)
print 'Starting REPL...'
repl(ser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment