canlib.linlib

class canlib.linlib.Channel(handle)[source]

Bases: object

A LINlib channel

This class is normally instantiated with openMaster or openSlave.

Channels are automatically closed on garbage collection, and can also be used as context managers in which case they close as soon as the context exits.

busOff()[source]

Go bus off

This function deactivates the LIN interface. It will not participate further in the LIN bus traffic.

busOn()[source]

Go bus on

This function activates the LIN interface.

clearMessage(msg_id)[source]

Clear a message buffer for a LIN slave

The message buffer will not answer next time it is polled.

close()[source]

Close this LINlib channel

Closes an open handle to a LIN channel.

Note

It is normally not necessary to call this function directly, as the internal handle is automatically closed when the Channel object is garbage collected.

New in version 1.6.

getCanHandle()[source]

Return the CAN handle given an open LIN handle

getFirmwareVersion()[source]

Retrieve the firmware version from the LIN interface

Returns a FirmwareVersion namedtuple containing boot_version and app_version that are VersionNumber namedtuples. If only one of these is needed, the return value can be unpacked as such:

boot_ver, app_ver = channel.getFirmwareVersion()

Note

For newer interfaces use linlib.getChannelData with linlib.ChannelData.CARD_FIRMWARE_REV instead.

The version numbers aren’t valid until Channel.busOn has been called.

The firmware in the LIN interface is divided into two parts, the boot code and the application. The boot code is used only when reprogramming (reflashing) the LIN interface. The application handles all LIN communication.

Version numbers are, since the precambric era, divided into a major version number, a minor version number and a build number. These are usually written like, for example, 3.2.12. Here the major number is 3, the minor number 2 and the build number 12.

read(timeout=0)[source]

Read a message from the LIN interface

If a message is available for reception, linOK is returned. This is a non-blocking call. It waits until a message is received in the LIN interface, or the specified timeout period elapses.

This may return a frame sent by writeMessage or writeWakeup.

Note

This call will also return echoes of what the LIN interface is transmitting with writeMessage. In other words, the LIN interface can hear itself.

Parameters:timeout (int) – Timeout in milliseconds.
Returns:(LINFrame)
requestMessage(msgid)[source]

Request a message from a slave

This function writes a LIN message header to the LIN bus. A slave in the system is then expected to fill in the header with data.

Note

This call is only available in master mode.

setBitrate(bps)[source]

Set the bitrate in bits per second

This function sets the bit rate for a master, or the initial bit rate for a slave. The LIN interface should not be on-bus when this function is called.

Note

The LIN Interface should not be on bus. Supported bit rates are 1000 - 20000 bits per second.

setupIllegalMessage(msgid, disturb_flags, delay)[source]

Create a corrupted LIN message

Using this function, it is possible to use the LIN interface to create corrupted LIN messages. You call the function once for each LIN identifier that should be affected.

To return to normal mode, either restart the LIN interface (by going off bus and on the bus again) or call the function with delay and disturb_flags set to zero.

Parameters:
  • msgid (int) – The identifier of the LIN message
  • disturb_flags (linlib.MessageDisturb) – One or more of the linlib.MessageDisturb flags.
  • delay (int) – The delay parameter will result in a delay of this many bittimes after the header and before the first data byte.

Note

The LIN Interface must be on bus for this command to work. It is supported in firmware version 2.4.1 and later.

setupLIN(flags=<Setup.VARIABLE_DLC: 2>, bps=0)[source]

Setup the LIN interface

This function changes various settings on a LIN Interface that is on bus. When going on bus, the bit rate and the flag values listed below are set to the default value (either as hard-coded in the firmware, or as stored in the non-volatile memory of the LIN Interface).

With this function, you can do one or more of the following things:

  • Select checksum according to LIN 2.0
  • Turn variable message length off. The message length then will depend on the message ID.

In master mode it is also possible to change the bit rate without going off bus first.

Note

The LIN Interface must be on bus for this command to work. It is supported in firmware version 2.5.1 and later. For LIN 2.0 compliance, you must specify both LIN_ENHANCED_CHECKSUM and LIN_VARIABLE_DLC.

Parameters:
  • flags (linlib.Setup) – One or more of the linlib.Setup flags
  • bps (int) – The bit rate in bits per second. This parameter can be used only in master mode. The bit rate is set without going off bus.
updateMessage(frame)[source]

Update a message buffer in a slave

This function updates a message buffer in a slave. The contents of the message buffer will be used the next time the slave is polled for the specified LIN message id.

Note

The LIN Interface must be on bus.

