Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62d6742987 | ||
|
|
66c27258da | ||
|
|
10b91eb8a2 |
@@ -1,5 +1,5 @@
|
|||||||
# SwiftSerial
|
# SwiftSerial
|
||||||
A Swift 3 Linux and Mac library for reading and writing to serial ports. This library has been tested to work on macOS Sierra, Linux Mint 18 (based on Ubuntu 16.04) and on the [Raspberry Pi 3 on Ubuntu 16.04](https://wiki.ubuntu.com/ARM/RaspberryPi). Other platforms using Ubuntu like the Beaglebone might work as well.
|
A Swift Linux and Mac library for reading and writing to serial ports. This library has been tested to work on macOS Sierra, Linux Mint 18 (based on Ubuntu 16.04) and on the [Raspberry Pi 3 on Ubuntu 16.04](https://wiki.ubuntu.com/ARM/RaspberryPi). Other platforms using Ubuntu like the Beaglebone might work as well.
|
||||||
|
|
||||||
This library is an improvement over my previous now deprecated library [SwiftLinuxSerial](https://github.com/yeokm1/SwiftLinuxSerial) which was less Swifty and supported only Linux. This library is thanks largely to [Jay Jun](https://github.com/jayjun). His original pull request can be found [here](https://github.com/yeokm1/SwiftLinuxSerial/pull/1).
|
This library is an improvement over my previous now deprecated library [SwiftLinuxSerial](https://github.com/yeokm1/SwiftLinuxSerial) which was less Swifty and supported only Linux. This library is thanks largely to [Jay Jun](https://github.com/jayjun). His original pull request can be found [here](https://github.com/yeokm1/SwiftLinuxSerial/pull/1).
|
||||||
|
|
||||||
|
|||||||
@@ -269,7 +269,7 @@ public class SerialPort {
|
|||||||
useHardwareFlowControl: Bool = false,
|
useHardwareFlowControl: Bool = false,
|
||||||
useSoftwareFlowControl: Bool = false,
|
useSoftwareFlowControl: Bool = false,
|
||||||
processOutput: Bool = false) {
|
processOutput: Bool = false) {
|
||||||
|
|
||||||
guard let fileDescriptor = fileDescriptor else {
|
guard let fileDescriptor = fileDescriptor else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -372,13 +372,13 @@ extension SerialPort {
|
|||||||
guard let fileDescriptor = fileDescriptor else {
|
guard let fileDescriptor = fileDescriptor else {
|
||||||
throw PortError.mustBeOpen
|
throw PortError.mustBeOpen
|
||||||
}
|
}
|
||||||
|
|
||||||
var s: stat = stat()
|
var s: stat = stat()
|
||||||
fstat(fileDescriptor, &s)
|
fstat(fileDescriptor, &s)
|
||||||
if s.st_nlink != 1 {
|
if s.st_nlink != 1 {
|
||||||
throw PortError.deviceNotConnected
|
throw PortError.deviceNotConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytesRead = read(fileDescriptor, buffer, size)
|
let bytesRead = read(fileDescriptor, buffer, size)
|
||||||
return bytesRead
|
return bytesRead
|
||||||
}
|
}
|
||||||
@@ -386,7 +386,7 @@ extension SerialPort {
|
|||||||
public func readData(ofLength length: Int) throws -> Data {
|
public func readData(ofLength length: Int) throws -> Data {
|
||||||
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
|
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
|
||||||
defer {
|
defer {
|
||||||
buffer.deallocate(capacity: length)
|
buffer.deallocate()
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytesRead = try readBytes(into: buffer, size: length)
|
let bytesRead = try readBytes(into: buffer, size: length)
|
||||||
@@ -425,7 +425,7 @@ extension SerialPort {
|
|||||||
var data = Data()
|
var data = Data()
|
||||||
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
|
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
|
||||||
defer {
|
defer {
|
||||||
buffer.deallocate(capacity: 1)
|
buffer.deallocate()
|
||||||
}
|
}
|
||||||
|
|
||||||
while true {
|
while true {
|
||||||
@@ -436,7 +436,7 @@ extension SerialPort {
|
|||||||
throw PortError.unableToConvertByteToCharacter
|
throw PortError.unableToConvertByteToCharacter
|
||||||
}
|
}
|
||||||
let character = CChar(buffer[0])
|
let character = CChar(buffer[0])
|
||||||
|
|
||||||
if character == terminator {
|
if character == terminator {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
@@ -459,14 +459,14 @@ extension SerialPort {
|
|||||||
|
|
||||||
public func readByte() throws -> UInt8 {
|
public func readByte() throws -> UInt8 {
|
||||||
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
|
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
buffer.deallocate(capacity: 1)
|
buffer.deallocate()
|
||||||
}
|
}
|
||||||
|
|
||||||
while true {
|
while true {
|
||||||
let bytesRead = try readBytes(into: buffer, size: 1)
|
let bytesRead = try readBytes(into: buffer, size: 1)
|
||||||
|
|
||||||
if bytesRead > 0 {
|
if bytesRead > 0 {
|
||||||
return buffer[0]
|
return buffer[0]
|
||||||
}
|
}
|
||||||
@@ -478,7 +478,7 @@ extension SerialPort {
|
|||||||
let character = UnicodeScalar(byteRead)
|
let character = UnicodeScalar(byteRead)
|
||||||
return character
|
return character
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Transmitting
|
// MARK: Transmitting
|
||||||
@@ -498,7 +498,7 @@ extension SerialPort {
|
|||||||
let size = data.count
|
let size = data.count
|
||||||
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
|
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
|
||||||
defer {
|
defer {
|
||||||
buffer.deallocate(capacity: size)
|
buffer.deallocate()
|
||||||
}
|
}
|
||||||
|
|
||||||
data.copyBytes(to: buffer, count: size)
|
data.copyBytes(to: buffer, count: size)
|
||||||
|
|||||||
Reference in New Issue
Block a user