author | Edouard Tisserant |
Fri, 05 Apr 2013 21:47:37 +0900 | |
changeset 2117 | accc4cbca8d3 |
parent 2116 | 2b1980a038b1 |
child 2132 | 9f5e4dc43053 |
permissions | -rw-r--r-- |
2086
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
1 |
import subprocess,sys,ctypes |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
2 |
from threading import Thread |
2114
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
3 |
import ctypes,time,re |
2115 | 4 |
from targets.typemapping import LogLevelsDict |
2086
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
5 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
6 |
SDOAnswered = PLCBinary.SDOAnswered |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
7 |
SDOAnswered.restype = None |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
8 |
SDOAnswered.argtypes = [] |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
9 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
10 |
SDOThread = None |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
11 |
Result = None |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
12 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
13 |
def SDOThreadProc(*params): |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
14 |
global Result |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
15 |
if params[0] == "upload": |
2115 | 16 |
cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x" |
2086
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
17 |
else: |
2115 | 18 |
cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s" |
2086
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
19 |
|
2115 | 20 |
command = cmdfmt % params[1:] |
21 |
proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) |
|
2086
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
22 |
res = proc.wait() |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
23 |
output = proc.communicate()[0] |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
24 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
25 |
if params[0] == "upload": |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
26 |
Result = None |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
27 |
if res == 0: |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
28 |
if params[2] in ["float", "double"]: |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
29 |
Result = float(output) |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
30 |
elif params[2] in ["string", "octet_string", "unicode_string"]: |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
31 |
Result = output |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
32 |
else: |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
33 |
hex_value, dec_value = output.split() |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
34 |
if int(hex_value, 16) == int(dec_value): |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
35 |
Result = int(dec_value) |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
36 |
else: |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
37 |
Result = res == 0 |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
38 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
39 |
SDOAnswered() |
2115 | 40 |
if res != 0 : |
41 |
PLCObject.LogMessage( |
|
42 |
LogLevelsDict["WARNING"], |
|
43 |
"%s : %s"%(command,output)) |
|
2086
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
44 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
45 |
def EthercatSDOUpload(pos, index, subindex, var_type): |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
46 |
global SDOThread |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
47 |
SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex]) |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
48 |
SDOThread.start() |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
49 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
50 |
def EthercatSDODownload(pos, index, subindex, var_type, value): |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
51 |
global SDOThread |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
52 |
SDOThread = Thread(target=SDOThreadProc, args=["download", pos, var_type, index, subindex, value]) |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
53 |
SDOThread.start() |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
54 |
|
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
55 |
def GetResult(): |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
56 |
global Result |
8e4992e0f147
Adding block library for SDO download and SDO upload
Laurent Bessard
parents:
diff
changeset
|
57 |
return Result |
2114
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
58 |
|
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
59 |
KMSGPollThread=None |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
60 |
StopKMSGThread=False |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
61 |
def KMSGPollThreadProc(): |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
62 |
""" |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
63 |
Logs Kernel messages starting with EtherCAT |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
64 |
Uses GLibc wrapper to Linux syscall "klogctl" |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
65 |
Last 4 KB are polled, and lines compared to last |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
66 |
captured line to detect new lines |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
67 |
""" |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
68 |
global StopKMSGThread |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
69 |
libc=ctypes.CDLL("libc.so.6") |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
70 |
klog = libc.klogctl |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
71 |
klog.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int] |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
72 |
klog.restype = ctypes.c_int |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
73 |
s=ctypes.create_string_buffer(4*1024) |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
74 |
last = None |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
75 |
while not StopKMSGThread: |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
76 |
l = klog(3,s,len(s)-1) |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
77 |
log = s.value[:l-1] |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
78 |
if last : |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
79 |
log = log.rpartition(last)[2] |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
80 |
if log : |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
81 |
last = log.rpartition('\n')[2] |
2116
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
82 |
for lvl,msg in re.findall( |
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
83 |
r'<(\d)>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$', |
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
84 |
log, re.MULTILINE): |
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
85 |
PLCObject.LogMessage( |
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
86 |
LogLevelsDict[{ |
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
87 |
"4":"WARNING", |
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
88 |
"3":"CRITICAL"}.get(lvl,"DEBUG")], |
2b1980a038b1
Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents:
2115
diff
changeset
|
89 |
msg) |
2114
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
90 |
time.sleep(0.5) |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
91 |
|
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
92 |
def _runtime_etherlab_init(): |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
93 |
global KMSGPollThread, StopKMSGThread |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
94 |
StopKMSGThread = False |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
95 |
KMSGPollThread = Thread(target = KMSGPollThreadProc) |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
96 |
KMSGPollThread.start() |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
97 |
|
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
98 |
def _runtime_etherlab_cleanup(): |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
99 |
global KMSGPollThread, StopKMSGThread |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
100 |
StopKMSGThread = True |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
101 |
KMSGPollThread = None |
fc1bc441cf71
Added logging based on collecting Kernel logs
Edouard Tisserant
parents:
2086
diff
changeset
|
102 |