def read_buffer(aichIC, aichEC, aichFR, gainIC, gainEC, connic, connec, connfr, plot_sample, ftmpIC, ftmpEC, ftmpFR, databstr_old=b''): databstr_full = b'' t0 = time.time() while True: buffersize = c.comedi_get_buffer_contents(aichIC.dev, aichIC.subdev) if buffersize >= READBUFFER: try: databstr_full += os.read(aichIC.fd, aichIC.maxbuffer) except: sys.stdout.write("NICOMEDI: Reading from device failed\n") sys.stdout.flush() if buffersize < 0: sys.stdout.write("NICOMEDI: Warning: buffer error\n") comedi_errcheck() break elif buffersize > aichIC.maxbuffer/2: sys.stdout.write("NICOMEDI: Warning: buffer is more than half full\n") elif buffersize <= MINBUFFER: break # t1 = time.time() # sys.stdout.write("R {0:.02f}\n".format(t1-t0)) if databstr_full == b'': # time.sleep(5e-4) return databstr_old databstr_full = databstr_old + databstr_full buffer12 = int(len(databstr_full)/12) * 12 databstr = databstr_full[:buffer12] databstr_rem = databstr_full[buffer12:] # t2 = time.time() # sys.stdout.write("C {0:.02f}\n".format(t2-t1)) tmpIC, tmpEC, frames = databstr2np(databstr, [gainIC, gainEC], [aichIC, aichEC, aichFR]) # t3 = time.time() # sys.stdout.write("N {0:.02f}\n".format(t3-t2)) ftmpIC.write(tmpIC.tostring()) ftmpIC.flush() ftmpEC.write(tmpEC.tostring()) ftmpEC.flush() ftmpFR.write(frames.tostring()) ftmpFR.flush() # t4 = time.time() # sys.stdout.write("F {0:.02f}\n".format(t4-t3)) if gPlot: try: connic.send(tmpIC[::plot_sample].tostring()) connec.send(tmpEC[::plot_sample].tostring()) connfr.send(frames[::plot_sample].tostring()) except: sys.stdout.write("NICOMEDI: Error communicating with scope\n") sys.stdout.write('.') sys.stdout.flush() return databstr_rem
def read_buffer(aichIC, aichEC, aichFR, gainIC, gainEC, connic, connec, connfr, plot_sample, ftmpIC, ftmpEC, ftmpFR, databstr_old=b''): buffersize = c.comedi_get_buffer_contents(aichIC.dev, aichIC.subdev) if buffersize < 0: sys.stdout.write("NICOMEDI: Warning: buffer error\n") comedi_errcheck() if buffersize > aichIC.maxbuffer/2: sys.stdout.write("NICOMEDI: Warning: buffer is more than half full\n") if buffersize >= 12288: try: databstr_full = os.read(aichIC.fd, aichIC.maxbuffer) except: sys.stdout.write("NICOMEDI: Reading from device failed\n") sys.stdout.flush() databstr_full = b'' databstr_full = databstr_old + databstr_full buffer12 = int(len(databstr_full)/12) * 12 databstr = databstr_full[:buffer12] databstr_rem = databstr_full[buffer12:] tmpIC, tmpEC, frames = databstr2np(databstr, [gainIC, gainEC], [aichIC, aichEC, aichFR]) ftmpIC.write(tmpIC.tostring()) ftmpIC.flush() ftmpEC.write(tmpEC.tostring()) ftmpEC.flush() ftmpFR.write(frames.tostring()) ftmpFR.flush() if gPlot: try: connic.send(tmpIC[::plot_sample].tostring()) connec.send(tmpEC[::plot_sample].tostring()) connfr.send(frames[::plot_sample].tostring()) except: sys.stdout.write("NICOMEDI: Error communicating with scope\n") sys.stdout.write('.') sys.stdout.flush() return databstr_rem else: time.sleep(1e-4) return databstr_old
print 'time_limit' , time_limit t0 = time.time() pause = float(packetSize)/nchans/freq/4 print 'pause' , pause alldata = numpy.empty((time_limit/2) , dtype = 'i2') 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"
raise Exception("error executing comedi_command") front = 0 back = 0 of = open("stream_log.bin", "wb") format = "H" flag = 1 time_limit = nchans * freq * 2 * secs # stop scan after "secs" seconds t0 = time.time() while flag: front += c.comedi_get_buffer_contents(dev, subdevice) ## print("front = ", front) if front > time_limit: flag = 0 t1 = time.time() # reached "secs" seconds if (front < back): print("front<back") print("ERROR comedi_get_buffer_contents") break if (front == back): time.sleep(.01) continue DATA = array.array("H") # reset array to empty ## nbytes = front - back ## chunk_limit = (nbytes / size) * size # count for "size" sized chunks ## remainder = nbytes%size # bytes left after chunk_limit chunks done
raise "error executing comedi_command" front = 0 back = 0 of = open("stream_log.bin","wb") format = "H" flag = 1 time_limit = nchans*freq*2*secs # stop scan after "secs" seconds t0 = time.time() while flag: front += c.comedi_get_buffer_contents(dev,subdevice) ## print "front = ", front if front > time_limit: flag = 0 t1 = time.time() # reached "secs" seconds if (front<back): print "front<back" print "ERROR comedi_get_buffer_contents" break if (front==back): time.sleep(.01) continue DATA = array.array("H") # reset array to empty ## nbytes = front - back ## chunk_limit = (nbytes / size) * size # count for "size" sized chunks ## remainder = nbytes%size # bytes left after chunk_limit chunks done
def read_buffer(aichIC, aichEC, aichFR, gainIC, gainEC, connic, connec, connfr, plot_sample, ftmpIC, ftmpEC, ftmpFR, databstr_old=b''): databstr_full = b'' t0 = time.time() while True: buffersize = c.comedi_get_buffer_contents(aichIC.dev, aichIC.subdev) if buffersize >= READBUFFER: try: databstr_full += os.read(aichIC.fd, aichIC.maxbuffer) except: sys.stdout.write("NICOMEDI: Reading from device failed\n") sys.stdout.flush() if buffersize < 0: sys.stdout.write("NICOMEDI: Warning: buffer error\n") comedi_errcheck() break elif buffersize > aichIC.maxbuffer / 2: sys.stdout.write( "NICOMEDI: Warning: buffer is more than half full\n") elif buffersize <= MINBUFFER: break # t1 = time.time() # sys.stdout.write("R {0:.02f}\n".format(t1-t0)) if databstr_full == b'': # time.sleep(5e-4) return databstr_old databstr_full = databstr_old + databstr_full buffer12 = int(len(databstr_full) / 12) * 12 databstr = databstr_full[:buffer12] databstr_rem = databstr_full[buffer12:] # t2 = time.time() # sys.stdout.write("C {0:.02f}\n".format(t2-t1)) tmpIC, tmpEC, frames = databstr2np(databstr, [gainIC, gainEC], [aichIC, aichEC, aichFR]) # t3 = time.time() # sys.stdout.write("N {0:.02f}\n".format(t3-t2)) ftmpIC.write(tmpIC.tostring()) ftmpIC.flush() ftmpEC.write(tmpEC.tostring()) ftmpEC.flush() ftmpFR.write(frames.tostring()) ftmpFR.flush() # t4 = time.time() # sys.stdout.write("F {0:.02f}\n".format(t4-t3)) if gPlot: try: connic.send(tmpIC[::plot_sample].tostring()) connec.send(tmpEC[::plot_sample].tostring()) connfr.send(frames[::plot_sample].tostring()) except: sys.stdout.write("NICOMEDI: Error communicating with scope\n") sys.stdout.write('.') sys.stdout.flush() return databstr_rem
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()
def hs_collect_data(self): self.stop_event.clear() front = 0 back = 0 hs_data = [] self.log_hs_data_start_time = datetime.datetime.utcnow() while not self.stop_event.isSet(): time_now = datetime.datetime.utcnow() dt = time_now - self.log_hs_data_start_time self.ls_progress = (dt.seconds + dt.microseconds*1.0e-6)*100.0/self.ls_period 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)%8 != 0: #print front,back front = front-4 if front == back: time.sleep(.01) continue print back,front,(front-back),(front%self.buffer_size) self.map.seek(back%self.buffer_size) for i in range(back,front,4): if i%self.buffer_size == 0: self.map.seek(0) hs_data.append(self.map.read(4)) c.comedi_mark_buffer_read(self.dev,self.subdevice,front-back) back = front #get remaining data front += c.comedi_get_buffer_contents(self.dev,self.subdevice) if (front-back)%8 != 0: #print front,back front = front-4 if front > back: print "ending",back,front,(front-back),(front%self.buffer_size) self.map.seek(back%self.buffer_size) for i in range(back,front,4): if i%self.buffer_size == 0: self.map.seek(0) hs_data.append(self.map.read(4)) self.comedi_reset() #And process the high speed data temp_data = [] for d in hs_data: temp_data.append(struct.unpack("I",d)[0]) temp_data = array(temp_data) temp_data = temp_data.reshape(len(temp_data)/2,2) temp_fb = self.convert_to_real(self.adc_gain,temp_data[:,0]) temp_sa = self.convert_to_real(self.adc_gain,temp_data[:,1]) self.fb_uf.extend(temp_fb) self.sa_uf.extend(temp_sa) self.ls_progress = 100 #And record the data if self.dlog.file_name is not None: self.dlog.add_hs_data(list(self.sa_uf),list(self.fb_uf),self.hs_freq, meta=self.hs_meta) #finally restart ls data self.take_ls_data()