Regarding Gateway Event Script - Tag Chabge

Hi,

I have write following script in "Script Console" & It is working fine and giving me result

import socket
import time
import re

def send_command(command, delay_time=5, timeout=30):
    host = "192.168.0.101"  # Printer's IP address
    port = 3000  # TCP port for AlphaJet printer

    s = None
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, port))
        s.settimeout(timeout)

        # Wait for "Connection Accepted" response
        response = s.recv(4096).decode()

        if "Connection Accepted" in response:
            s.sendall(command.encode())
            response = s.recv(4096).decode()
        else:
            return None

        time.sleep(delay_time)
        return response

    except socket.timeout:
        return None

    except Exception as e:
        return None

    finally:
        if s:
            s.close()

def extract_label_name(response):
    match = re.search(r'<LOADLAB>(.*?)</LOADLAB>', response)
    return match.group(1) if match else "N/A"

def extract_data(response):
    cable_match = re.search(r'aPvn="CABLE NUMBER">(\d+)', response)
    jacket_match = re.search(r'aPvn="jacket line ">(\d+)', response)

    cable = cable_match.group(1) if cable_match else "N/A"
    jacket = jacket_match.group(1) if jacket_match else "N/A"

    return cable, jacket

def write_to_ignition_tags(label_name, cable, jacket):
    try:
        system.tag.writeBlocking([
            "[Sample_Tags]Label_Name",
            "[Sample_Tags]Cable_Number",
            "[Sample_Tags]Jacket_Line"
        ], [label_name, cable, jacket])
    except Exception as e:
        print("Failed to write tags: {}".format(e))

# Step 1: Get the label name
cmd_loadlab = """<GP><LOADLAB/></GP>"""
response_loadlab = send_command(cmd_loadlab)

if response_loadlab:
    label_name = extract_label_name(response_loadlab)

    # Step 2: Use the label name to get details
    cmd_version = """<GP><LOADLAB aPrompt="1">\\{}</LOADLAB></GP>""".format(label_name)
    response_version = send_command(cmd_version, delay_time=10)

    # Step 3: Extract and write data to Ignition tags
    if response_version:
        cable, jacket = extract_data(response_version)
        print("\nExtracted Information:")
        print("Label Name: {}".format(label_name))
        print("CABLE NUMBER: {}".format(cable))
        print("jacket line: {}".format(jacket))
        write_to_ignition_tags(label_name, cable, jacket)

Response is,

Extracted Information:
Label Name: label\12 FIBER MLT DUCT-LITE 2024.txt
CABLE NUMBER: 0457
jacket line: 1
>>>

Also I getting the same information in memory tag as well.
(Pls see attached file Capture1.PNG)

but when I use the same script on the "gateway event script", It is not working means memory Tag is not updating.
(Pls see attached file Capture2.PNG , Capture3.PNG & Capture4.PNG)

Can anyone tell me what is wrong here ?
How Can I correct it ?
Pls note that I am new to the Ignition.



I am attaching here "Capture4.PNG" file.

A gateway tag change script is running on the gateway. Does the Ignition Gateway service on whatever your host OS is:

  1. Have access to the relevant IP address
  2. Have access to the relevant port
  3. Have the ability to open raw sockets in the OS

Are there any errors in the gateway's logs?

I have installed the Ignition gateway on my Laptop so I am using the "localhost:8088" to access the gateway.

Please find the error details,

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 62, in File "", line 11, in send_command NameError: global name 'socket' is not defined

at org.python.core.Py.NameError(Py.java:259)

at org.python.core.PyFrame.getglobal(PyFrame.java:265)

at org.python.pycode._pyx0.send_command$1(:35)

at org.python.pycode._pyx0.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:173)

at org.python.core.PyBaseCode.call(PyBaseCode.java:306)

at org.python.core.PyBaseCode.call(PyBaseCode.java:126)

at org.python.core.PyFunction.call(PyFunction.java:416)

at org.python.pycode._pyx0.f$0(:78)

at org.python.pycode._pyx0.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:173)

at org.python.core.PyCode.call(PyCode.java:18)

at org.python.core.Py.runCode(Py.java:1703)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:804)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:859)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:752)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:840)

at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:251)

at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:198)

at com.inductiveautomation.ignition.common.util.SerialExecutionQueue$PollAndExecute.run(SerialExecutionQueue.java:102)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: NameError: global name 'socket' is not defined

Also pls find the attached photot as well.

Please properly format the stack trace, see Wiki - how to post code on this forum.
It makes it easier for others to read. You can edit your post by clicking the penicl icon in the bottom right.

As for the cause:

It seems that its not happy with your use of socket

You should define this script in the scripting library of this project. There are some weird behaviors (most notable is the need to import system) with contexts in the event scripts.

These are almost always corrected by defining the script in the script library and calling the function from the event script.

3 Likes