From 3737566f8ce06ec6d5bb946895113784739c0520 Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Fri, 10 Jul 2020 22:27:00 +0930 Subject: [PATCH] Set baudrate, add mock serial struct to test with. --- Sources/SwiftRPLidar/LidarSerial.swift | 1 + Sources/SwiftRPLidar/SwiftRPLidar.swift | 1 + .../SwiftRPLidarTests/SwiftRPLidarTests.swift | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Sources/SwiftRPLidar/LidarSerial.swift b/Sources/SwiftRPLidar/LidarSerial.swift index 3fc9d57..222acae 100644 --- a/Sources/SwiftRPLidar/LidarSerial.swift +++ b/Sources/SwiftRPLidar/LidarSerial.swift @@ -7,4 +7,5 @@ public protocol LidarSerial { func openPort() throws; func readData(ofLength: Int) throws -> Data; func writeData(_ data: Data) throws -> Int; + func setBaudrate(baudrate: Int) } diff --git a/Sources/SwiftRPLidar/SwiftRPLidar.swift b/Sources/SwiftRPLidar/SwiftRPLidar.swift index a2785d4..32ec0bc 100644 --- a/Sources/SwiftRPLidar/SwiftRPLidar.swift +++ b/Sources/SwiftRPLidar/SwiftRPLidar.swift @@ -78,6 +78,7 @@ public class SwiftRPLidar { public func connect() throws { disconnect() try serialPort!.openPort() + serialPort?.setBaudrate(baudrate: 115200) } public func disconnect(){ diff --git a/Tests/SwiftRPLidarTests/SwiftRPLidarTests.swift b/Tests/SwiftRPLidarTests/SwiftRPLidarTests.swift index b64f39a..9628782 100644 --- a/Tests/SwiftRPLidarTests/SwiftRPLidarTests.swift +++ b/Tests/SwiftRPLidarTests/SwiftRPLidarTests.swift @@ -13,3 +13,28 @@ final class SwiftRPLidarTests: XCTestCase { ("testExample", testExample), ] } + +struct MockSerialPort: LidarSerial { + + var dtr: Bool + + var inWaiting: Int + + func closePort() { + } + + func openPort() throws { + } + + func readData(ofLength: Int) throws -> Data { + return Data() + } + + func writeData(_ data: Data) throws -> Int { + return 0 + } + + func setBaudrate(baudrate: Int) { + + } +}