Explorar el Código

read byte feature add

AleyRobotics Aleynikov Yuri hace 9 años
padre
commit
543e7d39e0
Se han modificado 1 ficheros con 17 adiciones y 2 borrados
  1. 17 2
      Sources/SwiftSerial.swift

+ 17 - 2
Sources/SwiftSerial.swift

@@ -460,8 +460,23 @@ extension SerialPort {
                 let character = UnicodeScalar(buffer[0])
                 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]
+            }
+        }
     }
 
 }