Swiftpack.co - Automotive-Swift/Swift-SocketCAN as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Automotive-Swift.
Automotive-Swift/Swift-SocketCAN 0.9.4
Access the Linux SocketCAN API via Swift
⭐️ 2
🕓 28 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/Automotive-Swift/Swift-SocketCAN.git", from: "0.9.4")

Swift-SocketCAN

Access the Linux SocketCAN API from Swift.

How to integrate

This is an SPM-compliant Swift Package: First, add the following line in Package.swift to your package dependencies:

.package(url: "https://github.com/AutomotiveSwift/Swift-SocketCAN.git", from: "0.9.0")

Then, add the module Swift-SocketCAN – where necessary – to your target dependencies.

Usage

Send a CAN frame to 0x18db33f1 (OBD2 29-bit broadcast address) via interface can0:

let socket = SocketCAN(iface: "can0")
do {
    socket.open(baudrate: 500000)

    let frame = Frame(id: 0x18db33f1, padded: [0x10, 0x01])
    try socket.write(frame: frame)
} catch {
    print("An error occured: \(error)")
}

Read CAN frames from can0 and dump them to the console:

let socket = SocketCAN(iface: "can0")
try! socket.open()

while true {
    do {
        let frame = socket.read(timeout: 500)
        var str = "\(socket.iface) \(frame.timestamp): [\(frame.dlc)]"
        for i in 0..<frame.dlc {
            str += String(format: " %02X", frame.data[i])
        }
        print(str)
    } catch SocketCAN.Error.timeout {
        // timeout, just continue
    } catch {
        print("error: \(error)")
    }
}

Status

This has only received minimal testing yet, but it seems to work so far. Early tests with making this an actor on Swift 5.5 have failed, but I've not given up yet.

Roadmap

  • ☑ ISOTP support
  • ☑ Bitrate configuration (needs root or CAP_NET_ADMIN)
  • ☐ Queue length configuration
  • ☐ BCM (Broadcast Manager) support

License and Contributions

This package is licensed under the term of the MIT License. Contributions are always welcome!

GitHub

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

Release Notes

Swift-SocketCAN 0.9.0
38 weeks ago

First public release.

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