From 965f6023d0e8c9f7213d76796fae58122b40c677 Mon Sep 17 00:00:00 2001 From: Samuel Cornejo Date: Tue, 14 Jan 2020 01:24:03 -0600 Subject: [PATCH] Workaround for for random: Index out of range: .../ContiguousArrayBuffer.swift error crashes when trying to read data from serial port --- Sources/SwiftyGPIO/Generic/UART.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/SwiftyGPIO/Generic/UART.swift b/Sources/SwiftyGPIO/Generic/UART.swift index 44afa33..21351b3 100644 --- a/Sources/SwiftyGPIO/Generic/UART.swift +++ b/Sources/SwiftyGPIO/Generic/UART.swift @@ -117,9 +117,13 @@ public final class SysFSUART: UARTInterface { var buf = [CChar](repeating:0, count: 4096) //4096 chars at max in canonical mode let n = read(fd, &buf, buf.count * MemoryLayout.stride) + + usleep(100_000) //100ms sleep. Workaround for random: Index out of range: .../ContiguousArrayBuffer.swift error + if n<0 { throw UARTError.IOError("Error while reading from UART") } + return Array(buf[0..