计算消息之间的时间 CANBUS

Count time between messages CANBUS

我无法为我的脚本想出一个可行的时间计数器。

timeit.timeit() 会大大降低性能,time.clock 不断增加(随着时钟时间的增加而不断增加)并使用数据流中的内置计时方法提供不一致的结果。

请参阅下面的代码以及我的 2 次尝试。

#!/usr/bin/python

import subprocess,time, timeit
import re, os, pprint, math
from collections import defaultdict
from Tkinter import *

DICT = {}
COUNT = defaultdict(int)
CURRSTAMP = 0

def RECEIVE(COMMAND):
    PROCESS = subprocess.Popen(COMMAND, stdout=subprocess.PIPE)
    LINES = iter(PROCESS.stdout.readline, "")
    try:
        START = time.clock() - constantly increases and does not set back to 0 after iteration
        #TIMER = "0"
        for LINE in LINES:
            if re.match(r"^\d+.*$",LINE):
                SPLITLINE = LINE.split()
#               CURRSTAMP = float(TIMER)
                del SPLITLINE[1:4]
                TIMER = SPLITLINE[0]
    #           TIMING =(float(TIMER) - CURRSTAMP) * 1000
                IDENTIFIER = SPLITLINE[1]
                COUNT[IDENTIFIER] += 1
                END = time.clock()
                TIMING = (END - START) *1000
                DLC = SPLITLINE[2]
                HEXBITS = SPLITLINE[3:]
                DICT[IDENTIFIER] = [DLC, HEXBITS, COUNT[IDENTIFIER],TIMING]
                time.sleep(0.005)
                for IDENTIFIER, HEXBITS in DICT.items():
                    os.system("clear")
                    pprint.pprint(DICT, width = 150)
        START = 0
    except KeyboardInterrupt:
        pass

time.clock() 似乎具有我需要的执行速度,但我必须遗漏一些东西,因为它在每个迭代器上递增 .10。

数据:

receivetest Version "Release_20090203_n"  (www.peak-system.com)
------- Copyright (C) 2004-2009 PEAK System-Technik GmbH ------
receivetest comes with ABSOLUTELY NO WARRANTY.     This is free
software  and you are welcome  to redistribute it under certain
conditions.   For   details   see    attached   COPYING   file.

receivetest: device node="/dev/pcan32"
             Only standard frames are accepted, init with 500 kbit/sec.
receivetest: driver version = Release_20141017_n
8827710.77 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8827710.546 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8827711.16 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8827711.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8827810.45 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8827810.514 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8827810.984 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8827811.453 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8827910.56 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8827910.525 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8827910.994 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8827911.464 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828010.66 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828010.536 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828011.5 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828011.474 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828110.77 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828110.546 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828111.16 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828111.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828210.45 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828210.514 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828210.984 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828211.453 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828310.184 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828310.610 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828311.80 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828311.549 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828410.66 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828410.536 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828411.5 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828411.474 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828510.77 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828510.546 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828511.16 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828511.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828610.88 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828610.557 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828610.984 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828611.496 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828710.98 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828710.568 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828711.37 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828711.506 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828810.109 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828810.578 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828811.48 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828811.517 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828910.120 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828910.546 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828911.16 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828911.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829010.88 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829010.557 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829011.26 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829011.496 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829110.98 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829110.568 receivetest: m s 0x00000690 8 00 18 00 00 00 83 59 1a 
8829111.37 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829111.506 receivetest: m s 0x00000691 8 01 38 9f 65 50 5a 06 00 
8829111.976 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829210.109 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829210.578 receivetest: m s 0x00000691 8 02 3f 03 67 0c 03 0a 00 
8829211.48 receivetest: m s 0x00000690 8 00 18 00 00 00 83 59 1a 
8829211.517 receivetest: m s 0x00000663 8 2f 00 62 02 00 03 0a 00 
8829211.986 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829310.120 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829310.589 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829311.58 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829311.528 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829410.88 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829410.557 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829411.26 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829411.496 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829425.746 receivetest: m s 0x00000688 8 02 00 00 5b 86 6a d2 ac 
8829510.141 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829510.610 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829511.37 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829511.549 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829610.109 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829610.578 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829611.48 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829611.517 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 

如有任何帮助,我们将不胜感激。

问题是您正在获取开始时间,然后算出该时间与每次迭代的结束时间之间的差异。您需要将开始时间设置为上一次迭代的结束时间,以获取自那以后的时间。

start = time.clock()
for line in lines:
   ...
   end = time.clock()
   timeTaken = end - start
   start = end

示例:

time:  1     2     3     4     5
event: start       i1          i2

你在做什么:

i1:    ------------> (2)
i2:    ------------------------> (4)

你想要什么:

i1:    ------------> (2)
i2:                 -----------> (2)