def connect(self): self.channel = self.config.get("KV_CHANNEL") openFlags = canlib.canOPEN_ACCEPT_VIRTUAL if self.config.get( "KV_ACCEPT_VIRTUAL") == True else None bitrate = canlib.canBITRATE_500K #bitrateFlags = canlib.canDRIVER_NORMAL self.ch = canlib.openChannel(self.channel, openFlags) self.parent.logger.debug("{} [CANLib version: {}]".format( ChannelData(self.channel).device_name, canlib.dllversion())) baudrate = int(self.parent.config.get("BAUDRATE")) if self.config.get("KV_BAUDRATE_PRESET"): if not baudrate in BAUDRATE_PRESETS: raise ValueError( "No preset for baudrate '{}'".format(baudrate)) self.ch.setBusParams(BAUDRATE_PRESETS[baudrate]) else: samplePoint = self.config.get("SAMPLE_POINT") sjw = self.config.get("SJW") tseg1 = self.config.get("TSEG1") tseg2 = self.config.get("TSEG2") self.ch.setBusParams(baudrate, tseg1, tseg2, sjw) self.ch.iocontrol.timer_scale = 10 # 10µS, fixed for now. self.ch.busOn() self.connected = True
def init(self, parent, master_id_with_ext: int, slave_id_with_ext: int, receive_callback): self.parent = parent bitrate = canlib.canBITRATE_500K #bitrateFlags = canlib.canDRIVER_NORMAL self.ch = canlib.openChannel(self.channel, self.openFlags) self.parent.logger.debug("{} [CANLib version: {}]".format( ChannelData(self.channel).device_name, canlib.dllversion())) self.ch.setBusParams(canlib.canBITRATE_250K) #self.ch.setBusOutputControl(bitrateFlags) self.ch.iocontrol.timer_scale = 10 # 10µS, fixed for now.
def __init__(self): # Initialize existence of thread, declare canlib object, open a channel handle (0), set channel parameters QtCore.QThread.__init__(self) # Create canlib object # Print out current canlib driver version to console print("CANlib version: " + str(canlib.dllversion())) # Open a channel that accepts connected physical CAN device, default to virtual channel if none found self.handle1 = canlib.openChannel(0, canlib.canOPEN_ACCEPT_VIRTUAL) self.h1data = canlib.ChannelData(0) # Print out current channel name and data to console print("Using channel: " + str(self.h1data.channel_name) + ", EAN: " + str(self.h1data.card_upc_no)) # Set the can bus control to normal self.handle1.setBusOutputControl(canlib.canDRIVER_NORMAL) # Set CAN bit-rate to 125k (GCM default bit-rate) self.handle1.setBusParams(canlib.canBITRATE_125K) # Turn channel bus ON self.handle1.busOn() self.actively_dumping = True # Flag for 'self.run' method
ch = canlib.openChannel(channel, openFlags) print("Using channel: %s, EAN: %s" % ( canlib.ChannelData(channel).channel_name, canlib.ChannelData(channel).card_upc_no)) ch.setBusOutputControl(outputControl) ch.setBusParams(bitrate) ch.busOn() return ch def tearDownChannel(ch): ch.busOff() ch.close() print("canlib dll version:", canlib.dllversion()) ch0 = setUpChannel(channel=0) frame = Frame(id_=100, data=[1, 2, 3, 4], flags=canlib.MessageFlag.EXT) while True: try: frame = ch0.read() print(frame) except (canlib.canNoMsg) as ex: pass except (canlib.canError) as ex: print(ex) tearDownChannel(ch0)