# HG changeset patch
# User Laurent Bessard
# Date 1366667662 -7200
# Node ID 9f5e4dc4305390b2d4bf5d02aca1d83f2f3de4e6
# Parent  504f18b3306a616436157bd449b87c992410744c
Added support for stopping SDOThreadProc when stopping PLC

diff -r 504f18b3306a -r 9f5e4dc43053 etherlab/runtime_etherlab.py
--- a/etherlab/runtime_etherlab.py	Mon Apr 22 09:30:48 2013 +0200
+++ b/etherlab/runtime_etherlab.py	Mon Apr 22 23:54:22 2013 +0200
@@ -1,4 +1,4 @@
-import subprocess,sys,ctypes
+import os,subprocess,sys,ctypes
 from threading import Thread
 import ctypes,time,re
 from targets.typemapping import LogLevelsDict
@@ -8,19 +8,20 @@
 SDOAnswered.argtypes = []
 
 SDOThread = None
+SDOProc = None
 Result = None
 
 def SDOThreadProc(*params):
-    global Result
+    global Result, SDOProc
     if params[0] == "upload":
         cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
     else:
         cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
     
     command = cmdfmt % params[1:]
-    proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
-    res = proc.wait()
-    output = proc.communicate()[0]
+    SDOProc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
+    res = SDOProc.wait()
+    output = SDOProc.communicate()[0]
     
     if params[0] == "upload":
         Result = None
@@ -96,7 +97,12 @@
     KMSGPollThread.start()
 
 def _runtime_etherlab_cleanup():
-    global KMSGPollThread, StopKMSGThread
+    global KMSGPollThread, StopKMSGThread, SDOProc, SDOThread
+    try:
+        os.kill(SDOProc.pid, SIGTERM)
+    except:
+        pass
+    SDOThread = None
     StopKMSGThread = True
     KMSGPollThread = None