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.