Swiftpack.co - christophhagen/SwiftSerial as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by christophhagen.
christophhagen/SwiftSerial 1.0.0
Serial port library using Swift async/await for Linux/macOS
⭐️ 4
🕓 1 year ago
.package(url: "https://github.com/christophhagen/SwiftSerial.git", from: "1.0.0")

SwiftSerial

A Swift wrapper around the system functions to interact with serial interfaces based on async/await.

Ports are implemented using the Swift actor pattern, which provides thread safety and asynchronous interactions with the interfaces.

Usage

SwiftSerial provides an easy way to interact with serial interfaces.


// Create the port
let serial = SerialPort(path: "/dev/ttyUSB0")

// Configure and open
try await serial.open(
    receiveRate: .baud112500, 
    transmitRate: .baud112500, 
    parity: .none, 
    stopBits: .one, 
    dataBits: .eight)
    
// Receive data
let data = try await serial.readBytes(count: 10)

// Wait for data to arrive
let data2 = try await serial.readBytesBlocking(count: 10, timeout: 1.0)

// Transmit data
try await serial.writeBytes([0x00, 0x01, 0x02, 0x04])

// Close the port when finished
serial.close()

Installation

Include the package in your Package.swift file:

...
    dependencies: [.package(url: "https://github.com/christophhagen/SwiftSerial", from: "1.0.0")
...
    .target(name: "MyTarget", dependencies: [.product(name: "SwiftSerial", package: "SwiftSerial")]),
...

License

MIT.

Thanks

This library is loosly based on SerialSwift (the name just made sense), but re-implemented to use async/await, with a bit fewer functions, but more documentation.

GitHub

link
Stars: 4
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Release Notes

1.0
1 year ago

All basic functionality is present:

  • Port settings and options
  • Receiving bytes (blocking/non-blocking)
  • Transmitting bytes
  • Closing the port

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