import comedi as c dev = c.comedi_open('/dev/comedi0') print dev maxdata = c.comedi_get_maxdata(dev, 0, 0) print "Max Data: %d" % maxdata subdev = c.comedi_find_subdevice_by_type(dev, c.COMEDI_SUBD_AO, 0) print "Subdevice: %d" % subdev print "Locked? %d" % c.comedi_lock(dev, subdev) nChannels = c.comedi_get_n_channels(dev, subdev) print "Num Channels: %d" % nChannels crange = c.comedi_get_range(dev, 0, 0, 0) print "Range: %s" % str(crange) data = maxdata/2 # start with no speed c.comedi_data_write(dev, subdev, 0, 0, 0, data) while True: usrin = raw_input("Enter a char: ") if usrin is 'q': break elif usrin is 'o': data = data + 10 elif usrin is 'l':
def main(): # Find comedi analog device number try: with open(COMEDI_PROC_F) as f: #print(f) for line in f.readlines(): m_analog = re.match(PCI_6033E_PATTERN, line) m_digital = re.match(PCI_DIO_32HS_PATTERN, line) if m_analog: print(m_analog.group()) dev_number = m_analog.groups()[0] analog_dev_fname = "/dev/comedi" + dev_number if m_digital: print(m_digital.group()) dev_number = m_digital.groups()[0] digital_dev_fname = "/dev/comedi" + dev_number except IOError: print("Could not find file: %s" % COMEDI_PROC_F) if (f): f.close() if (analog_dev_fname): analog_dev = c.comedi_open(analog_dev_fname) if not(analog_dev): print("Unable to open analog comedi device...") ret = c.comedi_lock(analog_dev, 0) if (ret < 0): print("Could not lock comedi device") ret = c.comedi_get_max_buffer_size(analog_dev, 0) if (ret > 0): print("analog max buffer size: %i" % ret) else: print("Failed to get analog dev max buffer size...") ret = c.comedi_set_max_buffer_size(analog_dev, 0, MAX_BUFFER_SIZE) if (ret > 0): print("analog dev new buffer size: %i" % ret) else: print("Failed to analog dev set max buffer size...") c.comedi_close(analog_dev) if (digital_dev_fname): digital_dev = c.comedi_open(digital_dev_fname) if not(digital_dev): print("Unable to open digital comedi device...") ret = c.comedi_lock(digital_dev, 0) if (ret < 0): print("Could not lock digital comedi device") ret = c.comedi_get_max_buffer_size(digital_dev, 0) if (ret > 0): print("analog max buffer size: %i" % ret) else: print("Failed to get digital dev max buffer size...") ret = c.comedi_set_max_buffer_size(digital_dev, 0, MAX_BUFFER_SIZE) if (ret > 0): print("digital dev new buffer size: %i" % ret) else: print("Failed to digital dev set max buffer size...") c.comedi_close(digital_dev)
def pci_6033e_init(self, dev_name): self.dev = c.comedi_open(dev_name) if not(self.dev): self.warn_dialog("Unable to open device: " + dev_name) return(-1) ret = c.comedi_lock(self.dev, SUBDEVICE) if (ret < 0): self.warn_dialog("Could not lock comedi device") return(-1) # get a file-descriptor for use later self.fd = c.comedi_fileno(self.dev) if (self.fd <= 0): self.warn_dialog("Error obtaining Comedi device file descriptor") c.comedi_close(self.dev) return(-1) # Channel range (0-5V) if (c.comedi_range_is_chan_specific(self.dev, SUBDEVICE) != 0): self.warn_dialog("Comedi range is channel specific!") c.comedi_close(self.dev) return(-1) self.comedi_range = c.comedi_get_range(self.dev, SUBDEVICE, 0, CHAN_RANGE) self.comedi_maxdata = c.comedi_get_maxdata(self.dev, SUBDEVICE, 0) board_name = c.comedi_get_board_name(self.dev) if (board_name != "pci-6033e"): print("Opened wrong device!") # Prepare channels, gains, refs self.comedi_num_chans = NUM_CHANNELS chans = range(self.comedi_num_chans) gains = [0]*self.comedi_num_chans aref = [c.AREF_GROUND]*self.comedi_num_chans chan_list = c.chanlist(self.comedi_num_chans) # Configure all the channels! for i in range(self.comedi_num_chans): chan_list[i] = c.cr_pack(chans[i], gains[i], aref[i]) # The comedi command self.cmd = c.comedi_cmd_struct() # 1.0e9 because this number is in nanoseconds for some reason period = int(1.0e9/float(SCAN_FREQ)) # Init cmd ret = c.comedi_get_cmd_generic_timed(self.dev, SUBDEVICE, self.cmd, self.comedi_num_chans, period) if (ret): self.warn_dialog("Could not initiate command") c.comedi_close(self.dev) return(-1) # Populate command self.cmd.chanlist = chan_list self.cmd.chanlist_len = self.comedi_num_chans self.cmd.scan_end_arg = self.comedi_num_chans self.cmd.stop_src = c.TRIG_NONE self.cmd.stop_arg = 0 print("real timing: %d ns" % self.cmd.convert_arg) print("Real scan freq: %d Hz" % (1.0/(float(self.cmd.convert_arg)*32.0*1.0e-9))) #print("period: %d ns" % period) print_cmd(self.cmd) # Test command out. ret = c.comedi_command_test(self.dev, self.cmd) if (ret < 0): self.warn_dialog("Comedi command test failed!") c.comedi_close(self.dev) return(-1) print("Command test passed") return(0)
import comedi as c dev = c.comedi_open('/dev/comedi0') print dev maxdata = c.comedi_get_maxdata(dev, 0, 0) print "Max Data: %d" % maxdata subdev = c.comedi_find_subdevice_by_type(dev, c.COMEDI_SUBD_AO, 0) print "Subdevice: %d" % subdev print "Locked? %d" % c.comedi_lock(dev, subdev) nChannels = c.comedi_get_n_channels(dev, subdev) print "Num Channels: %d" % nChannels crange = c.comedi_get_range(dev, 0, 0, 0) print "Range: %s" % str(crange) data = maxdata / 2 # start with no speed c.comedi_data_write(dev, subdev, 0, 0, 0, data) while True: usrin = raw_input("Enter a char: ") if usrin is 'q': break elif usrin is 'o': data = data + 10 elif usrin is 'l':
inputSubdev = c.comedi_find_subdevice_by_type(comediDevice, c.COMEDI_SUBD_AI, 0) print "input subdevice: %d" % inputSubdev inputMaxdata = c.comedi_get_maxdata(comediDevice, inputSubdev, channel) print "Input max Data: %d" % inputMaxdata nInputChannels = c.comedi_get_n_channels(comediDevice, inputSubdev) print "Num input Channels: %d" % nInputChannels nInputRanges = c.comedi_get_n_ranges(comediDevice, inputSubdev, channel) print "number Input Ranges: %d" % nInputRanges inputRange = c.comedi_get_range(comediDevice, inputSubdev, channel, 0) print "input range: : %s" % str(inputRange) print "Input locked? %d" % c.comedi_lock(comediDevice, inputSubdev) print "***** HANDLING OUTPUT DEVICE(S) *****" outputSubdev = c.comedi_find_subdevice_by_type(comediDevice, c.COMEDI_SUBD_AO, 0) print "output subdevice: %d" % outputSubdev outputMaxdata = c.comedi_get_maxdata(comediDevice, outputSubdev, channel) print "output max Data: %d" % outputMaxdata nOutputChannels = c.comedi_get_n_channels(comediDevice, outputSubdev) print "Num output Channels: %d" % nOutputChannels nOutputRanges = c.comedi_get_n_ranges(comediDevice, outputSubdev, channel) print "number output Ranges: %d" % nOutputRanges