etherlab/runtime_etherlab.py
changeset 2132 9f5e4dc43053
parent 2116 2b1980a038b1
child 2353 8f1a2846b2f5
child 2641 c9deff128c37
equal deleted inserted replaced
2131:504f18b3306a 2132:9f5e4dc43053
     1 import subprocess,sys,ctypes
     1 import os,subprocess,sys,ctypes
     2 from threading import Thread
     2 from threading import Thread
     3 import ctypes,time,re
     3 import ctypes,time,re
     4 from targets.typemapping import LogLevelsDict
     4 from targets.typemapping import LogLevelsDict
     5 
     5 
     6 SDOAnswered = PLCBinary.SDOAnswered
     6 SDOAnswered = PLCBinary.SDOAnswered
     7 SDOAnswered.restype = None
     7 SDOAnswered.restype = None
     8 SDOAnswered.argtypes = []
     8 SDOAnswered.argtypes = []
     9 
     9 
    10 SDOThread = None
    10 SDOThread = None
       
    11 SDOProc = None
    11 Result = None
    12 Result = None
    12 
    13 
    13 def SDOThreadProc(*params):
    14 def SDOThreadProc(*params):
    14     global Result
    15     global Result, SDOProc
    15     if params[0] == "upload":
    16     if params[0] == "upload":
    16         cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
    17         cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
    17     else:
    18     else:
    18         cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
    19         cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
    19     
    20     
    20     command = cmdfmt % params[1:]
    21     command = cmdfmt % params[1:]
    21     proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    22     SDOProc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    22     res = proc.wait()
    23     res = SDOProc.wait()
    23     output = proc.communicate()[0]
    24     output = SDOProc.communicate()[0]
    24     
    25     
    25     if params[0] == "upload":
    26     if params[0] == "upload":
    26         Result = None
    27         Result = None
    27         if res == 0:
    28         if res == 0:
    28             if params[2] in ["float", "double"]:
    29             if params[2] in ["float", "double"]:
    94     StopKMSGThread = False
    95     StopKMSGThread = False
    95     KMSGPollThread = Thread(target = KMSGPollThreadProc)
    96     KMSGPollThread = Thread(target = KMSGPollThreadProc)
    96     KMSGPollThread.start()
    97     KMSGPollThread.start()
    97 
    98 
    98 def _runtime_etherlab_cleanup():
    99 def _runtime_etherlab_cleanup():
    99     global KMSGPollThread, StopKMSGThread
   100     global KMSGPollThread, StopKMSGThread, SDOProc, SDOThread
       
   101     try:
       
   102         os.kill(SDOProc.pid, SIGTERM)
       
   103     except:
       
   104         pass
       
   105     SDOThread = None
   100     StopKMSGThread = True
   106     StopKMSGThread = True
   101     KMSGPollThread = None
   107     KMSGPollThread = None
   102 
   108