Serial Communication Library for macOS written in Swift.
- Development with Xcode 16.0+
- Written in Swift 6.0
- Compatible with macOS 13.0+
- DependencyList is available through Swift Package Manager.
- Put a check mark for "USB" in Capabilities of Targets (SandBox)
- Edit the entitlements and add
com.apple.security.device.serial
Serial Communication Demo App for Arduino is in this Project.
Sample Arduino code is here.
- 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))
}
}