SerialGate

Serial Communication Library for macOS written in Swift.

MIT License

Stars
11

SerialGate

Serial Communication Library for macOS written in Swift.

Requirements

  • Development with Xcode 15.2+
  • Written in Swift 5.9
  • swift-tools-version: 5.9
  • Compatible with macOS 11.0+

Installation

  1. DependencyList is available through Swift Package Manager.
  2. Put a check mark for "USB" in Capabilities of Targets (SandBox)
  3. Edit the entitlements and add com.apple.security.device.serial

Demo

Serial Communication Demo App for Arduino or mbed is in this Project.

Sample Arduino code is here.

Usage

  • Get serial ports
import Combine
import SerialGate

var cancellables = Set<AnyCancellable>()

SGPortManager.shared.availablePortsPublisher
    .sink { ports in
        // get ports
    }
    .store(in: &cancellables)
  • Open a serial port
try? port.setBaudRate(B9600)
try? port.open()
  • Close a serial port
try? port.close()
  • Send a message
let text: String = "Hello World"
try? port.send(text)
  • Read messages
port.receivedTextPublisher
    .sink { (error, text) in 
        if let text {
            Swift.print(text)
        }
    }
    .store(in: &cancellables)
  • Notifications about Port
port.changedPortStatePublisher
    .sink { portState in 
        Swift.print(portState.rawValue)
    }
    .store(in: &cancellables)
Package Rankings
Top 26.31% on Cocoapods.org