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

Commit

Permalink
Swift 5
Browse files Browse the repository at this point in the history
  • Loading branch information
5sw committed Apr 7, 2019
1 parent 581ebc7 commit 10b91eb
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Sources/SwiftSerial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public class SerialPort {
useHardwareFlowControl: Bool = false,
useSoftwareFlowControl: Bool = false,
processOutput: Bool = false) {

guard let fileDescriptor = fileDescriptor else {
return
}
Expand Down Expand Up @@ -372,21 +372,21 @@ 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
}

public func readData(ofLength length: Int) throws -> Data {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
defer {
buffer.deallocate(capacity: length)
buffer.deallocate()
}

let bytesRead = try readBytes(into: buffer, size: length)
Expand Down Expand Up @@ -425,7 +425,7 @@ extension SerialPort {
var data = Data()
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer {
buffer.deallocate(capacity: 1)
buffer.deallocate()
}

while true {
Expand All @@ -436,7 +436,7 @@ extension SerialPort {
throw PortError.unableToConvertByteToCharacter
}
let character = CChar(buffer[0])

if character == terminator {
break
} else {
Expand All @@ -459,14 +459,14 @@ extension SerialPort {

public func readByte() throws -> UInt8 {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)

defer {
buffer.deallocate(capacity: 1)
buffer.deallocate()
}

while true {
let bytesRead = try readBytes(into: buffer, size: 1)

if bytesRead > 0 {
return buffer[0]
}
Expand All @@ -478,7 +478,7 @@ extension SerialPort {
let character = UnicodeScalar(byteRead)
return character
}

}

// MARK: Transmitting
Expand All @@ -498,7 +498,7 @@ extension SerialPort {
let size = data.count
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
defer {
buffer.deallocate(capacity: size)
buffer.deallocate()
}

data.copyBytes(to: buffer, count: size)
Expand Down

0 comments on commit 10b91eb

Please sign in to comment.