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

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Automotive-Swift/Swift-SocketCAN
Access the Linux SocketCAN API via Swift
.package(url: "https://github.com/Automotive-Swift/Swift-SocketCAN.git", from: "0.9.1")

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 await 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! await 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 and Roadmap

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.

License and Contributions

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

GitHub

link
Stars: 0
Last commit: 10 hours ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Swift-SocketCAN 0.9.0
2 weeks ago

First public release.

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