Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Automotive-Swift/Swift-SocketCAN
Access the Linux SocketCAN API via Swift
https://github.com/Automotive-Swift/Swift-SocketCAN
Last synced: 14 days ago
JSON representation
Access the Linux SocketCAN API via Swift
- Host: GitHub
- URL: https://github.com/Automotive-Swift/Swift-SocketCAN
- Owner: Automotive-Swift
- License: mit
- Created: 2021-10-02T11:25:22.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-10T12:13:51.000Z (over 2 years ago)
- Last Synced: 2024-08-01T19:42:00.559Z (3 months ago)
- Language: C
- Size: 38.1 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift-SocketCAN
Access the Linux [SocketCAN](https://www.kernel.org/doc/html/latest/networking/can.html) 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:
```swift
.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`:
```swift
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:
```swift
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..