这个项目最初是 yeokm1 的 SwiftSerial。他后来存档了这个项目,并好心地链接了这个分支以供后续使用。
import SwiftSerial
...
// setup
let serialPort = SerialPort(path: "/dev/cu.usbmodem1234") // you'll need to find the correct device on your own, but this is what it will resemble on a mac
try serialPort.openPort()
try serialPort.setSettings(
baudRateSetting: .symmetrical(.baud115200),
minimumBytesToRead: 1)
// read output
Task {
let readStream = try serialPort.asyncLines()
for await line in readStream {
print(line, terminator: "")
}
}
// send data
try serialPort.writeString("foo")
// or
try serialPort.writeData(Data([1,2,3,4]))
请参阅演示 CLI 应用程序 SwiftTerminal
以获取工作示例。
.package(url: "https://github.com/mredig/SwiftSerial", .upToNextMinor("1.0.0")
SwiftTerminal
演示程序,用于连接和操作串行连接。