1 #A test-application to demonstrate using the Comedilib API
2 # and streaming acquisition in particular, from Python.
3 #This script is completely untested!
4 # author bryan.cole@teraview.co.uk
6 #set the paths so python can find the comedi module
8 sys.path.append('./build/lib.linux-i686-2.2')
13 dev=c.comedi_open('/dev/comedi0')
14 if not dev: raise Exception, "Error openning Comedi device"
16 #get a file-descriptor for use later
17 fd = c.comedi_fileno(dev)
19 nscans=100 #specify total number of scans
21 #three lists containing the chans, gains and referencing
22 #the lists must all have the same length
25 aref =[c.AREF_GROUND, c.AREF_GROUND, c.AREF_GROUND]
27 nchans = len(chans) #number of channels
29 #wrappers include a "chanlist" object (just an Unsigned Int array) for holding the chanlist information
30 mylist = c.chanlist(nchans) #create a chanlist of length nchans
32 #now pack the channel, gain and reference information into the chanlist object
33 #N.B. the CR_PACK and other comedi macros are now python functions
34 for index in range(nchans):
35 mylist[index]=c.cr_pack(chans[index], gains[index], aref[index])
37 #construct a comedi command manually
38 cmd = c.comedi_cmd_struct()
41 cmd.start_src = c.TRIG_NOW
43 cmd.scan_begin_src = c.TRIG_TIMER
44 cmd.scan_begin_arg = int(1.0e9/100000)
45 cmd.convert_src = c.TRIG_TIMER
47 cmd.scan_end_src = c.TRIG_COUNT
48 cmd.scan_end_arg = nchans
49 cmd.stop_src = c.TRIG_COUNT
52 cmd.chanlist_len = nchans
54 #test our comedi command a few times.
55 ret = c.comedi_command_test(dev,cmd)
56 print "first cmd test returns ", ret
57 ret = c.comedi_command_test(dev,cmd)
58 print "second test returns ", ret
59 ret = c.comedi_command_test(dev,cmd)
60 if ret: raise Exception, "Error %d testing comedi command" % ret
63 ret = c.comedi_command(dev,cmd)
65 #I think this will work but it's completely untested!
67 buffersize=1000 # max bytes to acquire per read attempt
68 data=os.read(fd, buffersize)
70 print "read %d bytes" % len(data)
72 data=os.read(fd, buffersize)
73 if len(datalist) == 0 :
74 raise Exeption, "Didn't get any data..."
76 ret = c.comedi_close(dev)
79 datastr = string.join(datalist,'')
81 print datastr #heres your data, as a single string!
83 #if you've got Numeric installed you can convert data into a flat Numpy array
85 # dataarray = Numeric.fromstring(data, Int16)
88 dataarray = numpy.fromstring(data, dtype=numpy.int16)
89 print "size: ", dataarray.size
92 print "Install numpy to read the data into Python"