forked from johannesPettersson80/codesys-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_codesys_path.py
More file actions
163 lines (137 loc) · 6.11 KB
/
debug_codesys_path.py
File metadata and controls
163 lines (137 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
CODESYS Path Debugging Tool
This script checks if the CODESYS executable is accessible and can be launched.
It helps diagnose issues with CODESYS integration.
"""
import os
import sys
import subprocess
import time
# Import the path from HTTP_SERVER.py
try:
from HTTP_SERVER import CODESYS_PATH
print(f"Imported CODESYS_PATH from HTTP_SERVER.py: {CODESYS_PATH}")
except ImportError:
CODESYS_PATH = r"C:\Program Files\CODESYS 3.5.21.0\CODESYS\Common\CODESYS.exe"
print(f"Using default CODESYS_PATH: {CODESYS_PATH}")
def check_codesys_path():
"""Check if the CODESYS executable exists and can be accessed."""
print("\n== CODESYS Path Check ==")
# Check if path exists
if os.path.exists(CODESYS_PATH):
print(f"✓ CODESYS executable found at: {CODESYS_PATH}")
else:
print(f"✗ CODESYS executable NOT found at: {CODESYS_PATH}")
print("\nPossible reasons:")
print("1. The path is incorrect")
print("2. CODESYS is not installed")
print("3. CODESYS is installed in a different location")
return False
# Check if path is a file
if os.path.isfile(CODESYS_PATH):
print("✓ Path points to a file")
else:
print("✗ Path does not point to a file")
return False
# Check if file is executable
try:
# Get file info
file_size = os.path.getsize(CODESYS_PATH) / (1024 * 1024) # Size in MB
print(f"✓ File size: {file_size:.2f} MB")
# Try to get version info if on Windows
if os.name == 'nt':
try:
import win32api
info = win32api.GetFileVersionInfo(CODESYS_PATH, '\\')
ms = info['FileVersionMS']
ls = info['FileVersionLS']
version = f"{win32api.HIWORD(ms)}.{win32api.LOWORD(ms)}.{win32api.HIWORD(ls)}.{win32api.LOWORD(ls)}"
print(f"✓ File version: {version}")
except:
print("i Could not retrieve file version information")
except Exception as e:
print(f"✗ Error accessing file: {str(e)}")
return False
print("\n== CODESYS Execution Test ==")
print("Attempting to launch CODESYS with --help option...")
try:
# Try to launch CODESYS with help flag (should exit quickly)
start = time.time()
process = subprocess.Popen(
[CODESYS_PATH, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Wait for a few seconds
try:
stdout, stderr = process.communicate(timeout=5)
stdout = stdout.decode('utf-8', errors='replace') if stdout else ""
stderr = stderr.decode('utf-8', errors='replace') if stderr else ""
# Check output
if stdout:
print(f"✓ CODESYS launched successfully with output")
print("\nOutput excerpt:")
lines = stdout.split('\n')
for line in lines[:10]: # Print first 10 lines
if line.strip():
print(f" {line}")
if len(lines) > 10:
print(f" ... (total {len(lines)} lines)")
elif stderr:
print(f"✗ CODESYS launched but had errors")
print("\nError output:")
print(stderr)
else:
print("✓ CODESYS launched with no output")
# Check exit code
exit_code = process.returncode
if exit_code == 0:
print(f"✓ CODESYS exited with code 0 (success)")
else:
print(f"✗ CODESYS exited with code {exit_code}")
print(f"✓ Total execution time: {time.time() - start:.2f} seconds")
except subprocess.TimeoutExpired:
process.kill()
print("✗ CODESYS process did not exit within timeout period (5s)")
print(" This might be normal for some CODESYS versions")
except Exception as e:
print(f"✗ Failed to launch CODESYS: {str(e)}")
return False
print("\n== Script Path Check ==")
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
PERSISTENT_SCRIPT = os.path.join(SCRIPT_DIR, "PERSISTENT_SESSION.py")
if os.path.exists(PERSISTENT_SCRIPT):
print(f"✓ PERSISTENT_SESSION.py found at: {PERSISTENT_SCRIPT}")
# Check script size and content
try:
with open(PERSISTENT_SCRIPT, 'r') as f:
content = f.read()
print(f"✓ Script size: {len(content)} bytes")
# Check for some key functions
if "def initialize(self):" in content:
print("✓ initialize() function found in script")
else:
print("✗ initialize() function NOT found in script")
if "def execute_script(self, script_path):" in content:
print("✓ execute_script() function found in script")
else:
print("✗ execute_script() function NOT found in script")
except Exception as e:
print(f"✗ Error reading script: {str(e)}")
else:
print(f"✗ PERSISTENT_SESSION.py NOT found at: {PERSISTENT_SCRIPT}")
print("\n== Summary ==")
print("The CODESYS executable appears to be accessible.")
print("If you're still experiencing issues, check:")
print("1. CODESYS version compatibility")
print("2. Script permissions")
print("3. Network connectivity and firewall settings")
print("4. Check logs for detailed error messages")
return True
if __name__ == "__main__":
print("=== CODESYS Integration Debug Tool ===")
print(f"Python version: {sys.version}")
print(f"Current directory: {os.getcwd()}")
check_codesys_path()