Continuous Sampling with Python --> Buffer index

Applications, development tools, FPGA, C, WEB
Post Reply
rafbar
Posts: 4
Joined: Thu Jan 07, 2016 12:45 pm

Continuous Sampling with Python --> Buffer index

Post by rafbar » Fri Dec 23, 2016 2:16 am

Hi all

I'm wondering, if the buffer index of the Pitaya is starting with 0 and ending with 16383 or (1,16384)??
Besides, do you see any errors in my code, sampling one Channel of 4 available (Yes, I got 2 Pitayas:) ) I assumed, that the buffer starts at position 1 and ends at 16384.

Thanks in advance
Chrys

def RedPitaya_WritetoFile ():
global rp_s, buffersize_pitaya, pointernow, pointerold, Log_Pitaya_file

rp_s.tx_txt('ACQ:WPOS?') #ask for the actual pointer position
pointerold = pointernow
pointernow = int(rp_s.rx_txt())

if pointernow > pointerold:
nsamples = pointernow - pointerold
rp_s.tx_txt('ACQ:SOUR1:DATA:STA:N? '+str(pointerold)+','+str(nsamples))
buff_string = rp_s.rx_txt() #get data
buff_string = buff_string.strip('{}\n\r').replace(" ", "").split(',')
buff = list(map(float, buff_string)) #data in float format

for item in buff:
Log_Pitaya_file.write("%s\n" % item)
else:
nsamples1 = buffersize_pitaya - pointerold+1; #buffersize = 16384
nsamples2 = pointernow;
rp_s.tx_txt('ACQ:SOUR1:DATA:STA:N? '+str(pointerold)+','+str(nsamples1))
buff_string1 = rp_s.rx_txt() #get data
rp_s.tx_txt('ACQ:SOUR1:DATA:STA:N? 1,' +str(nsamples1))
buff_string2 = rp_s.rx_txt() #get data
buff_string1 = buff_string1.strip('{}\n\r').replace(" ", "").split(',')
buff1 = list(map(float, buff_string1)) #data in float format
for item in buff1:
Log_Pitaya_file.write("%s\n" % item)
buff_string2 = buff_string2.strip('{}\n\r').replace(" ", "").split(',')
buff2 = list(map(float, buff_string2)) #data in float format
for item in buff2:
Log_Pitaya_file.write("%s\n" % item)

def RedPitaya_Connect ():
global rp_s, samplingrate_pitaya, buffersize_pitaya, pointernow, pointerold, actual_time_pitaya, old_time_pitaya
##OPEN CONNECTION TO RED PITAYA 1
rp_s = scpi.scpi('192.168.1.110')
decimation = 65536
buffersize_pitaya = 16384 #Buffersize [S]
samplingrate_pitaya = 125e6/decimation #Sampling rate [S/s]
rp_s.tx_txt('ACQ:RST') #reset aquire
rp_s.tx_txt('ACQ:DEC ' + str(decimation)) #set the decimation value
rp_s.tx_txt('ACQ:SOUR1:GAIN HV') #set voltage level according to jumpers (HV max 20V)
rp_s.tx_txt('ACQ:SOUR2:GAIN HV') #set voltage level according to jumpers (HV max 20V)
rp_s.tx_txt('ACQ:TRIG:LEV 0') #Trigger Level: 0 default = 0
rp_s.tx_txt('ACQ:TRIG:DLY 8192') #Trigger Delay: 8192 Samples default = 0

##Start the first acquisition
rp_s.tx_txt('ACQ:START') #Starts acquisition
rp_s.tx_txt('ACQ:TRIG DISABLED') #disabled --> continuous sampling
rp_s.tx_txt('ACQ:WPOS?') #ask for the actual pointer position
actual_time_pitaya = int(round(time.time() * 1000)) #in ms
old_time_pitaya = actual_time_pitaya
pointernow = int(rp_s.rx_txt())

def RedPitaya_StartLogging ():
global Log_Pitaya, Log_Pitaya_file, rp_s, samplingrate_pitaya, buffersize_pitaya, pointernow, pointerold, actual_time_pitaya, old_time_pitaya
Log_Pitaya = 1
##########Red pitaya start logging
RedPitaya_Connect () #CONNECTS TO THE PITAYAS AND OPENS A STREAM

actual_time = strftime("%Y%m%d", gmtime())
tmp_time = datetime.datetime.time(datetime.datetime.now()).strftime('%H_%M_%S')
actual_time = actual_time + "_" + tmp_time

Log_Pitaya_file = open("LogFiles/Pitaya_LogFiles/Pitaya_Log" + actual_time + ".txt","w+")
Log_Pitaya_file.write("Pitaya Log File from %s\n" % actual_time) #header1
Log_Pitaya_file.write("Samplingfrequency: %s [S/s]\n" % samplingrate_pitaya) #header2
Log_Pitaya_file.write("Ch1_1\tCh2_1\tCh1_2\tCh2_2\n") #header3

print "Start RedPitaya Logging: " + actual_time

def RedPitaya_StopLogging ():
global Log_Pitaya_file, rp_s, Log_Pitaya
Log_Pitaya = 0
Log_Pitaya_file.close() #close logfile stream
rp_s.tx_txt('ACQ:STOP') #Stops acquisition

#plot.plot(buff)
#plot.ylabel('Voltage')
#plot.show()
print "Stop RedPitaya Logging!"

Nils Roos
Posts: 1441
Joined: Sat Jun 07, 2014 12:49 pm
Location: Königswinter

Re: Continuous Sampling with Python --> Buffer index

Post by Nils Roos » Tue Dec 27, 2016 3:45 pm

Hi Chrys,

the RP api and the scpi-server use 0-based indices, so you'll need to change that in your program.
Also, there's a small error in this part of the code (the last line should use "nsamples2"):

Code: Select all

rp_s.tx_txt('ACQ:SOUR1:DATA:STA:N? '+str(pointerold)+','+str(nsamples1))
buff_string1 = rp_s.rx_txt() #get data
rp_s.tx_txt('ACQ:SOUR1:DATA:STA:N? 1,' +str(nsamples1))

rafbar
Posts: 4
Joined: Thu Jan 07, 2016 12:45 pm

Re: Continuous Sampling with Python --> Buffer index

Post by rafbar » Sun Jan 01, 2017 4:45 pm

Thank you very much for the reply and error searching!!

There is an other problem with continuous sampling... My GUI works fine but the redpitaya_scpi.py tends to hang randomly after a wile. I think the while(1) loop in rx_txt:
How can I timeout this loop? I tried several stuff with loopcounters but non worked. I think when it hangs, it hangs in the line with chunk = self._socket.recv(.......).decode(...)

def rx_txt(self, chunksize = 4096):
"""Receive text string and return it after removing the delimiter."""
msg = ''
while 1:
chunk = self._socket.recv(chunksize + len(self.delimiter)).decode('utf-8') # Receive chunk size of 2^n preferably
msg += chunk
if (len(chunk) and chunk[-2:] == self.delimiter):
break
return msg[:-2]

Best Chrys

Nils Roos
Posts: 1441
Joined: Sat Jun 07, 2014 12:49 pm
Location: Königswinter

Re: Continuous Sampling with Python --> Buffer index

Post by Nils Roos » Mon Jan 02, 2017 12:04 am

I don't know much about python, but it's probably possible to set a receive timeout on the "_socket".

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: No registered users and 15 guests