Skip to content

Serial Communication Library for macOS written in Swift.

License

Notifications You must be signed in to change notification settings

Kyome22/SerialGate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SerialGate

Serial Communication Library for macOS written in Swift.

Requirements

  • Development with Xcode 16.0+
  • Written in Swift 6.0
  • Compatible with macOS 13.0+

Installation

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

Demo

Serial Communication Demo App for Arduino is in this Project.

demo

Sample Arduino code is here.

Usage

  • Get serial ports
import SerialGate

Task {
    for await ports in SGPortManager.shared.availablePortsStream {
        // get ports
    }
}
  • Open a serial port
try? port.set(baudRate: B9600)
try? port.open()
  • Close a serial port
try? port.close()
  • Send a message
try? port.send(text: "Hello World")
// or
try? port.send(data: Data())
  • Read messages
Task {
    for await result in port.textStream {
        switch result {
        case let .success(text):
            Swift.print(text)
        case let .failure(error):
            Swift.print(error.localizedDescription)
        }
    }
}
// or
Task {
    for await result in port.dataStream {
        switch result {
        case let .success(data):
            // use data
        case let .failure(error):
            Swift.print(error.localizedDescription)
        }
    }
}
  • Notifications about Port State
Task {
    for await portState in port.portStateStream {
        Swift.print(String(describing: portState))
    }
}

About

Serial Communication Library for macOS written in Swift.

Resources

License

Stars

Watchers

Forks