Making a CSV files with data of Fibre rolls

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:

I already get a CSV file, but its not each roll. It makes different lines for the same roll:
image

Does somebody know what I do wrong?

Thanks for the help.

Kind regards

There are a few things that I see.

  1. 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.
  2. 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.

  1. 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.
  2. When building a new list, there is no reason to append the items, just initialize the list inline as I have done.
  3. 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)
  1. 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.
4 Likes

Hi Irose,

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:

What do I need to put in the Tags tab:

Again, thanks for your time. I appreciate it.

Kind regards

You’ll also need to add the tag you want to monitor ([Bontexgeo Kft]Reset) to the list of tag paths (your last screenshot).

And there’s a small mistake in the script: line 11, it should be tagValues, not tagValus.

1 Like

Hi Pascal,

Thanks for the reply.
Ok I added this in the tag tab.
When I try to run the script in the script console I have next fault:

Kind regards

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])

Hi Pascal,

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.

Thanks!

Typo has been corrected. Thanks for the catch.

Hi,

So the machine is running and I see on the local server that the tag change is executed:

But, if I look on the server, where the csv file needs to be saved, I see nothing:
image

The file name is TIS_TEST_(date).

Any idea how this comes?

Thanks!

If I look, I have this error in the log activity:

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])
1 Like

Hi Irose,

Thanks for the answer.
If I change it I get next screen:

Kind regards

I found it (TAB and SPACE problem)

Hi @lrose,

I changed the script like you told me:

But, I still have the same error:


Any idea how it comes?

Thanks!

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.

1 Like

Hi,
Yes you were right.
The error is gone, but now I have next error:


I changed the “create CSV” with “system.dataset.exportCSV()”, and now he does not recognize this one.

Kind regards

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.

You are going to want to use system.dataset.toCSV - Ignition User Manual 8.1 - Ignition Documentation instead

1 Like

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.

1 Like

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.

Thanks, copy paste. Didn’t take the time to remove the import. Gone now.

1 Like