2 Commits
0.0.6 ... 0.0.7

Author SHA1 Message Date
Yeo Kheng Meng
3e4b905e7b Update README.md 2016-10-29 17:48:52 +08:00
Yeo Kheng Meng
e73ea6056d update with new character specific functions 2016-10-29 17:41:27 +08:00
2 changed files with 41 additions and 6 deletions

View File

@@ -166,6 +166,11 @@ func readUntilChar(_ terminator: CChar) throws -> String
``` ```
Keep reading until the specified CChar is encountered. Return the string read so far without that value. Keep reading until the specified CChar is encountered. Return the string read so far without that value.
```swift
func readChar() throws -> UnicodeScalar
```
Read only one character. This works best if `minimumBytesToRead` has been set to `1` when opening the port. This function internally calls `readBytes()`.
### Writing data to the port ### Writing data to the port
There are several functions you can use to write data. All functions here are blocking till all the data has been written. All functions can throw `PortError.mustBeOpen`. There are several functions you can use to write data. All functions here are blocking till all the data has been written. All functions can throw `PortError.mustBeOpen`.
@@ -185,6 +190,11 @@ func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> I
``` ```
Function for those that want to mess with unsafe pointers. You have to specify how many bytes have to be written. Will return how many bytes actually written. Function for those that want to mess with unsafe pointers. You have to specify how many bytes have to be written. Will return how many bytes actually written.
```swift
func writeChar(_ character: UnicodeScalar) throws -> Int{
```
Writes only one character. Will return `1` if successful. This function internally calls `writeString()`. Pull requests for a better way of doing this is appreciated.
### Closing the port ### Closing the port
Just do `serialPort.closePort()` to close the port once you are done using it. Just do `serialPort.closePort()` to close the port once you are done using it.

View File

@@ -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
}
} }