Timer SCripts

Hello All,

I have written Timer Script to update my window numeric text field element per 1000ms. I am reading system/client current date and time and separating it into two textfields. Below is my script .

[code]import system

global currentdate
global currenttime
global currenttime1

datetime = system.tag.getTagValue("[System]Client/System/CurrentDateTime")

currentdate = system.db.dateFormat(datetime, “dd/MM/yyyy”)

currenttime = system.db.dateFormat(datetime, “HH:mm:ss”)
timeformatted = system.db.dateFormat(datetime, “HH”)
timeformatted1 = system.db.dateFormat(datetime, “:mm:ss”)
lasthr = int(timeformatted) - 1
currenttime1 = str(lasthr) + timeformatted1

window = system.gui.getWindow(“Data_Logging”)
Date = window.rootContainer.getComponent(“Date”)
Date.text = currentdate
Time = window.rootContainer.getComponent(“Time”)
Time.text = currenttime
Time1 = window.rootContainer.getComponent(“Time1”)
Time1.text = currenttime1[/code]

This script executes very well sometimes but sometimes it fails to update my on screen numeric text fields.
Anybody knows what is the problem in my code? Or is there any setting in Ignition that I am missing here.

I am using Ignition 7.3.7

Thanks,
Ashish

why dont you use expressions on the components instead of a timer script?

I agree with diat150. Why not use expressions? would be very easy to setup.

Hello,
Thank you. Can u give me some example code of expression
I have never used that.
Thanks

Next to the text property of the label, click on the grey cylinder.
Select the Expression radio button.
Enter the following and click OK.

dateFormat({[System]Client/System/CurrentDateTime},"HH:mm:ss")

As already said you should use an expression.

But that’s no reason we can’t get your script working.

I tried your script and it worked fine for me. I’m using Ignition 7.6.5-r1.

Any idea in what context the values don’t get updated? After navigation or some other action?

What components are you using exactly? Text Field components or Numeric Text Field components?

Hello nmudge,
Thanks for testing my script. I get this problem when I do some changes in development environment and save project and when I return to run time I get this error.
After saving project sometimes it works but some time it doesn’t. Then I enable and Disable the numeric text field and again save it works. I don’t know why this happens. I never made any change in my timer script since I wrote it.
I am using numeric text fields.

Thanks for the help.
Ashish

Hello all,

Thanks I got that working with expressions programming.

Hi ashish007pict,

Ignition uses Swing for its GUI components and Swing is single threaded. Using client timer scripts introduces another thread manipulating Swing components. The GUI thread is called the event dispath thread (EDT). Using the system.util.invokeLater function puts execution of code in the EDT.

I am wondering if your scripting problem will go away if you use system.util.invokeLater to execute your component code.

Here’s code that does this:

import system

datetime = system.tag.getTagValue("[System]Client/System/CurrentDateTime")

currentdate = system.db.dateFormat(datetime, "dd/MM/yyyy")

currenttime = system.db.dateFormat(datetime, "HH:mm:ss")
timeformatted = system.db.dateFormat(datetime, "HH")
timeformatted1 = system.db.dateFormat(datetime, ":mm:ss")
lasthr = int(timeformatted) - 1
currenttime1 = str(lasthr) + timeformatted1

def func(currentdate=currentdate,currenttime=currenttime,currenttime1=currenttime1):
	import system
	window = system.gui.getWindow("Data_Logging")
	Date = window.rootContainer.getComponent("Date")
	Date.text = currentdate
	Time = window.rootContainer.getComponent("Time")
	Time.text = currenttime
	Time1 = window.rootContainer.getComponent("Time1")
	Time1.text = currenttime1

system.util.invokeLater(func)

You forgot the “import system” in that function, nmudge.

Thanks adamaustin, you are right. I edited my post and added it in there.

A post was split to a new topic: Scripting question