6 Commits

Author SHA1 Message Date
Yeo Kheng Meng
62d6742987 update readme 2019-04-08 15:29:04 +08:00
Yeo Kheng Meng
66c27258da Merge pull request #8 from 5sw/master
Update for Swift 4/5
2019-04-08 15:26:00 +08:00
Sven Weidauer
10b91eb8a2 Swift 5 2019-04-07 12:23:59 +02:00
Yeo Kheng Meng
581ebc7e10 Merge pull request #7 from DasenB/master
catch type conversion error and prevent infinite loop
2018-09-16 21:52:48 +08:00
bjarnedevel0per
db7a3b79a2 handle removal of the serialdevice 2018-09-11 10:26:36 +02:00
bjarnedevel0per
1b9591e07a catch type conversion error 2018-09-11 08:30:41 +02:00
2 changed files with 22 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
# 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).

View File

@@ -209,6 +209,8 @@ public enum PortError: Int32, Error {
case mustReceiveOrTransmit
case mustBeOpen
case stringsMustBeUTF8
case unableToConvertByteToCharacter
case deviceNotConnected
}
public class SerialPort {
@@ -371,6 +373,12 @@ extension SerialPort {
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
}
@@ -378,7 +386,7 @@ extension SerialPort {
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)
@@ -417,13 +425,16 @@ extension SerialPort {
var data = Data()
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 {
if ( buffer[0] > 127) {
throw PortError.unableToConvertByteToCharacter
}
let character = CChar(buffer[0])
if character == terminator {
@@ -450,7 +461,7 @@ extension SerialPort {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer {
buffer.deallocate(capacity: 1)
buffer.deallocate()
}
while true {
@@ -487,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)