From db7a3b79a296c75f9f43f9006f6efbe2869616b3 Mon Sep 17 00:00:00 2001 From: bjarnedevel0per <> Date: Tue, 11 Sep 2018 10:26:36 +0200 Subject: [PATCH] handle removal of the serialdevice --- Sources/SwiftSerial.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftSerial.swift b/Sources/SwiftSerial.swift index 0add9b7..706618d 100644 --- a/Sources/SwiftSerial.swift +++ b/Sources/SwiftSerial.swift @@ -210,6 +210,7 @@ public enum PortError: Int32, Error { case mustBeOpen case stringsMustBeUTF8 case unableToConvertByteToCharacter + case deviceNotConnected } public class SerialPort { @@ -371,7 +372,13 @@ extension SerialPort { guard let fileDescriptor = fileDescriptor else { throw PortError.mustBeOpen } - + + var s: stat = stat() + fstat(fileDescriptor, &s) + if s.st_nlink != 1 { + throw PortError.deviceNotConnected + } + let bytesRead = read(fileDescriptor, buffer, size) return bytesRead }