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