Created
September 11, 2015 13:27
-
-
Save anthonyclays/1e86ad11972b5f9719a3 to your computer and use it in GitHub Desktop.
SR830 repl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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