etherlab/runtime_etherlab.py
changeset 2355 fec77f2b9e07
parent 2353 8f1a2846b2f5
child 2357 7c67286cddbe
equal deleted inserted replaced
2354:9460872f1440 2355:fec77f2b9e07
    15     global Result, SDOProc
    15     global Result, SDOProc
    16     if params[0] == "upload":
    16     if params[0] == "upload":
    17         cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
    17         cmdfmt = "ethercat upload -p %d -t %s 0x%.4x 0x%.2x"
    18     else:
    18     else:
    19         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"
    20     
    20 
    21     command = cmdfmt % params[1:]
    21     command = cmdfmt % params[1:]
    22     SDOProc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    22     SDOProc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    23     res = SDOProc.wait()
    23     res = SDOProc.wait()
    24     output = SDOProc.communicate()[0]
    24     output = SDOProc.communicate()[0]
    25     
    25 
    26     if params[0] == "upload":
    26     if params[0] == "upload":
    27         Result = None
    27         Result = None
    28         if res == 0:
    28         if res == 0:
    29             if params[2] in ["float", "double"]:
    29             if params[2] in ["float", "double"]:
    30                 Result = float(output)
    30                 Result = float(output)
    34                 hex_value, dec_value = output.split()
    34                 hex_value, dec_value = output.split()
    35                 if int(hex_value, 16) == int(dec_value):
    35                 if int(hex_value, 16) == int(dec_value):
    36                     Result = int(dec_value)
    36                     Result = int(dec_value)
    37     else:
    37     else:
    38         Result = res == 0
    38         Result = res == 0
    39     
    39 
    40     SDOAnswered()
    40     SDOAnswered()
    41     if res != 0 :
    41     if res != 0 :
    42         PLCObject.LogMessage(
    42         PLCObject.LogMessage(
    43             LogLevelsDict["WARNING"], 
    43             LogLevelsDict["WARNING"],
    44             "%s : %s"%(command,output))
    44             "%s : %s"%(command,output))
    45     
    45 
    46 def EthercatSDOUpload(pos, index, subindex, var_type):
    46 def EthercatSDOUpload(pos, index, subindex, var_type):
    47     global SDOThread
    47     global SDOThread
    48     SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex])
    48     SDOThread = Thread(target=SDOThreadProc, args=["upload", pos, var_type, index, subindex])
    49     SDOThread.start()
    49     SDOThread.start()
    50     
    50 
    51 def EthercatSDODownload(pos, index, subindex, var_type, value):
    51 def EthercatSDODownload(pos, index, subindex, var_type, value):
    52     global SDOThread
    52     global SDOThread
    53     SDOThread = Thread(target=SDOThreadProc, args=["download", pos, var_type, index, subindex, value])
    53     SDOThread = Thread(target=SDOThreadProc, args=["download", pos, var_type, index, subindex, value])
    54     SDOThread.start()
    54     SDOThread.start()
    55 
    55 
    61 StopKMSGThread=False
    61 StopKMSGThread=False
    62 def KMSGPollThreadProc():
    62 def KMSGPollThreadProc():
    63     """
    63     """
    64     Logs Kernel messages starting with EtherCAT
    64     Logs Kernel messages starting with EtherCAT
    65     Uses GLibc wrapper to Linux syscall "klogctl"
    65     Uses GLibc wrapper to Linux syscall "klogctl"
    66     Last 4 KB are polled, and lines compared to last 
    66     Last 4 KB are polled, and lines compared to last
    67     captured line to detect new lines
    67     captured line to detect new lines
    68     """
    68     """
    69     global StopKMSGThread
    69     global StopKMSGThread
    70     libc=ctypes.CDLL("libc.so.6")
    70     libc=ctypes.CDLL("libc.so.6")
    71     klog = libc.klogctl
    71     klog = libc.klogctl
    76     while not StopKMSGThread:
    76     while not StopKMSGThread:
    77         l = klog(3,s,len(s)-1)
    77         l = klog(3,s,len(s)-1)
    78         log = s.value[:l-1]
    78         log = s.value[:l-1]
    79         if last :
    79         if last :
    80             log = log.rpartition(last)[2]
    80             log = log.rpartition(last)[2]
    81         if log : 
    81         if log :
    82             last = log.rpartition('\n')[2]
    82             last = log.rpartition('\n')[2]
    83             for lvl,msg in re.findall(
    83             for lvl,msg in re.findall(
    84                             r'<(\d)>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
    84                             r'<(\d)>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
    85                             log, re.MULTILINE):
    85                             log, re.MULTILINE):
    86                 PLCObject.LogMessage(
    86                 PLCObject.LogMessage(
    87                     LogLevelsDict[{
    87                     LogLevelsDict[{
    88                         "4":"WARNING",
    88                         "4":"WARNING",
    89                         "3":"CRITICAL"}.get(lvl,"DEBUG")],
    89                         "3":"CRITICAL"}.get(lvl,"DEBUG")],
    90                     msg)
    90                     msg)
    91         time.sleep(0.5) 
    91         time.sleep(0.5)
    92 
    92 
    93 def _runtime_etherlab_init():
    93 def _runtime_etherlab_init():
    94     global KMSGPollThread, StopKMSGThread
    94     global KMSGPollThread, StopKMSGThread
    95     StopKMSGThread = False
    95     StopKMSGThread = False
    96     KMSGPollThread = Thread(target = KMSGPollThreadProc)
    96     KMSGPollThread = Thread(target = KMSGPollThreadProc)
   103     except Exception:
   103     except Exception:
   104         pass
   104         pass
   105     SDOThread = None
   105     SDOThread = None
   106     StopKMSGThread = True
   106     StopKMSGThread = True
   107     KMSGPollThread = None
   107     KMSGPollThread = None
   108