Parameters:frame (Frame) – The information to be updated. Only the ~id~, ~data~, and ~dlc~ attributes are used. Note that the frame can, but not need not, be a LINFrame.
writeMessage(frame)[source]

Write a LIN message

Write a LIN message. It is advisable to wait until the message is echoed by read before transmitting a new message, or in case of a schedule table being used, transmit the next message when the previous one is known to be complete.

Note

Only available in master mode

Parameters:frame – Frame :: The information to be updated. Only the ~id~, ~data~, and ~dlc~ attributes are used. Note that the frame can, but not need not, be a LINFrame.
writeSync(timeout)[source]

Make sure all message transmitted to the interface have been received

timeout is in milliseconds.

When messages are transmitted to the LIN Interface, they are queued by the driver before appearing on the CAN bus.

If the LIN Interface is in master mode and a LIN message has been transmitted with writeMessage, this function will return when the LIN Interface has received the message. If another LIN message is being received or transmitted, the message will not be transmitted on the LIN bus at once. And even if the LIN Interface is idle, the header of the new message will just have been started when writeSync returns.

After calling updateMessage and clearMessage for a slave, this function is enough to know that the LIN Interface is updated.

After writeMessage, it is advisable to wait until the message is echoed by read before transmitting a new message, or in case of a schedule table being used, transmit the next message when the previous one is known to be complete.

When, in master mode, a message should be transmitted after a poll (reception) is done, it might be necessary to call writeMessage before the result is received via read as the LIN Interface waits up to the maximum frame length before knowing a received message is complete. A new message to transmit will force completion if the currently received one.

writeWakeup(count=0, interval=1)[source]

Write one or more wakeup frames

If count is zero (the default), one single wakeup frame is transmitted. If ~count > 1~, several wakeup frames are transmitted spaced with interval bittimes. The LIN interface will interrupt the sequence when a LIN message or another command is received. The stream of wakeups will be recived as incoming messages with the linlib.MessageFlag.RX flag.

Parameters:
  • count (int) – The number of wakeup frames to send.
  • interval (int) – The time, in bit times, between the wakeup frames.
class canlib.linlib.ChannelData[source]

Bases: canlib.cenum.CEnum

linCHANNELDATA_xxx

These defines are used in linlib.getChannelData.

CARD_FIRMWARE_REV = 9
class canlib.linlib.ChannelType[source]

Bases: canlib.cenum.CEnum

Flags for linlib.openChannel

MASTER = 1
SLAVE = 2
class canlib.linlib.Error[source]

Bases: canlib.cenum.CEnum

CANERROR = -15
DRIVER = -18
DRIVERFAILED = -19
ERRRESP = -16
INTERNAL = -22
INVHANDLE = -14
LICENSE = -21
MASTERONLY = -5
NOCARD = -20
NOCHANNELS = -10
NOHANDLES = -13
NOMEM = -9
NOMSG = -1
NOTFOUND = -8
NOTINITIALIZED = -12
NOTRUNNING = -3
NOT_IMPLEMENTED = -26
NO_ACCESS = -23
NO_REF_POWER = -25
PARAM = -7
RUNNING = -4
SLAVEONLY = -6
TIMEOUT = -11
VERSION = -24
WRONGRESP = -17
class canlib.linlib.FirmwareVersion(boot_version, app_version)

Bases: tuple

app_version

Alias for field number 1

boot_version

Alias for field number 0

exception canlib.linlib.LinError[source]

Bases: canlib.exceptions.DllException

Base class for exceptions raised by the linlib class

Looks up the error text in the linlib dll and presents it together with the error code and the wrapper function that triggered the exception.

exception canlib.linlib.LinGeneralError(status)[source]

Bases: canlib.linlib.exceptions.LinError

A linlib error that does not (yet) have its own Exception

Warning

Do not explicitly catch this error, instead catch LinError. Your error may at any point in the future get its own exception class, and so will no longer be of this type. The actual status code that raised any LinError can always be accessed through a status attribute.

exception canlib.linlib.LinNoMessageError[source]

Bases: canlib.linlib.exceptions.LinError

No messages where availible

status = -1
exception canlib.linlib.LinNotImplementedError[source]

Bases: canlib.linlib.exceptions.LinError

Feature/function not implemented in the device

status = -26
class canlib.linlib.MessageDisturb[source]

Bases: canlib.cenum.CEnum

LIN illegal message flags

CSUM = 1
PARITY = 2
class canlib.linlib.MessageFlag(*args, **kwds)[source]

Bases: canlib.cenum.CFlag

LIN message flags

The following flags can be returned from linlib.readMessage and linlib.readMessageWait.

