etherlab/runtime_etherlab.py
changeset 2114 fc1bc441cf71
parent 2086 8e4992e0f147
child 2115 edb49073227e
equal deleted inserted replaced
2113:b14b6f9008dc 2114:fc1bc441cf71
     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 
     4 
     4 SDOAnswered = PLCBinary.SDOAnswered
     5 SDOAnswered = PLCBinary.SDOAnswered
     5 SDOAnswered.restype = None
     6 SDOAnswered.restype = None
     6 SDOAnswered.argtypes = []
     7 SDOAnswered.argtypes = []
     7 
     8 
    46     SDOThread.start()
    47     SDOThread.start()
    47 
    48 
    48 def GetResult():
    49 def GetResult():
    49     global Result
    50     global Result
    50     return Result
    51     return Result
       
    52 
       
    53 KMSGPollThread=None
       
    54 StopKMSGThread=False
       
    55 def KMSGPollThreadProc():
       
    56     """
       
    57     Logs Kernel messages starting with EtherCAT
       
    58     Uses GLibc wrapper to Linux syscall "klogctl"
       
    59     Last 4 KB are polled, and lines compared to last 
       
    60     captured line to detect new lines
       
    61     """
       
    62     global StopKMSGThread
       
    63     libc=ctypes.CDLL("libc.so.6")
       
    64     klog = libc.klogctl
       
    65     klog.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int]
       
    66     klog.restype = ctypes.c_int
       
    67     s=ctypes.create_string_buffer(4*1024)
       
    68     last = None
       
    69     while not StopKMSGThread:
       
    70         l = klog(3,s,len(s)-1)
       
    71         log = s.value[:l-1]
       
    72         if last :
       
    73             log = log.rpartition(last)[2]
       
    74         if log : 
       
    75             last = log.rpartition('\n')[2]
       
    76             for msg in re.findall(r'<\d>\[\s*\d*\.\d*\]\s*(EtherCAT\s*.*)$',
       
    77                                   log, re.MULTILINE):
       
    78                 PLCObject.LogMessage(msg)
       
    79         time.sleep(0.5) 
       
    80 
       
    81 def _runtime_etherlab_init():
       
    82     global KMSGPollThread, StopKMSGThread
       
    83     StopKMSGThread = False
       
    84     KMSGPollThread = Thread(target = KMSGPollThreadProc)
       
    85     KMSGPollThread.start()
       
    86 
       
    87 def _runtime_etherlab_cleanup():
       
    88     global KMSGPollThread, StopKMSGThread
       
    89     StopKMSGThread = True
       
    90     KMSGPollThread = None
       
    91 
       
    92