etherlab/runtime_etherlab.py
changeset 2120 8e6e6364087e
parent 2116 2b1980a038b1
child 2132 9f5e4dc43053
equal deleted inserted replaced
2119:5460eba6c5e0 2120:8e6e6364087e
     1 import subprocess,sys,ctypes
     1 import 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 
     5 
     5 SDOAnswered = PLCBinary.SDOAnswered
     6 SDOAnswered = PLCBinary.SDOAnswered
     6 SDOAnswered.restype = None
     7 SDOAnswered.restype = None
     7 SDOAnswered.argtypes = []
     8 SDOAnswered.argtypes = []
     8 
     9 
    10 Result = None
    11 Result = None
    11 
    12 
    12 def SDOThreadProc(*params):
    13 def SDOThreadProc(*params):
    13     global Result
    14     global Result
    14     if params[0] == "upload":
    15     if params[0] == "upload":
    15         command = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
    16         cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
    16     else:
    17     else:
    17         command = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
    18         cmdfmt = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
    18     
    19     
    19     proc = subprocess.Popen(command % params[1:], stdout=subprocess.PIPE, shell=True)
    20     command = cmdfmt % params[1:]
       
    21     proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    20     res = proc.wait()
    22     res = proc.wait()
    21     output = proc.communicate()[0]
    23     output = proc.communicate()[0]
    22     
    24     
    23     if params[0] == "upload":
    25     if params[0] == "upload":
    24         Result = None
    26         Result = None
    33                     Result = int(dec_value)
    35                     Result = int(dec_value)
    34     else:
    36     else:
    35         Result = res == 0
    37         Result = res == 0
    36     
    38     
    37     SDOAnswered()
    39     SDOAnswered()
       
    40     if res != 0 :
       
    41         PLCObject.LogMessage(
       
    42             LogLevelsDict["WARNING"], 
       
    43             "%s : %s"%(command,output))
    38     
    44     
    39 def EthercatSDOUpload(pos, index, subindex, var_type):
    45 def EthercatSDOUpload(pos, index, subindex, var_type):
    40     global SDOThread
    46     global SDOThread
    41     SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex])
    47     SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex])
    42     SDOThread.start()
    48     SDOThread.start()
    71         log = s.value[:l-1]
    77         log = s.value[:l-1]
    72         if last :
    78         if last :
    73             log = log.rpartition(last)[2]
    79             log = log.rpartition(last)[2]
    74         if log : 
    80         if log : 
    75             last = log.rpartition('\n')[2]
    81             last = log.rpartition('\n')[2]
    76             for msg in re.findall(r'<\d>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
    82             for lvl,msg in re.findall(
    77                                   log, re.MULTILINE):
    83                             r'<(\d)>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
    78                 PLCObject.LogMessage(msg)
    84                             log, re.MULTILINE):
       
    85                 PLCObject.LogMessage(
       
    86                     LogLevelsDict[{
       
    87                         "4":"WARNING",
       
    88                         "3":"CRITICAL"}.get(lvl,"DEBUG")],
       
    89                     msg)
    79         time.sleep(0.5) 
    90         time.sleep(0.5) 
    80 
    91 
    81 def _runtime_etherlab_init():
    92 def _runtime_etherlab_init():
    82     global KMSGPollThread, StopKMSGThread
    93     global KMSGPollThread, StopKMSGThread
    83     StopKMSGThread = False
    94     StopKMSGThread = False
    87 def _runtime_etherlab_cleanup():
    98 def _runtime_etherlab_cleanup():
    88     global KMSGPollThread, StopKMSGThread
    99     global KMSGPollThread, StopKMSGThread
    89     StopKMSGThread = True
   100     StopKMSGThread = True
    90     KMSGPollThread = None
   101     KMSGPollThread = None
    91 
   102 
    92