Jelajahi Sumber

Correct issue where sometimes the bytesRead can be -1 especially when read() is called for the first time

Yeo Kheng Meng 9 tahun lalu
induk
melakukan
58c6521bbf
1 mengubah file dengan 11 tambahan dan 1 penghapusan
  1. 11 1
      Sources/SwiftSerial.swift

+ 11 - 1
Sources/SwiftSerial.swift

@@ -380,7 +380,16 @@ extension SerialPort {
         }
 
         let bytesRead = try readBytes(into: buffer, size: length)
-        let data = Data(bytes: buffer, count: bytesRead)
+
+        var data : Data
+
+        if bytesRead > 0 {
+            data = Data(bytes: buffer, count: bytesRead)
+        } else {
+            //This is to avoid the case where bytesRead can be negative causing problems allocating the Data buffer
+       	    data = Data(bytes: buffer, count: 0)
+        }
+
         return data
     }
 
@@ -390,6 +399,7 @@ extension SerialPort {
 
         while remainingBytesToRead > 0 {
             let data = try readData(ofLength: remainingBytesToRead)
+
             if let string = String(data: data, encoding: String.Encoding.utf8) {
                 result += string
                 remainingBytesToRead -= data.count