Count time between messages CANBUS

0

I'm having trouble coming up with a viable time counter for my script.

timeit.timeit() slows performance too much, time.clock constantly increase ( as gather as this is running off an ever increase clock time) and using the in-built timing method from the data stream is providing inconsistant results.

Please see code below with 2 of my attempts.

#!/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() seems to have the speed of execution I need but I must be missing something as it increments by .10 on each iterator.

DATA:

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 

Any help would be much appreciated.

python
linux
time
timer
data-stream
asked on Stack Overflow Jan 7, 2015 by Jalcock501

1 Answer

1

The issue is that you are getting the start time, then working out the difference between that and the end time of each iteration. You need to set the start time to the end time of the previous iteration to get the time since that.

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

Example:

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

What you are doing:

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

What you want:

i1:    ------------> (2)
i2:                 -----------> (2)
answered on Stack Overflow Jan 7, 2015 by Gareth Latty • edited Jan 7, 2015 by Gareth Latty

User contributions licensed under CC BY-SA 3.0