-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeithley_serial.py
More file actions
executable file
·64 lines (54 loc) · 1.61 KB
/
keithley_serial.py
File metadata and controls
executable file
·64 lines (54 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
import time
import serial
"""
Open a port for serial communication with the Keithley.
Returns: serial object
"""
def start_serial(port='/dev/tty.USA19H1462P1.1'):
# Configure the serial connections.
# These are specific to the Keithley 2400 SourceMeter.
# This device is configurable, but these are default (except for BAUD).
# Using a carriage return only for termination.
try:
ser = serial.Serial(
port=port,
baudrate=57600,
parity=serial.PARITY_NONE,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE,
timeout=1 # Want this ONLY on the start up!
)
# Need to set the "request to send" pin low
ser.setRTS(False)
return ser
except:
print("Some went wrong...")
def write(ser, command):
ser.write(command + '\r')
def open_serial(ser):
ser.open()
def close_serial(ser):
ser.close()
def read(ser, timeout=1):
out = []
terminated = False
loopTime = time.time()
while ser.inWaiting() > 0 or not terminated:
data = str(ser.read(1))
if data == '\r':
out.append('\n')
terminated = True
elif data != '':
out.append(data)
loopTime = time.time()
if time.time()-loopTime > timeout:
break
if out != '':
return ' '.join(out)
else:
return ''
def write_and_read(ser, command, pause=.25):
write(ser, command)
time.sleep(pause)
return read(ser)