RTXI 1.3
comedilib/demo/python/cmd.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 ## It emulates the program "cmd" which is distributed with
00003 ## the comedilib software
00004 ## Copyright (C) May 2003  Luc Lefebvre
00005 ## luc.lefebvre@mcgill.ca
00006 ## This program is free software; you can redistribute it and/or
00007 ## modify it under the terms of the GNU General Public License
00008 ## as published by the Free Software Foundation; either version 2
00009 ## of the License, or (at your option) any later version.
00010 
00011 ## This program is distributed in the hope that it will be useful,
00012 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 ## GNU General Public License for more details.
00015 
00016 
00017 #set the paths so python can find the comedi module
00018 import sys, os, string, struct, time
00019 sys.path.append('./build/lib.linux-i586-2.1')
00020 
00021 import comedi as c
00022 
00023 #open a comedi device
00024 dev=c.comedi_open('/dev/comedi0')
00025 if not dev: raise "Error openning Comedi device"
00026 
00027 #get a file-descriptor for use later
00028 fd = c.comedi_fileno(dev)
00029 if fd<=0: raise "Error obtaining Comedi device file descriptor"
00030 
00031 BUFSZ = 10000
00032 freq=1000 # as defined in demo/common.c
00033 subdevice=0 #as defined in demo/common.c
00034 nscans=8000 #specify total number of scans
00035 
00036 #three lists containing the chans, gains and referencing
00037 #the lists must all have the same length
00038 chans=[0,1,2,3]
00039 gains=[0,0,0,0]
00040 aref =[c.AREF_GROUND, c.AREF_GROUND, c.AREF_GROUND, c.AREF_GROUND]
00041 
00042 cmdtest_messages = [
00043         "success",
00044         "invalid source",
00045         "source conflict",
00046         "invalid argument",
00047         "argument conflict",
00048         "invalid chanlist"]
00049 
00050 nchans = len(chans) #number of channels
00051 
00052 #wrappers include a "chanlist" object (just an Unsigned Int array) for holding the chanlist information
00053 mylist = c.chanlist(nchans) #create a chanlist of length nchans
00054 
00055 #now pack the channel, gain and reference information into the chanlist object
00056 #N.B. the CR_PACK and other comedi macros are now python functions
00057 for index in range(nchans):
00058         mylist[index]=c.cr_pack(chans[index], gains[index], aref[index])
00059 
00060 def dump_cmd(cmd):
00061         print "---------------------------"
00062         print "command structure contains:"
00063         print "cmd.subdev : ", cmd.subdev
00064         print "cmd.flags : ", cmd.flags
00065         print "cmd.start :\t", cmd.start_src, "\t", cmd.start_arg
00066         print "cmd.scan_beg :\t", cmd.scan_begin_src, "\t", cmd.scan_begin_arg
00067         print "cmd.convert :\t", cmd.convert_src, "\t", cmd.convert_arg
00068         print "cmd.scan_end :\t", cmd.scan_end_src, "\t", cmd.scan_end_arg
00069         print "cmd.stop :\t", cmd.stop_src, "\t", cmd.stop_arg
00070         print "cmd.chanlist : ", cmd.chanlist
00071         print "cmd.chanlist_len : ", cmd.chanlist_len
00072         print "cmd.data : ", cmd.data
00073         print "cmd.data_len : ", cmd.data_len
00074         print "---------------------------"
00075 
00076 ## ret = c.comedi_get_buffer_size(dev, subdevice)
00077 ## if ret==-1:
00078 ##      raise "Error fetching comedi buffer size"
00079 ## else:
00080 ##      print "buffer size = ", ret
00081 ## ret = c.comedi_get_max_buffer_size(dev, subdevice)
00082 ## if ret==-1:
00083 ##      raise "Error fetching comedi max buff size"
00084 ## else:
00085 ##      print "max buff size = ", ret
00086 #construct a comedi command
00087 cmd = c.comedi_cmd_struct()
00088 
00089 ret = c.comedi_get_cmd_generic_timed(dev,subdevice,cmd,1.0e9/freq)
00090 if ret: raise "Error comedi_get_cmd_generic failed"
00091         
00092 cmd.chanlist = mylist # adjust for our particular context
00093 cmd.chanlist_len = nchans
00094 cmd.scan_end_arg = nchans
00095 if cmd.stop_src==c.TRIG_COUNT: cmd.stop_arg=nscans
00096 
00097 print "command before testing"
00098 dump_cmd(cmd)
00099 
00100 #test our comedi command a few times. 
00101 ret = c.comedi_command_test(dev,cmd)
00102 print "first cmd test returns ", ret, cmdtest_messages[ret]
00103 if ret<0: raise "comedi_command_test failed"
00104 dump_cmd(cmd)
00105 ret = c.comedi_command_test(dev,cmd)
00106 print "second test returns ", ret, cmdtest_messages[ret]
00107 if ret<0: raise "comedi_command_test failed"
00108 if ret !=0:
00109         dump_cmd(cmd)
00110         raise "Error preparing command"
00111 
00112 #execute the command!
00113 ## ret = c.comedi_command(dev,cmd)
00114 ## if ret !=0: raise "comedi_command failed..."
00115 
00116 datastr = ()
00117 t0 = time.time()
00118 ret = c.comedi_command(dev,cmd)
00119 if ret !=0: raise "comedi_command failed..."
00120 while (1):
00121         #ret = c.comedi_poll(dev,subdevice)
00122         #print "poll ret = ", ret
00123         data = os.read(fd,BUFSZ)
00124         #print "len(data) = ", len(data)
00125         if len(data)==0:
00126                 break
00127         n = len(data)/2 # 2 bytes per 'H'
00128         format = `n`+'H'
00129         #print "format = ", format
00130         #bytes = struct.calcsize(format)
00131         #print "bytes = ", bytes
00132         #nbytes = c.comedi_get_buffer_contents(dev,subdevice)
00133         #print "n = ", n, " nbytes = ", nbytes
00134         datastr = datastr + struct.unpack(format,data)
00135 
00136 t1 = time.time()
00137 print "start time : ", t0
00138 print "end time : ", t1
00139 print "time : ", t1 - t0, " seconds"
00140 
00141 count = 0
00142 while count < len(datastr):
00143         for i in range(4):
00144                 print "%d\t" % datastr[count+i],
00145         print "\n"
00146         count = count + 4
00147         
00148 ret = c.comedi_close(dev)
00149 if ret !=0: raise "comedi_close failed..."
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines