예제 #1
0
    def pci_6033e_get_data(self):
        # Run the command
        # ret = c.comedi_command(self.dev, self.cmd)
        # if (ret != 0):
        #     self.warn_dialog("PCI-6033E cannot collect data! Error: %d" % ret)
        #     print(c.comedi_strerror(c.comedi_errno()))
        #     return(False)
            
        data_tup = ()
        data = ""
        # Format string for struct.unpack()
        format = '32H'

        # See if we can read anything from fd (timeout 0.05 seconds).
        ret = select.select([self.fd], [], [], 0.05)
        if (not ret[0]):
            # Poll the device to try and get some data.
            cret = c.comedi_poll(self.dev, SUBDEVICE)
            if (cret < 0):
                print("comedi poll fail: %d" % ret)
        else:
            # Read some data!
            data = os.read(self.fd, BUF_SIZE)
            self.data_buf += data

        if (len(data) > 0):
            bytes_read = len(data)
            #print("Read %d bytes" % bytes_read)
            # Number of rows of data in the chunk
            r = math.floor(len(self.data_buf)/(self.comedi_num_chans*WORD_SIZE))
            for i in range(int(r)):
                #print(len(data[64*i:64*(i+1)]))
                data_tup = data_tup + struct.unpack(format, self.data_buf[64*i:64*(i+1)])
                #print(data_tup)
                #data_tup = ()
                #print(len(data))
            
            for n, point in enumerate(data_tup):
                self.analog_data[n%self.comedi_num_chans].append(point)
            for i in range(int(r)):
                self.x.append(self.x[-1] + 1.0/SCAN_FREQ)

            self.data_buf = self.data_buf[len(data_tup*2):]
            #print("LEFTOVER DATA:")
            #print(len(self.data_buf))

        return(True)
예제 #2
0
ret = c.comedi_command(dev,cmd)
if ret<0:
    raise "error executing comedi_command"


while flag:
    print 'buffer offset', c.comedi_get_buffer_offset(device, subdevice)
    print '	buffer contents', c.comedi_get_buffer_contents(device, subdevice)	
    front += c.comedi_get_buffer_contents(dev,subdevice)

    print "front = ", front
    if front > time_limit:
        flag = 0
        t1 = time.time() # reached "secs" seconds
        c.comedi_cancel(device, subdevice)
        c.comedi_poll(device, subdevice)
        c.comedi_close(dev)
        break

    if (front<back):
        t1 = time.time() # reached "secs" seconds
        print "front<back"
        print "ERROR comedi_get_buffer_contents"
        c.comedi_cancel(device, subdevice)
        c.comedi_poll(device, subdevice)
        break
    if (front==back):
        print 'sleep'
        #~ time.sleep(.001)
        time.sleep(pause)
        continue
예제 #3
0
파일: mscan.py 프로젝트: HenkPostma/DAQ
alldata = numpy.empty((time_limit/2) , dtype = 'i2')

scancount = 0
while scancount < 10:
    ret = c.comedi_command(dev,cmd)
    if ret<0:
        raise "error executing comedi_command"



    offset = c.comedi_get_buffer_offset(device, subdevice)
    print "offset", offset

    count = 0
    data = range(1)
    while c.comedi_poll(device, subdevice) != -1 and count < 1000:
        data.append(c.comedi_get_buffer_contents(device, subdevice))
        count = count+1
        
    print "count", count

    #data = c.comedi_get_buffer_contents(device, subdevice)
    print "poll ", c.comedi_poll(device, subdevice)
    print "data", data
    print type(data)

    #buf = data
    #buf = buf.astype('f')
    #buf = buf.reshape((buf.size/nchans,nchans))
    #t = numpy.arange((buf.shape[0])).astype('f')/freq
    #for i in range(nchans):
예제 #4
0
    def ls_collect_data(self):
        self.stop_event.clear()
        time_diff = datetime.timedelta(microseconds=200)
        front = 0
        back = 0
        bytes_per_reading = self.nchan_ls * 4 # 4 byte int for each reading
        while not self.stop_event.isSet():
            front += c.comedi_get_buffer_contents(self.dev,self.subdevice)
            if front < back:
                self.logger.error("front<back comedi buffer error")
                break
            if (front-back)%bytes_per_reading != 0:
                #print front,back
                front = front-(front-back)%bytes_per_reading
            if front == back:
                time.sleep(.01)
                continue
            
            self.adc_lock.acquire()
            #start = c.comedi_get_buffer_offset(self.dev,self.subdevice)
            #print start,n_count,(start+n_count)
            self.map.seek(back%self.buffer_size)
            data = []
            for i in range(back,front,4):
                if i%self.buffer_size == 0:
                    self.map.seek(0)
                data.append(struct.unpack("I",self.map.read(4))[0])
          
            dd = array(data)
            n_elements = len(dd)/self.nchan_ls
            dd = dd.reshape(n_elements, self.nchan_ls)
            c.comedi_mark_buffer_read(self.dev,self.subdevice,front-back)
            back = front
            self.adc_lock.release()

            #!!!RW247 FIX THIS!!!!!!
            temp_fb = self.convert_to_real(self.adc_gain,dd[:,0])
            temp_sa = self.convert_to_real(self.adc_gain,dd[:,1])
            temp_fb_nofilt = self.convert_to_real(self.adc_gain,dd[:,2])
            temp_sa_nofilt = self.convert_to_real(self.adc_gain,dd[:,3])
            temp_mon = self.convert_to_real(self.adc_gain,dd[:,4])
            temp_err = self.convert_to_real(self.adc_gain,dd[:,5])

            #Here we assume that the first value is first channel
            #self.data_lock.acquire()
            self.fb.extend(temp_fb)
            self.sa.extend(temp_sa)
            self.fb_nofilt.extend(temp_fb_nofilt)
            self.sa_nofilt.extend(temp_sa_nofilt)
            self.mon.extend(temp_mon)
            self.err.extend(temp_err)
            
            self.sa_logging.extend(temp_sa)
            self.fb_logging.extend(temp_fb)

            #self.data_lock.release()
            self.filter_lock.acquire()
            self.fb_temp.extend(temp_fb)
            self.sa_temp.extend(temp_sa)
                
            for i in xrange(n_elements):
                mjd = dt_to_mjd(self.adc_time)
                self.timestamps.append(mjd)
                self.ts_temp.append(mjd)
                self.mjd_logging.append(mjd)
                self.adc_time = self.adc_time + time_diff

            self.filter_lock.release()
            
        #OK so we got a stop call - Halt the command
        #Get the latest data and then reset
        n_count = c.comedi_poll(self.dev,self.subdevice)
        n_count = c.comedi_get_buffer_contents(self.dev,self.subdevice)
        print "END LS POLL", n_count
        #if n_count != 0:
        #    start = c.comedi_get_buffer_offset(self.dev,self.subdevice)
         #   data = self.map[start:(start+n_count)]
 
        self.comedi_reset()