-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigital_input_read.py
More file actions
79 lines (65 loc) · 2.33 KB
/
digital_input_read.py
File metadata and controls
79 lines (65 loc) · 2.33 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#Benjamin Cary, bencary@mit.edu
#some code by Peter N. Saeta, Harvey Mudd College Professor, 2011 July 2
# Commands you need before you can use the labjack with linux:
# $ sudo apt-get install build-essential
# $ sudo apt-get install libusb-1.0-0-dev
# $ sudo apt-get install git-core
# $ git clone git://github.com/labjack/exodriver.git
# $ cd exodriver/
# $ sudo ./install.sh
# $ cd ..
# $ git clone git://github.com/labjack/LabJackPython.git
# $ cd LabJackPython/
# $ sudo python setup.py install
import u3, time
from math import sqrt
# Prepare the u3 interface for streaming
d = u3.U3() # initialize the interface; assumes a single U3 is plugged in to a USB port
d.configU3() # set default configuration
d.configIO(FIOAnalog = 0) # ask for digital inputs
d.setFIOState(0, state = 0) #sets state low and sets as output (which is fixed by next function
d.getDIState(0) #sets digital input
# requests digital I/O channels to be scanned,
# more special terminals here: https://labjack.com/support/datasheets/u3/operation/stream-mode/digital-inputs-timers-counters
# negative channel should almost always be set 31. The sampling frequency is 5000 samples (of each channel)
# per second. The Resolution parameter sets the effective quality of the
# samples. See http://labjack.com/support/u3/users-guide/3.2
d.streamConfig( NumChannels = 1,
PChannels = [193],
NChannels = [31],
Resolution = 3,
SampleFrequency = 5000 )
d.packetsPerRequest = 1 # you can adjust this value to get more or less data
# Try to measure a data set.
def measure():
try:
d.streamStart()
for r in d.streamData():
if r is not None:
if r['errors'] or r['numPackets'] != d.packetsPerRequest or r['missed']:
print "error"
break
finally:
d.streamStop()
return r
# Write a set of data to a file "lmao.txt"
def writeData( r ):
f = open( 'lmao.txt', 'w' )
for i in range(0, len(r)):
f.write(str(r['AIN193'])+'\n')
f.close()
def findComplete(r):
for i in range(0, len(r)):
if (r['AIN193'][i] == (255, 255)):
print r['AIN193'][i]
print "complete"
return False
else:
return True
not_complete = True
while(not_complete):
not_complete = findComplete( measure() )
# for writing scanned data into a file
# for i in range(1,5):
# writeData(measure())
# sleep(1)