From f9981b1ceca99f34ee8f38f9efd5306eda19c657 Mon Sep 17 00:00:00 2001 From: AleyRobotics Date: Sun, 25 Dec 2016 22:13:28 +0300 Subject: [PATCH 1/2] Read byte function add Read UInt8 from port --- Sources/SwiftSerial.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftSerial.swift b/Sources/SwiftSerial.swift index 2236f05..204b88b 100644 --- a/Sources/SwiftSerial.swift +++ b/Sources/SwiftSerial.swift @@ -461,7 +461,23 @@ extension SerialPort { return character } } - + } + + public func readByte() throws -> UInt8 { + let buffer = UnsafeMutablePointer.allocate(capacity: 1) + + defer { + buffer.deallocate(capacity: 1) + } + + while true { + let bytesRead = try readBytes(into: buffer, size: 1) + + if bytesRead > 0 { + let character = UInt8(buffer[0]) + return character + } + } } } From 543e7d39e078ad61c9ba5d333fd0bfaa5529c641 Mon Sep 17 00:00:00 2001 From: AleyRobotics Aleynikov Yuri Date: Sat, 24 Jun 2017 11:21:58 +0300 Subject: [PATCH 2/2] read byte feature add --- Sources/SwiftSerial.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftSerial.swift b/Sources/SwiftSerial.swift index 2236f05..4d7b04d 100644 --- a/Sources/SwiftSerial.swift +++ b/Sources/SwiftSerial.swift @@ -460,8 +460,23 @@ extension SerialPort { let character = UnicodeScalar(buffer[0]) return character } - } - + } + } + + public func readByte() throws -> UInt8 { + let buffer = UnsafeMutablePointer.allocate(capacity: 1) + + defer { + buffer.deallocate(capacity: 1) + } + + while true { + let bytesRead = try readBytes(into: buffer, size: 1) + + if bytesRead > 0 { + return buffer[0] + } + } } }