소스 검색

Merge pull request #1 from AleyRobotics/master

read byte feature add
Yeo Kheng Meng 9 년 전
부모
커밋
e2063420e9
1개의 변경된 파일16개의 추가작업 그리고 1개의 파일을 삭제
  1. 16 1
      Sources/SwiftSerial.swift

+ 16 - 1
Sources/SwiftSerial.swift

@@ -461,7 +461,22 @@ extension SerialPort {
                 return character     
             }
         }         
-
+    }
+    
+    public func readByte() throws -> UInt8 {
+        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
+        
+        defer {
+            buffer.deallocate(capacity: 1)
+        }
+        
+        while true {
+            let bytesRead = try readBytes(into: buffer, size: 1)
+            
+            if bytesRead > 0 {
+                return buffer[0]
+            }
+        }
     }
 
 }