BIT_ERROR = <MessageFlag.BIT_ERROR: 128>
CSUM_ERROR = <MessageFlag.CSUM_ERROR: 16>
NODATA = <MessageFlag.NODATA: 8>
PARITY_ERROR = <MessageFlag.PARITY_ERROR: 32>
RX = <MessageFlag.RX: 2>
SYNCH_ERROR = <MessageFlag.SYNCH_ERROR: 64>
TX = <MessageFlag.TX: 1>
WAKEUP_FRAME = <MessageFlag.WAKEUP_FRAME: 4>
class canlib.linlib.MessageInfo[source]

Bases: _ctypes.Structure

bitrate

Structure/Union member

byteTime

Structure/Union member

checkSum

Structure/Union member

frameLength

Structure/Union member

idPar

Structure/Union member

synchBreakLength

Structure/Union member

synchEdgeTime

Structure/Union member

timestamp

Structure/Union member

z

Structure/Union member

class canlib.linlib.MessageParity[source]

Bases: canlib.cenum.CEnum

LIN message parity

ENHANCED = 8
STANDARD = 4
class canlib.linlib.Setup(*args, **kwds)[source]

Bases: canlib.cenum.CFlag

Used in linlib.setup

ENHANCED_CHECKSUM = <Setup.ENHANCED_CHECKSUM: 1>
VARIABLE_DLC = <Setup.VARIABLE_DLC: 2>
class canlib.linlib.TransceiverData(ean, serial, type)

Bases: tuple

ean

Alias for field number 0

serial

Alias for field number 1

type

Alias for field number 2

canlib.linlib.dllversion()[source]

Retrieve the LIN library version.

Note

Requires CANlib v5.3

canlib.linlib.getChannelData(channel_number, item=<ChannelData.CARD_FIRMWARE_REV: 9>)[source]

This function can be used to retrieve certain pieces of information about a channel.

Note

You must pass a channel number and not a channel handle.

Parameters:
  • channel (int) – The number of the channel you are interested in. Channel numbers are integers in the interval beginning at 0.
  • item (linlib.ChannelData) – This parameter specifies what data to obtain for the specified channel. Currently the only item available is ChannelData.CARD_FIRMWARE_REV, which is the default.
Returns:

(major, minor, release, build) :: A four-tuple of the firmware

version. (For ChannelData.CARD_FIRMWARE_REV)

canlib.linlib.getTransceiverData(channel_number)[source]

Get the transceiver information for a CAN channel

The application typically uses this call to find out whether a particular CAN channel has a LIN interface connected to it. For a Kvaser LIN Leaf it retrieves the transceiver type and device information.

This function call will open the CAN channel, but no CAN messages are transmitted on it. In other words, it’s risk-free to use even if no LIN interface is connected, or if the channel is connected to a CAN system.

Note

Attempts to use the channel for LIN communication will be meaningful only if the returned TransceiverData’s ~type~ attribute is one of canlib.canlib.TransceiverType.LIN or canlib.canlib.TransceiverType.CANFD_LIN

A LIN interface need not be powered for this call to succeed.

The information may not always be accurate. Especially after changing transceiver on a running LAPcan card, you should go on bus and off bus again to be sure the transceiver information is updated.

canlib.linlib.initializeLibrary()[source]

Initialize LINlib

Note

LINlib is automatically initialized when canlib.linlib is imported. This function is only necessary when LINlib has been manually unloaded with unloadLibrary.

canlib.linlib.openChannel(channel_number, channel_type, bps=None)[source]

Open a channel to a LIN interface.

Parameters:
  • channel_number (int) – The number of the channel. This is the same channel number as used by canlib.canlib.ppenChannel.
  • channel_type (linlib.ChannelType) – Whether the LIN interface will be a master or slave.
  • bps (int or None) – If not None, Channel.setBitrate will be called with this value before the channel is returned.
Returns:

(linlib.Channel) – The opened channel

Note

For DRV Lin: The cable must be powered and connected to a LAPcan channel. For Kvaser LIN Leaf: The Leaf must be powered from the LIN side.

canlib.linlib.openMaster(channel_number, bps=None)[source]

Open a channel as a master

This function simply calls openChannel with channel_type set to ChannelType.MASTER.

canlib.linlib.openSlave(channel_number, bps=None)[source]

Open a channel as a slave

This function simply calls openChannel with channel_type set to ChannelType.SLAVE.

canlib.linlib.unloadLibrary()[source]

Deinitialize LINlib

This function de-initializes the LIN library. After this function is called linInitializeLibrary must be called before any other LIN function is called.