update with new character specific functions

This commit is contained in:
Yeo Kheng Meng
2016-10-29 17:41:27 +08:00
parent 640cf2f7c3
commit e73ea6056d

View File

@@ -191,7 +191,7 @@ public enum ParityType {
case even case even
case odd case odd
var parityValue: tcflag_t { var parityValue: tcflag_t {
switch self { switch self {
case .none: case .none:
return 0 return 0
@@ -389,7 +389,7 @@ extension SerialPort {
data = Data(bytes: buffer, count: bytesRead) data = Data(bytes: buffer, count: bytesRead)
} else { } else {
//This is to avoid the case where bytesRead can be negative causing problems allocating the Data buffer //This is to avoid the case where bytesRead can be negative causing problems allocating the Data buffer
data = Data(bytes: buffer, count: 0) data = Data(bytes: buffer, count: 0)
} }
return data return data
@@ -421,17 +421,17 @@ extension SerialPort {
} }
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 {
let character = CChar(buffer[0]) let character = CChar(buffer[0])
if character == terminator { if character == terminator {
break break
} else { } else {
data.append(buffer, count: 1) data.append(buffer, count: 1)
} }
} }
} }
if let string = String(data: data, encoding: String.Encoding.utf8) { if let string = String(data: data, encoding: String.Encoding.utf8) {
@@ -445,6 +445,25 @@ extension SerialPort {
let newlineChar = CChar(10) // Newline/Line feed character `\n` is 10 let newlineChar = CChar(10) // Newline/Line feed character `\n` is 10
return try readUntilChar(newlineChar) return try readUntilChar(newlineChar)
} }
public func readChar() throws -> UnicodeScalar {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer {
buffer.deallocate(capacity: 1)
}
while true {
let bytesRead = try readBytes(into: buffer, size: 1)
if bytesRead > 0 {
let character = UnicodeScalar(buffer[0])
return character
}
}
}
} }
// MARK: Transmitting // MARK: Transmitting
@@ -480,4 +499,10 @@ extension SerialPort {
return try writeData(data) return try writeData(data)
} }
public func writeChar(_ character: UnicodeScalar) throws -> Int{
let stringEquiv = String(character)
let bytesWritten = try writeString(stringEquiv)
return bytesWritten
}
} }