Dear all,
I am quiet new in the Ignition world.
I work for a textile company. We produce rolls of geotextile.
Now I want to make a CSV file where I can see some data off each roll.
My script is as followed:
This is in a Gateway Timer Event, which may or may not be the correct event type. Since the very first thing you do is look at the Reset tag, I would probably recommend that this script be placed in a Tag Change Script.
You have the delay set a 5ms and you are using system.tag.write() to write the value.
a. 5ms is quite fast, what is the expected cycle time? Generally I would expect a timer event to run at about half the expected cycle time.
b.system.tag.write() is not only deprecated, but is also an asynchronous function. This is the real gotcha here, as almost certainly the timer is re-occurring before the write actually occurs.
I would suggest that you move this to a Tag Change event on the Reset tag. Then change your script to look something like the following:
if not initialChange and newValue.getValue():
import csv
#set a list of tag paths of all tag values needed in script and read them all at once
tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
#system.tag.readBlocking() returns a list of qualified values so this builds a list with just the values
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
#Get the current date, format it and add to the front of the tagValues list
now = system.date.now()
tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
#crete the CSV file
fileName = r"D:\TIS_TEST_{}.csv".format(now,"yyMMdd")
csv.createCSV(fileName, headerValues, tagValues)
#reset the "reset" tag. system.tag.writeBlocking blocks the thread until the write has finished.
system.tag.writeBlocking([str(event.getTagPath())],[0])
This script does exactly the same thing as your script, I have made some changes to streamline it, but the result of the script should be the same.
Always read as many tags as you can at once. system.tag.readBlocking() takes a list of tag paths and reads all tags in a single call. This is much faster than making individual calls as each call will block the thread (prevent further execution) until the read is complete. This is also true of the deprecated system.tag.read() funciton.
When building a new list, there is no reason to append the items, just initialize the list inline as I have done.
To initialize the tagValues list I use a list comprehension, in expanded form that code would look like:
tagValues = []
for qValue in system.tag.readBlocking(tagPaths):
tagValues.append(qValue.value)
Rather than using the csv package as you are, you may want to consider using the system.dataset.exportCSV() function instead. I have no reason for this other than it is a function that is native to Ignition. Of course if you’re going to use a dataset function, you could also use the system.dataset.exportExcel() if you need this in an excel format, rather than just a csv.
Thanks for the answer.
Sorry but I am really new in this world. I have not really a background in this.
If I understand good I need to copy your script here:
This script won’t work in the script console. It was made for a gateway tag change script, and that’s where it’s supposed to go. The script console doesn’t know what InitialChange and newValue are.
That being said, the error returned here is a syntax error, which means it won’t work anywhere. In this case, it seems you missed the last parenthesis when copy/pasting.
edit: This should work in the console:
import csv
trigger = system.tag.readBlocking(["[Bontexgeo Kft]Reset"])[0].value
if trigger:
#set a list of tag paths of all tag values needed in script and read them all at once
tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
#system.tag.readBlocking() returns a list of qualified values so this builds a list with just the values
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
#Get the current date, format it and add to the front of the tagValues list
now = system.date.now()
tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
#crete the CSV file
fileName = r"D:\TIS_TEST_{}.csv".format(now,"yyMMdd")
csv.createCSV(fileName, headerValues, tagValues)
#reset the "reset" tag. system.tag.writeBlocking blocks the thread until the write has finished.
system.tag.writeBlocking(["[Bontexgeo Kft]Reset"],[0])
Ok thanks for the answer.
The line is no down, so I will wait until it is back on running.
I will inform you if I get my data from each roll in a CSV file.
The error means that the csv module you are importing has no function createCSV().
I didn’t take the time to verify if how you were using the csv module was actually valid or not.
Honestly, you’re probably better off using the Ignition provided system.dataset.exportCSV() function. That would look like this:
if not initialChange and newValue.getValue():
import csv
#set a list of tag paths of all tag values needed in script and read them all at once
tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
#system.tag.readBlocking() returns a list of qualified values so this builds a list with just the values
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
#Get the current date, format it and add to the front of the tagValues list
now = system.date.now()
tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
#crete the CSV file
fileName = r"D:\TIS_TEST_{}.csv".format(now,"yyMMdd")
system.dataset.exportCSV(fileName,True,system.dataset.toDataSet(headerValues,[tagValues]))
#reset the "reset" tag. system.tag.writeBlocking blocks the thread until the write has finished.
system.tag.writeBlocking([str(event.getTagPath())],[0])
Are you sure you saved it? Anything that is italicized in the project browser is not saved. I only say this because looking at that script you have there I don’t see createCSV anywhere which makes it seem like your old version is being used.
Looking at the documentation it’s because system.dataset.exportCSV is for a Vision client scope only as part of its functionality is to ask the user where to save. This doesn’t make sense in a Gateway scope.
ugh, that one’s on me. Should have checked the scope.
if not initialChange and newValue.getValue():
#set a list of tag paths of all tag values needed in script and read them all at once
tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
#system.tag.readBlocking() returns a list of qualified values so this builds a list with just the values
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
#Get the current date, format it and add to the front of the tagValues list
now = system.date.now()
tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
#crete the CSV file
fileName = r"D:\TIS_TEST_{}.csv".format(now,"yyMMdd")
csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues,[tagValues]),True,True,False)
system.file.writeFile(filePath,csv)
#reset the "reset" tag. system.tag.writeBlocking blocks the thread until the write has finished.
system.tag.writeBlocking([str(event.getTagPath())],[0])
This script should do the trick. Make sure you verify for correct indentation after copying.
Not that it’s doing any harm, but there is no reason to import csv anymore since you don’t use it, plus the namespace gets overwritten by csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues,[tagValues]),True,True,False) so another reason to drop that line.