EXPORT DATA IN CSV FILE

I been trying to save input data to a csv file, I managed to save some data but not enough because the program crashes, aborted.
how I can get to keep a csv file while capturing it?
the visualization features of the signal is not important, it is important to keep the saved data.

in qt.py file it is where he became the only pair to save the data change in csv file.

class OpenADCQt(QObject):
       dataUpdated = Signal(list, int)
    def __init__(self, MainWindow=None, includePreview=True, setupLayout=True, includeParameters=True, console=None, showScriptParameter=None):
        super(OpenADCQt,  self).__init__()
        self.console = console
        self.offset = 0.5
        self.ser = None
        self.sc = None
        self.datapoints = []
        self.preview = None
        self.adc_settings = None
        self.timerStatusRefresh = QTimer(self)
        self.timerStatusRefresh.timeout.connect(self.statusRefresh)
        self.showScriptParameter = showScriptParameter

        if setupLayout:
            self.setupLayout(MainWindow, includePreview, includeParameters)
    def setEnabled(self, enabled):
        print("self.oa.arm-set Enabled")
        pass

    def statusRefresh(self):
        print("self.oa.arm-statusRefresh")
        pass

    def setupLayout(self, MainWindow, includePreview=True,  includeParameters=True):
        print("self.oa.arm-setupLayout")
        vlayout = QVBoxLayout()
        self.tb = QToolBox()
        vlayout.addWidget(self.tb)

        ###### Graphical Preview Window
        if includePreview:
            self.preview = previewWindow()
            self.preview.addDock(MainWindow)
            vlayout.addWidget(self.preview.getSetupWidget())

        if includeParameters:
            self.setupParameterTree()
            self.paramDock = QDockWidget("Settings")
            self.paramDock.setAllowedAreas(Qt.BottomDockWidgetArea | Qt.RightDockWidgetArea|        
     Qt.LeftDockWidgetArea)
            self.paramDock.setWidget(self.paramTree)
            MainWindow.addDockWidget(Qt.LeftDockWidgetArea, self.paramDock)

        self.masterLayout = vlayout
        self.setEnabled(False)

    def getLayout(self):
        print("self.oa.arm-getLayout")
        return self.masterLayout

    def paramTreeChanged(self, param, changes):
        print("self.oa.arm-paramTreeChanged")
        if self.showScriptParameter is not None:
            self.showScriptParameter(param, changes, self.params)

    def setupParameterTree(self, makeTree=True):
        print("self.oa.arm-setupParameterTree")
        if self.adc_settings is None:
            self.adc_settings = openadc.OpenADCSettings(self.console)
            self.params = Parameter.create(name='OpenADC', type='group', children=self.adc_settings.parameters(doUpdate=False))
            #ExtendedParameter.setupExtended(self.params)
            ep = ExtendedParameter()
            ep.setupExtended(self.params, self)

            #todo: this is a somewhat insane way to cut through the layers
            self.adc_settings.setFindParam(self.findParam)

            if makeTree:
                self.paramTree = ParameterTree()
                self.paramTree.setParameters(self.params, showTop=False)

    def reloadParameterTree(self):
        print("self.oa.arm-reloadParameterTree")
        self.adc_settings.setInterface(self.sc)
        self.params.blockTreeChangeSignal()
        self.params.getAllParameters()
        self.params.unblockTreeChangeSignal()

    def processData(self, data):
        print("self.oa.arm-processData")      
        fpData = []

        for b in data:
         #lastpt = -100;

        if data[0] != 0xAC:
            self.showMessage("Unexpected sync byte: 0x%x"%data[0])
            return None

        for i in range(2, len(data)-1, 2):
             
            if (0x80 & data[i + 1]) or ((0x80 & data[i + 0]) == 0):
                self.statusBar().showMessage("Error at byte %d"%i)
                print("Bytes: %x %x"%(data[i], data[i+1]))
                return None
            #Convert
            intpt = data[i + 1] | ((data[i+0] & 0x07) << 7)

            #input validation test: uncomment following and use
            #ramp input on FPGA
            ##if (intpt != lastpt + 1) and (lastpt != 0x3ff):
            ##    print "intpt: %x lstpt %x\n"%(intpt, lastpt)
            ##lastpt = intpt;

            fpData.append(float(intpt) / 1024.0 - self.offset)
        return fpData

    def arm(self):
        print("self.oa.arm-arm") 
        self.sc.arm()

    def read(self, update=True, NumberPoints=None):
              
        print("ya llego a la lectura---new")
        print("self.oa.arm-read")
        
        if NumberPoints == None:
            NumberPoints = self.adc_settings.parm_trigger.maxSamples()
        progress = QProgressDialog("Reading", "Abort", 0, 100)
        progress.setWindowModality(Qt.WindowModal)
        progress.setMinimumDuration(1000)
        try:
            self.datapoints = self.sc.readData(NumberPoints, progress)
             ##################################################################
            longitud=len(self.datapoints)
            counter=0
            while(counter<longitud):
                f=open("data.csv",'a')
                writer=csv.writer(f,delimiter=',')
                writer.writerow([counter,self.datapoints[counter]])
                f.close
                counter=counter+1
            #################################################################                
        except IndexError, e:
            raise IOError("Error reading data: %s"%str(e))
        except Exception as f:
            print f
        self.dataUpdated.emit(self.datapoints, -self.adc_settings.parm_trigger.presamples(True))

—>Dear Colin O’Flynn need to know how I can save the data arrived from the FPGA, I’ve tried, but only keeps a small group of values, fraction values.
—>In that part of the code would be the correct way to store the data? that variable would be the right to keep all the data.
—>The only change I’ve made is in the qt.py file, as shown in the previous message.
—>I want to know what the chain variables sent and received through the serial port between FPGA and Python.
—>Dou you have a video or link or document or manual help me to better understand the transmission and
reception of data between openadc and FPGA?
—>The only change I’ve made is in qt.py file, attachment.

thank you for your help :wink:
openadc.zip (43.6 KB)

Hello there,

Did you try adding this at the end of the “processData” function? It may be that “return fpData” is the ticket. Let me try and setup a basic test here and see if it works…

-Colin

Oops - ignore that! The processData function in qt.py is never called, I was thinking about the other file. Anyway I think you almost have it, but try the following:

try:
            self.datapoints = self.sc.readData(NumberPoints, progress)
            ##################################################################
            longitud = len(self.datapoints)
            counter = 0
            data = ""
            while(counter < longitud):
                data += "%d, %f\n" % (counter, self.datapoints[counter])
                counter = counter + 1
            f = open('test.csv', 'w')
            f.write(data)
            f.close()
        ##################################################################
        except IndexError, e:
            raise IOError("Error reading data: %s"%str(e))

Hopefully that makes sense - I included some lines of code around what you had already

It is ok, but f = open(‘test.csv’, ‘a’) is correct.
Dou you have a video or link or document or manual help me to better understand the transmission and
reception of data between Python and FPGA?

Hi Edward,

That should download the file in chunks of approx 25000 samples. There isn’t a ton of documentation besides what is in the code right now unfortunately…

that’s right, I can save limit frequency signals up to 10 - 15 MHz.

could increase the sampling rate of FPGA with the DCM 100 MHz? it’s possible? :sunglasses:

Hello,

Ah yes it’s possible! I think there is the “CLKGEN” option already in there that you might be able to set to 100 MHz? It should work fine at that frequency…