Updating a PLC tag based on SQL Table value


I have a table that shows the status and production numbers of all of our equipment. Each machine has its own row that gets updated periodically.

I want to monitor the value in a “Reset” column. When the value goes TRUE, I want to set a PLC integer tag to 0 for that particular machine. Right now, I’ve got a query tag that pulls the dataset with a script on value change that looks like the following:

	ds = currentValue.value
	for i in range(ds.getRowCount()):
		if ds.getValueAt(i, 'Reset') > 0:
			if ds.getValueAt(i,'MachineName') == 'Equipment':
				system.tag.writeBlocking("[~]Equipment/Part Total",0)

However, it’s not working and I’m at a loss as to where to start. The query looks like it’s running fine, but when I update the Reset flag, nothing happens. What am I missing?

It may be that writeBlocking expects a list of tagPaths and values opposed to wirte which expected a string path and value

I would also refactor the script a bit for personal preference and readability, assuming this is all of the script.

ds = currentValue.value
for i in range(ds.getRowCount()):
    if ds.getValueAt(i,'Reset') and ds.getValueAt(i,'MachineName') == 'Equipment':
         system.tag.writeBlocking(['[~]Equipment/Part Total'],[0])

Of course if you have the SQL Bridge module, Transaction Groups are perfect for this type of thing.

Ah, I had formatted it that way because ultimately this will be used with multiple machines.

I also have the SQL Bridge module, but I’ve never used it to monitor a SQL table to change a tag. Only used it to update a table based on PLC values. Do you know of any documentation that explains how it works in that direction?

For information on using SQL Bridge for this, look into the DB to OPC Update Mode.

Transaction Group Update Modes

If you decide to do it in script, I would suggest something along these lines to build up the list, that way you are only calling the writeBlocking function once. This will significantly improve the speed that the script can run.

ds = currentValue.value
tagValues = []
tagPaths = []

for i in range(ds.getRowCount()):
    if ds.getValueAt(i,'Reset'):
        tagPaths.append('[~]%s/Part Total' % (ds.getValueAt(i,'MachineName')))

1 Like

Got it! Much appreciated. Scripting is new for me so still learning all of those fine details.