etherlab/runtime_etherlab.py
changeset 2086 8e4992e0f147
child 2114 fc1bc441cf71
equal deleted inserted replaced
2085:ae263886ae92 2086:8e4992e0f147
       
     1 import subprocess,sys,ctypes
       
     2 from threading import Thread
       
     3 
       
     4 SDOAnswered = PLCBinary.SDOAnswered
       
     5 SDOAnswered.restype = None
       
     6 SDOAnswered.argtypes = []
       
     7 
       
     8 SDOThread = None
       
     9 Result = None
       
    10 
       
    11 def SDOThreadProc(*params):
       
    12     global Result
       
    13     if params[0] == "upload":
       
    14         command = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
       
    15     else:
       
    16         command = "ethercat download -p %d -t %s 0x%.4x 0x%.2x %s"
       
    17     
       
    18     proc = subprocess.Popen(command % params[1:], stdout=subprocess.PIPE, shell=True)
       
    19     res = proc.wait()
       
    20     output = proc.communicate()[0]
       
    21     
       
    22     if params[0] == "upload":
       
    23         Result = None
       
    24         if res == 0:
       
    25             if params[2] in ["float", "double"]:
       
    26                 Result = float(output)
       
    27             elif params[2] in ["string", "octet_string", "unicode_string"]:
       
    28                 Result = output
       
    29             else:
       
    30                 hex_value, dec_value = output.split()
       
    31                 if int(hex_value, 16) == int(dec_value):
       
    32                     Result = int(dec_value)
       
    33     else:
       
    34         Result = res == 0
       
    35     
       
    36     SDOAnswered()
       
    37     
       
    38 def EthercatSDOUpload(pos, index, subindex, var_type):
       
    39     global SDOThread
       
    40     SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex])
       
    41     SDOThread.start()
       
    42     
       
    43 def EthercatSDODownload(pos, index, subindex, var_type, value):
       
    44     global SDOThread
       
    45     SDOThread = Thread(target=SDOThreadProc, args=["download", pos, var_type, index, subindex, value])
       
    46     SDOThread.start()
       
    47 
       
    48 def GetResult():
       
    49     global Result
       
    50     return Result