etherlab/runtime_etherlab.py
changeset 2115 edb49073227e
parent 2114 fc1bc441cf71
child 2116 2b1980a038b1
equal deleted inserted replaced
2114:fc1bc441cf71 2115:edb49073227e
     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()
    87 def _runtime_etherlab_cleanup():
    93 def _runtime_etherlab_cleanup():
    88     global KMSGPollThread, StopKMSGThread
    94     global KMSGPollThread, StopKMSGThread
    89     StopKMSGThread = True
    95     StopKMSGThread = True
    90     KMSGPollThread = None
    96     KMSGPollThread = None
    91 
    97 
    92