SerialSwift

SerialSwift 让在 MacOS 上与您的串行外围设备通信变得轻而易举。

更棒的是,SerialSwift 从根本上设计为可观察的事件驱动的,这使得从外部串行外围设备将信息导入到您的应用程序中比以往任何时候都更容易。

SerialSwift 构建于多个软件包之上

通过这种方式,SerialSwift 可以以您喜欢的任何方式集成到您的代码中,使其非常通用。

安装

Xcode 项目

选择 File -> Swift Packages -> Add Package Dependency 并输入 https://github.com/Flowduino/SerialSwift.git

Swift Package Manager 项目

您可以在您自己的软件包的 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

如果您需要在与串行设备的连接关闭时执行特定操作,则可以从代码中的任何位置轻松完成。

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

如果您需要在串行设备向您的计算机发送数据时执行特定操作,则可以从代码中的任何位置轻松完成。

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

如果您需要在串行设备遇到错误时执行特定操作,则可以从代码中的任何位置轻松完成。

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

如果您需要在串行设备与您的计算机建立连接时执行特定操作,则可以从代码中的任何位置轻松完成。

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

如果您需要在串行设备从您的计算机断开连接时执行特定操作,则可以从代码中的任何位置轻松完成。

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 文件

加入我们的 Discord

如果您需要额外的支持,或者想讨论 SerialSwift、Swift 或任何其他与 Flowduino 相关的主题,您可以加入我们的 Discord