Developer Blog

21/10/2019 by Magnus Carlsson

Using kvmlib to read log Files from Kvaser Memorator Light HS v2

The Kvaser Memorator Light HS v2 features a single CAN channel, but two CAN message buffers that are used as ring (FIFO) buffers. One buffer logs all messages on the bus, the other buffer logs approximately 1000 messages before and 1000 messages after any error frame. The Kvaser Memorator Light HS v2 also automatically determines the correct CAN bus bit rate so no configuration is needed.

Connecting a Kvaser Memorator Light HS v2 to the Kvaser Memorator Config Tool, each log file is marked with Track “Err” or “All” based on which of the two buffers the log file is taken from.

Figure 1: A look at the two different types of logfiles logged by a Kvaser Memorator Light HS v2 inside Kvaser Memorator Config Tool.

Reading the log files is done in the same way as you would read log files from any Kvaser Memorator, the only difference is that the ‘logfile.log_type‘ now gives us “Err” for the files originating from the buffer that only triggers on Error Frames (with about 1000 events before and after the Error Frame).

  # 01_list_loggfiles.py
    from canlib import EAN, Device
    from canlib import kvmlib
    
     # Connect to our Kvaser Memorator Light HS v2 with EAN 73-30130-01058-1
     # and mount the log area
     dev = Device.find(ean=EAN('01058-1'))
     memo = kvmlib.openDevice(dev.channel_number(), mount=True)
    
     fileCount = len(memo.log)
     print('Found {} file{} on card.'.format(
        fileCount,
        "s" if fileCount > 1 else "")
     )
   
     # Loop through all logfiles and write some information about them
     for i, logfile in enumerate(memo.log):
         track = logfile.log_type  # marks from what type of buffer the file originates from
         events = logfile.event_count_estimation()
         start = logfile.start_time.isoformat(' ')
         stop = logfile.end_time.isoformat(' ')
         print("%s: %s %s events, start: %s, stop: %s" % (i, track, events, start, stop))
    
         # read first event to get EAN/serial number
         for event in logfile:
            print(str(event)[10:])  # reusing built-in formatting, removing 
               time part
            break  # we are only interested in the first event, so skip the rest
  
        # Close the Kvaser Memorator
        memo.close()

Listing 1: List logged files to stdout

Running the program reports the same information as was seen in the Kvaser Memorator Config Tool.

Found 7 files on card.
 0: LogFileType.ERR 1420 events, start: 2019-08-22 13:13:06, stop: 2019-08-22 13:13:30
       - EAN:73-30130-01058-1 s/n:204 FW:v3.16.100 LIO:v4.0
 1: LogFileType.ERR 2140 events, start: 2019-08-22 13:13:30, stop: 2019-08-22 13:13:49
       - EAN:73-30130-01058-1 s/n:204 FW:v3.16.100 LIO:v4.0
 2: LogFileType.ERR 2940 events, start: 2019-08-22 13:14:11, stop: 2019-08-22 13:14:56
       - EAN:73-30130-01058-1 s/n:204 FW:v3.16.100 LIO:v4.0
 3: LogFileType.ERR 2900 events, start: 2019-08-22 13:15:24, stop: 2019-08-22 13:15:43
       - EAN:73-30130-01058-1 s/n:204 FW:v3.16.100 LIO:v4.0
 4: LogFileType.ALL 3500 events, start: 2019-08-22 13:13:05, stop: 2019-08-22 13:13:49
       - EAN:73-30130-01058-1 s/n:204 FW:v3.16.100 LIO:v4.0
 5: LogFileType.ALL 2500 events, start: 2019-08-22 13:14:08, stop: 2019-08-22 13:14:56
       - EAN:73-30130-01058-1 s/n:204 FW:v3.16.100 LIO:v4.0
 6: LogFileType.ALL 2500 events, start: 2019-08-22 13:15:13, stop: 2019-08-22 13:15:43
       - EAN:73-30130-01058-1 s/n:204 FW:v3.16.100 LIO:v4.0
Author Image

Magnus Carlsson

Magnus Carlsson is a Software Developer for Kvaser AB and has developed firmware and software for Kvaser products since 2007. He has also written a number of articles for Kvaser’s Developer Blog dealing with the popular Python language.