Versioning started.
[pycomedi.git] / pycomedi / test_comedi.py
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
5
6 #set the paths so python can find the comedi module
7 import sys, os, string
8 sys.path.append('./build/lib.linux-i686-2.2')
9
10 import comedi as c
11
12 #open a comedi device
13 dev=c.comedi_open('/dev/comedi0')
14 if not dev: raise Exception, "Error openning Comedi device"
15
16 #get a file-descriptor for use later
17 fd = c.comedi_fileno(dev)
18
19 nscans=100 #specify total number of scans
20 #1000
21 #three lists containing the chans, gains and referencing
22 #the lists must all have the same length
23 chans=[0,2,3]
24 gains=[1,1,1]
25 aref =[c.AREF_GROUND, c.AREF_GROUND, c.AREF_GROUND]
26
27 nchans = len(chans) #number of channels
28
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
31
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])
36
37 #construct a comedi command manually
38 cmd = c.comedi_cmd_struct()
39 cmd.subdev = 0
40 cmd.flags = 0
41 cmd.start_src = c.TRIG_NOW
42 cmd.sart_arg = 0
43 cmd.scan_begin_src = c.TRIG_TIMER
44 cmd.scan_begin_arg = int(1.0e9/100000)
45 cmd.convert_src = c.TRIG_TIMER
46 cmd.convert_arg = 1
47 cmd.scan_end_src = c.TRIG_COUNT
48 cmd.scan_end_arg = nchans
49 cmd.stop_src = c.TRIG_COUNT
50 cmd.stop_arg = nscans
51 cmd.chanlist = mylist
52 cmd.chanlist_len = nchans
53
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
61
62 #execute the command!
63 ret = c.comedi_command(dev,cmd)
64
65 #I think this will work but it's completely untested!
66 datalist=[]
67 buffersize=1000 # max bytes to acquire per read attempt
68 data=os.read(fd, buffersize)
69 while len(data)>0:
70         print "read %d bytes" % len(data)
71         datalist.append(data)
72         data=os.read(fd, buffersize)
73 if len(datalist) == 0 :
74         raise Exeption, "Didn't get any data..."
75
76 ret = c.comedi_close(dev)
77
78 print datalist
79 datastr = string.join(datalist,'')
80
81 print datastr #heres your data, as a single string!
82
83 #if you've got Numeric installed you can convert data into a flat Numpy array
84 # with:
85 # dataarray = Numeric.fromstring(data, Int16)
86 try :
87         import numpy
88         dataarray = numpy.fromstring(data, dtype=numpy.int16)
89         print "size: ", dataarray.size
90         print dataarray
91 except ImportError :
92         print "Install numpy to read the data into Python"