0

I am a beginner at Python Programming. What I am essentially trying to create is a program that will sample the voltage from an ADC every millisecond, put this data into a matrix, and then export it to an Excel file. Right now I'm using placeholders for the ADC data. Here's what I've got so far:

import xlsxwriter
import time
import numpy
import threading

a = numpy.array(['Time (ms)','Current (A)'])    #Writes header values, sets up array a
maxtime = 200   #Limit for how long the program will run, in ms
elapsed = 0

def readdata(maxtime):
    global elapsed
    global a
    if elapsed <= maxtime:
        threading.Timer(0.01, readdata)
        elapsed +=1
        b = numpy.array(['Test1', 'Test2']) #'Test1' and 'Test2' Will eventually be replaced with ADC data
        a = numpy.concatenate((a, b), axis=0)   #Combines the arrays
    else:
        generatespreadsheet(a)


def generatespreadsheet():
    global a
    workbook = xlsxwriter.Workbook(time.strftime("%Y%m%d-%H%M%S") + ".xlsx") #Define workbook name as date and time
    worksheet = workbook.add_worksheet()    #Adds the first sheet
    row = 0

    for col, data in enumerate(a):
        worksheet.write_row(col, row, data) #write the array in Excel
    workbook.close()

readdata(maxtime)

The issue I'm having is that the code appears to do nothing at all. Not even throwing error codes. Any help is greatly appreciated!

UPDATE: I have switeched over to using a generator, code follows:

    import xlsxwriter
import time
import numpy
import threading

a = numpy.array(['Time (ms)','Current (A)'])    #Writes header values, sets up array a
maxtime = 200   #Limit for how long the program will run, in ms
elapsed = 0

def do_every(period,f):
    def g_tick():

        t = time.time()
        count = 0
        while True:
            count += 1
            yield max(t + count*period - time.time(),0)
    g = g_tick()
    while True:
        time.sleep(next(g))
        f()
        global elapsed
        global maxtime
        elapsed += 1
        if elapsed >= maxtime
            generatespreadsheet()



def readdata():
    global a
    b = numpy.array(['Test1', 'Test2']) #'Test1' and 'Test2' Will eventually be replaced with ADC data
    a = numpy.concatenate((a, b), axis=0)   #Combines the arrays




def generatespreadsheet():
    global a
    workbook = xlsxwriter.Workbook(time.strftime("%Y%m%d-%H%M%S") + ".xlsx") #Define workbook name as date and time
    worksheet = workbook.add_worksheet()    #Adds the first sheet
    row = 0

    for col, data in enumerate(a):
        worksheet.write_row(col, row, data) #write the array in Excel
    workbook.close()
    exit()


do_every(0.001,readdata)
  • 1
    I have not worked much with pythonic threads but feels like you are missing the start operation here. `Some_thread.start()` – mad_ Aug 07 '18 at 14:26
  • Possible duplicate of [Executing periodic actions in Python](https://stackoverflow.com/questions/8600161/executing-periodic-actions-in-python) – mad_ Aug 07 '18 at 14:28
  • you need to start your thread. do https://docs.python.org/3.7/library/threading.html#timer-objects . But are you sure you want to start so many threads? at a glance it looks like you will end up with about 200 threads. Not sure if you machine can handle that... My advise would be to change the logic... – Bayko Aug 07 '18 at 15:13
  • Thanks for your help @bayko, though I'm not sure where or how to implement this in the code. – Gus Collier Aug 07 '18 at 15:23

0 Answers0