Reading PLC tags offline

If I don’t have a PLC but I have the project, most of our tags are within UDTs which are already developed, so these are fairly easy to create manually or using a tool that takes in a list of instance names and parameters and creates the udt instances for me.

Without a PLC, in v8, you can convert tags between opc and memory without any information being removed (this wasn’t in the case in v7).

I use this script, just copied into the script console, to convert tag json copied into the clipboard, between OPC & memory types. Actual memory tags or any other tags won’t be affected, only those that were originally OPC will be affected.

Just comment in/out the bottom two lines, then paste back into the tag browser (need to right click, paste - Ctrl+V doesn’t work for tags). It works for tags as well as UDT definitions and their tags.

from java.awt.datatransfer import StringSelection
from java.awt.datatransfer import Clipboard
from java.awt import Toolkit 
from java.awt.datatransfer import DataFlavor

### clipboard functions ###
def setup():
    global toolkit
    global clipboard
    toolkit =  Toolkit.getDefaultToolkit()
    clipboard = toolkit.getSystemClipboard()

def writeClipboard(text):
	setup()
	clipboard.setContents(StringSelection(text), None)
	
def readClipboard():
	setup()
	contents = clipboard.getContents(None)
	return contents.getTransferData(DataFlavor.stringFlavor)
### end clipboard functions ###

def convertTagSource(obj, valSource='opc'):
	if valSource not in ['opc','memory']:
		import sys
		sys.exit()
	# if the obj is a list, then run through it and convert any "memory opc" tags to opc
	if isinstance(obj,list):
		for i in range(len(obj)):
			obj[i] = convertTagSource(obj[i], valSource)
			
	# if the obj is a dict, check if it is a tag that is opc and make sure its valueSource defines it as opc
	if isinstance(obj,dict):
		if 'opcItemPath' in obj:
			obj["valueSource"] = valSource
			
		# 
		for key in obj.keys():
			if isinstance(obj[key], dict):
				obj[key] = convertTagSource(obj[key], valSource)
			if isinstance(obj[key], list):
				for i in range(len(obj[key])):
					obj[key][i] = convertTagSource(obj[key][i], valSource)
			
	# if its just a flat type, ignore it
	else:
		pass
	return obj

def convertTagJsonTo(convertTo='opc'):
	# make sure you copy some tag json first to the clipboard!
	tagsJson = readClipboard()
	tags = system.util.jsonDecode(tagsJson)

	if convertTo in ['opc','memory']:
		for item in tags:
			tags[item] = convertTagSource(tags[item], convertTo)
	
		tags = system.util.jsonEncode(tags)
		tags = tags.replace(":True", ":true").replace(":False", ":false") # json technically doesn't capitalise first letter of bool vals.. compare tools whinge
		writeClipboard(tags)
	else:
		system.gui.errorBox("You failed at setting the value source correctly... use 'opc' or 'memory' idiot", 'Idiot')

convertTagJsonTo('memory')
#convertTagJsonTo('opc')

For Vision as well, to get rid of the red overlays, you can disable overlays in the designer:
image

1 Like