Correct issue where sometimes the bytesRead can be -1 especially when read() is called for the first time
This commit is contained in:
@@ -380,7 +380,16 @@ extension SerialPort {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let bytesRead = try readBytes(into: buffer, size: length)
|
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
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,6 +399,7 @@ extension SerialPort {
|
|||||||
|
|
||||||
while remainingBytesToRead > 0 {
|
while remainingBytesToRead > 0 {
|
||||||
let data = try readData(ofLength: remainingBytesToRead)
|
let data = try readData(ofLength: remainingBytesToRead)
|
||||||
|
|
||||||
if let string = String(data: data, encoding: String.Encoding.utf8) {
|
if let string = String(data: data, encoding: String.Encoding.utf8) {
|
||||||
result += string
|
result += string
|
||||||
remainingBytesToRead -= data.count
|
remainingBytesToRead -= data.count
|
||||||
|
|||||||
Reference in New Issue
Block a user