Reading tags

How to Read values from Text file and assign them back to plc tags??? :prayer:

Can you give an example of a couple lines of the the csv file long with the tagnames you want to write to?

here is the text file i hav attached. from this file i wanna read each value separately and want to assign them to plc tags
idle.txt (92 Bytes)

Ok, give me about a half-hour… :slight_smile:

Does this file get appended to, so there is a new line every entry?

no the file wont get appended… only the values ll b updated in one line of text file and i want to read these values in ignition and want to assign them to plc tags

Okay, then. This will read a one-liner csv file and return the value as a list.

[code]
def readCsvAsList(path):
from java.io import BufferedReader,FileReader,File
br = BufferedReader(FileReader(path))

data = line.split(’,’)

return data[/code]

Then you con use the system.tag.witeToTags function to do the rest:

taglist=readCsvAsList("C:\idle.txt") system.tag.writeToTags([Tag1,Tag2,Tag3,...],taglist)

Edit: Whoops! Wrong script!

[quote=“JordanCClark”]Okay, then. This will read a one-liner csv file and return the value as a list.

[code]
def readCsvAsList(path):
from java.io import BufferedReader,FileReader,File
br = BufferedReader(FileReader(path))

data = line.split(’,’)

return data[/code]

Then you con use the system.tag.witeToTags function to do the rest:

taglist=readCsvAsList("C:\idle.txt") system.tag.writeToTags([Tag1,Tag2,Tag3,...],taglist)

Edit: Whoops! Wrong script![/quote]

Ill bet this doesn’t work. Where is line set. Might want to close br or you will leak memory.

viewtopic.php?f=88&t=7885

[quote=“Kyle Chase”]
Ill bet this doesn’t work. Where is line set. Might want to close br or you will leak memory.[/quote]

Yep, you’re right. Should be more like this:

[code]def readCsvAsList(path):
from java.io import BufferedReader,FileReader,File
br = BufferedReader(FileReader(path))

line = br.readLine()

data = line.split(’,’)

br.close()

return data[/code]

Thanks for the catch!

This wont work with his current file. readLine requires either a carriage return or a line feed, which his file does not have.

That would be true if we were reading multiple lines. Since we only read one line, it really doesn’t matter if it terminates in a CR, LF, or EOF


I stand corrected…

No worries here. Actually, I love these discussions. If it helps us to to stretch ourselves, then it’s all worth while! :smiley:

The only other thing I would worry about is properly closing the file. Right now, you have no safety if an error occurs after opening the file.

In Java, I would do the following

try{
   BufferedReader reader = new BufferedReader(new FileReader(path));
   //parse data
}catch (IOException e){
   LOGGER.info("Something went wrong",e);
} finally {
   reader.close();
   reader = null;
}

This should work, but doesnt. Maybe Jython 2.1 has no finally clause?

[code]
def readCsvAsList(path):
from java.io import BufferedReader,FileReader,File,IOException
try:
br = BufferedReader(FileReader(path))

  line = br.readLine()

  data = line.split(',')

except IOException:
log something
finally:
br.close()

return data[/code]

instead of using system.tag.writeToTag function for each tag path separately i want to use system.tag.writeToTags function coz i wanna read 300 tags from a text file.

:scratch: