Swiftpack.co - mac-can/KvaserCAN-Library as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by mac-can.
mac-can/KvaserCAN-Library v0.3.2
macOS® Driver and SDK for USB CAN Interfaces from Kvaser
⭐️ 11
🕓 1 week ago
.package(url: "https://github.com/mac-can/KvaserCAN-Library.git", from: "v0.3.2")

macOS® User-Space Driver for USB CAN Interfaces from Kvaser

Copyright © 2020-2023 Uwe Vogt, UV Software, Berlin ([email protected])

Running CAN and CAN FD on Mac®

Running CAN and CAN FD on Mac is the mission of the MacCAN project. The macOS driver for USB CAN interfaces from Kvaser is based on MacCAN-Core which is an abstraction (or rather a wrapper) of Apple´s IOUsbKit to create USB user-space drivers for CAN interfaces from various vendors under macOS.


This repo contains the source code for the MacCAN-KvaserCAN driver and several alternatives to build dynamic libraries for macOS, either as a C++ class library (libKvaserCAN), or as a CAN API V3 driver library (libUVCANKVL), as well as my beloved CAN utilities can_moni and can_test, and some C/C++, Swift, and Python example programs.

CAN API V3 is a wrapper specification to have a uniform CAN Interface API for various CAN interfaces from different vendors running under multiple operating systems. See header file CANAPI.h for the CAN API V3 wrapper specification.

Note: This project does not aim to implement Kvaser´s CANlib library on macOS.


/// \name   KvaserCAN API
/// \brief  CAN API V3 driver for Kvaser CAN interfaces
/// \note   See CCanApi for a description of the overridden methods
/// \{
class CKvaserCAN : public CCanApi {
    // constructor / destructor

    // CCanApi overrides
    static bool GetFirstChannel(SChannelInfo &info, void *param = NULL);
    static bool GetNextChannel(SChannelInfo &info, void *param = NULL);

    static CANAPI_Return_t ProbeChannel(int32_t channel, const CANAPI_OpMode_t &opMode, const void *param, EChannelState &state);
    static CANAPI_Return_t ProbeChannel(int32_t channel, const CANAPI_OpMode_t &opMode, EChannelState &state);

    CANAPI_Return_t InitializeChannel(int32_t channel, const CANAPI_OpMode_t &opMode, const void *param = NULL);
    CANAPI_Return_t TeardownChannel();
    CANAPI_Return_t SignalChannel();

    CANAPI_Return_t StartController(CANAPI_Bitrate_t bitrate);
    CANAPI_Return_t ResetController();

    CANAPI_Return_t WriteMessage(CANAPI_Message_t message, uint16_t timeout = 0U);
    CANAPI_Return_t ReadMessage(CANAPI_Message_t &message, uint16_t timeout = CANREAD_INFINITE);

    CANAPI_Return_t GetStatus(CANAPI_Status_t &status);
    CANAPI_Return_t GetBusLoad(uint8_t &load);

    CANAPI_Return_t GetBitrate(CANAPI_Bitrate_t &bitrate);
    CANAPI_Return_t GetBusSpeed(CANAPI_BusSpeed_t &speed);

    CANAPI_Return_t GetProperty(uint16_t param, void *value, uint32_t nbyte);
    CANAPI_Return_t SetProperty(uint16_t param, const void *value, uint32_t nbyte);

    char *GetHardwareVersion();  // (for compatibility reasons)
    char *GetFirmwareVersion();  // (for compatibility reasons)
    static char *GetVersion();  // (for compatibility reasons)

    static CANAPI_Return_t MapIndex2Bitrate(int32_t index, CANAPI_Bitrate_t &bitrate);
    static CANAPI_Return_t MapString2Bitrate(const char *string, CANAPI_Bitrate_t &bitrate);
    static CANAPI_Return_t MapBitrate2String(CANAPI_Bitrate_t bitrate, char *string, size_t length);
    static CANAPI_Return_t MapBitrate2Speed(CANAPI_Bitrate_t bitrate, CANAPI_BusSpeed_t &speed);

    static uint8_t Dlc2Len(uint8_t dlc) { return CCanApi::Dlc2Len(dlc); }
    static uint8_t Len2Dlc(uint8_t len) { return CCanApi::Len2Dlc(len); }
/// \}

Build Targets

Important note: To build any of the following build targets run the script build_no.sh to generate a pseudo build number.

uv-pc013mac:~ eris$ cd ~/Projects/MacCAN/KvaserCAN
uv-pc013mac:KvaserCAN eris$ ./build_no.sh

Repeat this step after each git commit, git pull, git clone, etc.

Then you can build all targets by typing the usual commands:

uv-pc013mac:~ eris$ cd ~/Projects/MacCAN/KvaserCAN
uv-pc013mac:KvaserCAN eris$ make clean
uv-pc013mac:KvaserCAN eris$ make all
uv-pc013mac:KvaserCAN eris$ sudo make install
uv-pc013mac:KvaserCAN eris$

(The version number of the library can be adapted by editing the appropriated Makefile and changing the variable VERSION accordingly. Don´t forget to set the version number also in the source files.)


libKvaserCAN is a dynamic library with a CAN API V3 compatible application programming interface for use in C++ applications. See header file KvaserCAN.h for a description of all class members.


libUVCANKVL is a dynamic library with a CAN API V3 compatible application programming interface for use in C applications. See header file can_api.h for a description of all API functions.


can_moni is a command line tool to view incoming CAN messages. I hate this messing around with binary masks for identifier filtering. So I wrote this little program to have an exclude list for single identifiers or identifier ranges (see program option --exclude or just -x). Precede the list with a ~ and you get an include list.

Type can_moni --help to display all program options.


can_test is a command line tool to test CAN communication. Originally developed for electronic environmental tests on an embedded Linux system with SocketCAN, I´m using it for many years as a traffic generator for CAN stress-tests.

Type can_test --help to display all program options.

Target Platform

  • macOS 11.0 and later (Intel x64 and Apple silicon)

Development Environment

macOS Ventura

  • macOS Ventura (13.5.1) on a Mac mini (M1, 2020)
  • Apple clang version 14.0.3 (clang-1403.
  • Xcode Version 14.3.1 (14E300c)

macOS Big Sur

  • macOS Big Sur (11.7.9) on a MacBook Pro (2019)
  • Apple clang version 13.0.0 (clang-1300.0.29.30)
  • Xcode Version 13.2.1 (13C100)

Supported CAN Hardware

  • Kvaser Leaf Light v2 (EAN: 73-30130-00685-0)
  • Kvaser Leaf Pro HS v2 (EAN: 73-30130-00843-4)
  • Kvaser U100P (EAN: 73-30130-01174-8)

Since version 0.3 theoretically all (single-channel) CAN interfaces from the device family Leaf Interfaces (CAN 2.0 interfaces, e.g. Leaf Light v2) and from the device family Mhydra Interfaces (CAN FD interfaces, e.g. U100P) are supported. To add a new CAN USB interface from Kvaser, only its USB ProductID and some device specific attributes have to be entered or enabled in the module KvaserCAN_Devices.

Note: The CAN interfaces listed above are the ones that are enabled and have been tested.


The Xcode project for the trial program includes an xctest target with one test suite for each CAN API V3 C interface function. To run the test suites or single test cases two CAN devices are required. General test settings can be adapted in the file Settings.h.

Known Bugs and Caveats

  • For a list of known bugs and caveats see tab Issues in the GitHub repo.
  • For multi-channel devices from Kvaser, the driver/library can only access channel CAN1. The other channels are not enumerated.

This and That

CAN API V3 Reference

A generic documentation of the CAN API V3 application programming interface can be found here.

Wrapper Library for Windows®

A CAN API V3 compatible Wrapper Library for Windows is also available. It is build upon Kvaser´s CANlib DLL and can be downloaded from / cloned at GitHub.


This work is dual-licensed under the terms of the BSD 2-Clause "Simplified" License and under the terms of the GNU General Public License v3.0 (or any later version). You can choose between one of them if you use this work in whole or in part.

SPDX-License-Identifier: BSD-2-Clause OR GPL-3.0-or-later


Mac and macOS are trademarks of Apple Inc., registered in the U.S. and other countries.
Windows is a registered trademark of Microsoft Corporation in the United States and/or other countries.
Linux is a registered trademark of Linus Torvalds.
All other company, product and service names mentioned herein may be trademarks, registered trademarks or service marks of their respective owners.

Hazard Note

If you connect your CAN device to a real CAN network when using this library, you might damage your application.


E-Mail: mailto://[email protected]
Internet: https://www.mac-can.net


Stars: 11
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Service release of September 10, 2023
1 week ago

Major changes:

  • Updated MacCAN Core sources to rev. 1757 (core 0.4)
  • Updated CAN API V3 sources to rev. 1187 and applied the changes
    • Added old error code -40 as queue overrun error and renumbered error error frame received from -40 to -19
    • Removed unused error code CANERR_ERR_FRAME (note: the error code -19 is marked as reserved)
    • Removed flag fdoe and brse from struct can_speed_t (note: the flags are marked as reserved)
    • Integrated reworked module can_btr:
      • btr_check_bitrate: if FDOE w/o BRSE then check data phase field acc. OPTION_CANBTR_DATA_FIELDS
      • btr_bitrate2speed: no range checks anymore, calculation is done completely in float
      • btr_bitrate2index: returns the index if an index is given
      • btr_bitrate2string: no range checks anymore
      • btr_sja10002bitrate: set data phase field acc. OPTION_CANBTR_DATA_FIELDS
      • btr_compare_bitrates: comparision on basis of transmission rates (new function)
      • Removed flag fdoe and brse from struct btr_speed_t (standalone variant)
      • Documented all interface functions by Doxygen comments
      • Added header file CANBTR_Defaults.h
      • Reworked the bit-rate string scanner (btr_string2bitrate):
        • Return a flag if any data phase key is found
        • Return a flag if no. samp. key is found
        • Return BTRERR_ILLPARA on error
        • Removed the range checks
        • Set miminum frequency to 1
        • Checked for duplicated keys
        • Accept only case sensitive keys
      • Reworked the bit-rate string printer (btr_bitrate2string):
        • Added a parameter to output data phase keys
        • Added a parameter to output no. samp. key
        • Added a parameter for maximum buffer size
      • Fixed a bug with strtol if a correct value is outside the range of representable values (note: long is 32 bit wide for MS compilers and 64 bit for gcc based compilers!)
    • Added property values for transmit queue properties (note: values for filtering have been shifted by 8)
    • Fixed a bug with saturation of CAN FD DLC conversion
    • Updated description of compiler switches
    • Reworked formatting of message flags
  • Added CAN API V3 Tests with GoogleTest (rev. 1194)
  • Added Kvaser-specific bit-rate defaults
  • Added bit-rate settings for 5kbps
  • Fixed a bug with bus error state flags
  • Fixed a bug with capability flag ERR
  • Fixed a bug with mode flag ERR
  • Fixed a bug with message flag ESI
  • Fixed a bug with message flag RTR
  • Made the utilities more generic
  • Updated Swift wrapper and package
  • Updated the trial program
  • Updated the examples

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics