Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from DasenB/master
Browse files Browse the repository at this point in the history
catch type conversion error and prevent infinite loop
  • Loading branch information
yeokm1 authored Sep 16, 2018
2 parents af1fa13 + db7a3b7 commit 581ebc7
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Sources/SwiftSerial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ public enum PortError: Int32, Error {
case mustReceiveOrTransmit
case mustBeOpen
case stringsMustBeUTF8
case unableToConvertByteToCharacter
case deviceNotConnected
}

public class SerialPort {
Expand Down Expand Up @@ -370,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
}
Expand Down Expand Up @@ -424,6 +432,9 @@ extension SerialPort {
let bytesRead = try readBytes(into: buffer, size: 1)

if bytesRead > 0 {
if ( buffer[0] > 127) {
throw PortError.unableToConvertByteToCharacter
}
let character = CChar(buffer[0])

if character == terminator {
Expand Down

0 comments on commit 581ebc7

Please sign in to comment.