Try this example:
import notecard
import serial
port = serial.Serial("COM4", 9600)
card = notecard.OpenSerial(port)
numRecords = 4
dataTemplate = modem_template = {"seq":11, "pressure":12.1, "runcycles":12, "faults":12, "mode": 11, "temp": 12.1 }
arrayTemplate = [dataTemplate] *numRecords
req = {"req": "note.template","file":"mydata.qo","body":{"data":arrayTemplate}}
rsp = card.Transaction(req)
modem = []
for i in range(0, numRecords):
cont = {'seq':i, 'pressure':0+i,"pressure":27.3+i, "runcycles":13+i,"faults":19+i,"mode":2+i,"temp":31.2+i}
data_to_store = {key:cont[key] for key in ['seq','pressure','runcycles','faults','mode','temp']}
modem.append(data_to_store)
req = {"req": "note.add","file":"mydata.qo","body":{"data":modem}}
rsp = card.Transaction(req)
print(rsp)
You only need to do the note.template
request when you set the template the first time, or change the template size.
Templates do require knowing the size before adding the Note to the Notefile.
You could rearrange to do the following, though there is a little overhead in storing the template description, so you may only want to do that when absolutely necessary.
import notecard
import serial
port = serial.Serial("COM4", 9600)
card = notecard.OpenSerial(port)
numRecords = 4 #could be some other integer, but not super large
dataTemplate = modem_template = {"seq":11, "pressure":12.1, "runcycles":12, "faults":12, "mode": 11, "temp": 12.1 }
modem = []
for i in range(0, numRecords):
cont = {'seq':i, 'pressure':0+i,"pressure":27.3+i, "runcycles":13+i,"faults":19+i,"mode":2+i,"temp":31.2+i}
data_to_store = {key:cont[key] for key in ['seq','pressure','runcycles','faults','mode','temp']}
modem.append(data_to_store)
#reset template size if size is different from last time.
if(numRecords != numRecordsLastTime):
arrayTemplate = [dataTemplate] *numRecords
req = {"req": "note.template","file":"mydata.qo","body":{"data":arrayTemplate}}
rsp = card.Transaction(req)
#add Note body to templated file
req = {"req": "note.add","file":"mydata.qo","body":{"data":modem}}
rsp = card.Transaction(req)
print(rsp)