Serial Communication Library for macOS written in Swift.
- Development with Xcode 15.2+
- Written in Swift 5.9
- swift-tools-version: 5.9
- Compatible with macOS 11.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 or mbed is in this Project.
Sample Arduino code is here.
- 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)