# Monitor a Channel Using a Database

    """dbmonitor.py -- Read CAN messages using a database

    This script will use canlib.canlib and canlib.kvadblib to monitor a CAN
    channel, and look up all messages received in a database before printing them.

    It requires a CANlib channel with a connected device capable of receiving CAN
    messages, some source of CAN messages, and the same database that the source is
    using to generate the messages.

    The source of the messages may be e.g. the pinger.py example script.

    """
    import argparse

    from canlib import canlib, kvadblib

    def printframe(db, frame):
        bmsg = db.interpret(frame)
        msg = bmsg._message

        # form = '═^' + str(width)
        # print(format(" %s " % msg.name, form))

        print('┏', msg.name)

        if msg.comment:
            print('┃', '"%s"' % msg.comment)

        for bsig in bmsg:
            print('┃', bsig.name + ':', bsig.phys, bsig.unit)

        print('┗')

    def monitor_channel(channel_number, db_name):
        db = kvadblib.Dbc(filename=db_name)

        ch = canlib.openChannel(channel_number, canlib.canOPEN_ACCEPT_VIRTUAL)
        ch.setBusOutputControl(canlib.canDRIVER_NORMAL)
        ch.setBusParams(canlib.canBITRATE_1M)
        ch.busOn()

        print("Listening...")
        while True:
            try:
                frame = ch.read(timeout=50000)
                printframe(db, frame)
            except canlib.CanNoMsg:
                pass

    if __name__ == '__main__':
        parser = argparse.ArgumentParser(
            description="Listen on a CAN channel and print all signals received, as specified by a database.")
        parser.add_argument('channel', type=int, default=1, nargs='?', help=(
            "The channel to listen on."))
        parser.add_argument('--db', default="engine_example.dbc", help=(
            "The database file to look up messages and signals in."))
        args = parser.parse_args()

        monitor_channel(args.channel, args.db)

## Description

Any CAN messages received on the specified channel will be looked up in the database using `canlib.kvadblib.Dbc.interpret`, which allows the script to print the “phys” value of each signal instead of just printing the raw data (as Monitor a Channel does). The script also prints the message’s name and comment (if available), as well as the signals name and unit.

## Sample Output

    ┏ EngineData
    ┃ PetrolLevel: 0.0 l
    ┃ EngPower: 12.0 kW
    ┃ EngForce: 0.0 N
    ┃ IdleRunning: 0.0
    ┃ EngTemp: -30.0 °C
    ┃ EngSpeed: 7735.0 rpm
    ┗
    ┏ GearBoxInfo
    ┃ EcoMode: 0.0
    ┃ ShiftRequest: 0.0
    ┃ Gear: 0.0
    ┗
    ┏ EngineData
    ┃ PetrolLevel: 0.0 l
    ┃ EngPower: 28.0 kW
    ┃ EngForce: 0.0 N
    ┃ IdleRunning: 0.0
    ┃ EngTemp: -30.0 °C
    ┃ EngSpeed: 3467.0 rpm
    ┗
    ┏ GearBoxInfo
    ┃ EcoMode: 1.0
    ┃ ShiftRequest: 0.0
    ┃ Gear: 0.0
    ┗
    ┏ EngineData
    ┃ PetrolLevel: 0.0 l
    ┃ EngPower: 0.0 kW
    ┃ EngForce: 0.0 N
    ┃ IdleRunning: 0.0
    ┃ EngTemp: -50.0 °C
    ┃ EngSpeed: 1639.0 rpm
    ┗
    ┏ GearBoxInfo
    ┃ EcoMode: 1.0
    ┃ ShiftRequest: 0.0
    ┃ Gear: 1.0
    ┗
    ┏ EngineData
    ┃ PetrolLevel: 60.0 l
    ┃ EngPower: 0.0 kW
    ┃ EngForce: 0.0 N
    ┃ IdleRunning: 0.0
    ┃ EngTemp: 142.0 °C
    ┃ EngSpeed: 0.0 rpm
    ┗
    ┏ GearBoxInfo
    ┃ EcoMode: 0.0
    ┃ ShiftRequest: 0.0
    ┃ Gear: 0.0
    ┗
    ┏ EngineData
    ┃ PetrolLevel: 172.0 l
    ┃ EngPower: 51.0 kW
    ┃ EngForce: 0.0 N
    ┃ IdleRunning: 0.0
    ┃ EngTemp: -50.0 °C
    ┃ EngSpeed: 0.0 rpm
    ┗
    ┏ GearBoxInfo
    ┃ EcoMode: 0.0
    ┃ ShiftRequest: 0.0
    ┃ Gear: 0.0
    ┗