SerialSwift
让在 MacOS 上与您的串行外围设备通信变得轻而易举。
更棒的是,SerialSwift
从根本上设计为可观察的和事件驱动的,这使得从外部串行外围设备将信息导入到您的应用程序中比以往任何时候都更容易。
SerialSwift
构建于多个软件包之上
ThreadSafeSwift
用于确保整个库的线程安全Observable
用于在整个库中提供协议一致性观察者模式支持EventDrivenSwift
用于为您外围设备生成的每个串行事件发出相关且极其高性能的事件。ORSSerialPort
用于实际与您的串行外围设备进行交互。通过这种方式,SerialSwift
可以以您喜欢的任何方式集成到您的代码中,使其非常通用。
选择 File
-> Swift Packages
-> Add Package Dependency
并输入 https://github.com/Flowduino/SerialSwift.git
您可以在您自己的软件包的 Package.swift
文件中使用 SerialSwift
作为软件包依赖项
let package = Package(
//...
dependencies: [
.package(
url: "https://github.com/Flowduino/SerialSwift.git",
.upToNextMajor(from: "1.0.0")
),
],
//...
)
从那里,在您任何需要它的软件包目标中,将 SerialSwift
称为“目标依赖项”。
targets: [
.target(
name: "YourLibrary",
dependencies: [
"SerialSwift",
],
//...
),
//...
]
然后,您可以在任何需要它的代码中执行 import SerialSwift
。
以下是 SerialSwift
提供的功能的一些快速简便的用法示例
您可以像这样轻松地为每个外围设备创建一个 SerialSwift
实例
var mySerialDevice = Serial["/dev/cu.myserialdevice", .baud9600]
当然,您需要将上面示例中的两个参数替换为:(首先)您的串行设备的路径,然后是(其次)您的串行设备使用的波特率。
建议您在应用程序的全局位置(例如在应用程序的 Environment
中,或作为单例)保留对 Serialable
对象的引用。这是因为,从您连接到串行外围设备的那一刻起,它将开始发出事件,您可以在应用程序中的任何位置使用这些事件……如下所示。
现在您已连接到串行设备,以下事件将在整个应用程序中普遍发出,并且可以从代码中的任何位置使用。
如果您需要在与串行设备的连接关闭时执行特定操作,则可以从代码中的任何位置轻松完成。
SerialPortClosedEvent.addListener(self) { (event: SerialPortClosedEvent, priority) in
/**
Your code goes in here!
Properties available to you:
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
`event.serial` = A reference to the Serial device which triggered the Event.
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
*/
}
如果您需要在串行设备向您的计算机发送数据时执行特定操作,则可以从代码中的任何位置轻松完成。
SerialPortDataReceivedEvent.addListener(self) { (event: SerialPortDataReceivedEvent, priority) in
/**
Your code goes in here!
Properties available to you:
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
`event.serial` = A reference to the Serial device which triggered the Event.
`event.data` = The actual `Data` your Serial device sent to your computer.
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
*/
}
如果您需要在串行设备遇到错误时执行特定操作,则可以从代码中的任何位置轻松完成。
SerialPortErrorEvent.addListener(self) { (event: SerialPortErrorEvent, priority) in
/**
Your code goes in here!
Properties available to you:
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
`event.serial` = A reference to the Serial device which triggered the Event.
`event.error` = The actual `Error` your Serial device encountered.
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
*/
}
如果您需要在串行设备与您的计算机建立连接时执行特定操作,则可以从代码中的任何位置轻松完成。
SerialPortOpenedEvent.addListener(self) { (event: SerialPortOpenedEvent, priority) in
/**
Your code goes in here!
Properties available to you:
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
`event.serial` = A reference to the Serial device which triggered the Event.
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
*/
}
如果您需要在串行设备从您的计算机断开连接时执行特定操作,则可以从代码中的任何位置轻松完成。
SerialPortRemovedEvent.addListener(self) { (event: SerialPortRemovedEvent, priority) in
/**
Your code goes in here!
Properties available to you:
`event.refTime` = the precise "Mach Time" at which the Serial Port closed
`event.serial` = A reference to the Serial device which triggered the Event.
You can use `if ObjectIdentifier(event.serial) != ObjectIdentifier(mySerialDevice) { return }` to ensure you're only acting on Events emitted by a specific Serial device
*/
}
SerialSwift
在 MIT 许可证下可用。 有关更多信息,请参阅 LICENSE 文件。
如果您需要额外的支持,或者想讨论 SerialSwift
、Swift 或任何其他与 Flowduino 相关的主题,您可以加入我们的 Discord。