62 Commits

Author SHA1 Message Date
Piv
4a167032f3 Add gitlab-ci file 2020-09-13 13:33:18 +09:30
Michael Pivato
27a5d92aa0 Merge branch 'dtr_support' into 'master'
Dtr support

See merge request vato007/SwiftSerial!1
2020-09-12 12:11:09 +00:00
=
2cb65bc2ff Fix linux compilation 2020-07-10 19:25:59 +09:30
Piv
34e59a7d87 Minor cleanup 2020-07-08 18:41:59 +09:30
Piv
496a62549f Add inWaiting Property 2020-07-07 22:48:17 +09:30
Piv
9799f402e7 Add dtr property to SerialPort 2020-07-04 15:14:43 +09:30
Yeo Kheng Meng
0e824cc38c Updated readme to reflect new binary transfer test. 0.1.2 version. 2020-01-26 17:22:21 +08:00
Yeo Kheng Meng
91a7ea35b6 add extra test for binary 2020-01-26 17:17:59 +08:00
Yeo Kheng Meng
46a834e147 Merge pull request #17 from Scanomat/master
Disabled input processing
2020-01-26 17:08:23 +08:00
Michael Rimestad
e37d2df358 Disabled input processing 2020-01-10 14:50:44 +01:00
Yeo Kheng Meng
86625ee414 Merge pull request #14 from nallick/swift-tools-version-5
Update SPM to swift-tools-version:5.0
2019-10-01 09:47:49 +08:00
Nick Nallick
29bbe8505f Update examples to Swift 5 2019-09-30 14:06:53 -06:00
Nick Nallick
005183c282 Update SPM to swift-tools-version:5.0 2019-09-30 13:50:13 -06:00
Yeo Kheng Meng
ddc06f53ef Merge pull request #12 from EmreSURK/master
XCode sandbox mode explained.
2019-05-02 22:16:54 +08:00
Emre ŞURK
1027251723 Merge pull request #1 from EmreSURK/EmreSURK-patch-1
Added XCode Sanbox advise.
2019-05-02 11:35:24 +03:00
Emre ŞURK
6db41b985c Added XCode Sanbox advise. 2019-05-02 11:35:08 +03:00
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
Yeo Kheng Meng
af1fa134cc Merge pull request #6 from lagomorph/compilefix
Fix compilation error
2018-03-30 13:14:27 +08:00
John Scarfone
812a55a381 make compile 2018-03-21 19:29:05 -04:00
Yeo Kheng Meng
41ddf0247a readme formatting 2017-06-24 17:56:03 +08:00
Yeo Kheng Meng
f3c3a214e3 adjust readbyte documentation 2017-06-24 17:54:36 +08:00
Yeo Kheng Meng
5536368ba7 refactor readchar to use readbyte 2017-06-24 17:40:49 +08:00
Yeo Kheng Meng
e2063420e9 Merge pull request #1 from AleyRobotics/master
read byte feature add
2017-06-24 17:35:26 +08:00
AleyRobotics Aleynikov Yuri
cfaf24357c Merge commit 'f9981b1ceca99f34ee8f38f9efd5306eda19c657'
Conflicts:
	Sources/SwiftSerial.swift
2017-06-24 11:38:25 +03:00
AleyRobotics Aleynikov Yuri
543e7d39e0 read byte feature add 2017-06-24 11:21:58 +03:00
AleyRobotics
f9981b1cec Read byte function add
Read UInt8 from port
2016-12-25 22:13:28 +03:00
Yeo Kheng Meng
d36d0132a8 Update README.md 2016-11-24 23:30:52 +08:00
Yeo Kheng Meng
b29775dfbe Update README.md 2016-11-20 16:08:54 +08:00
Yeo Kheng Meng
8a8dfcb190 slides 2016-11-20 16:00:19 +08:00
Yeo Kheng Meng
25600e48fb add extra newlines when switching roles for SwiftIMExample 2016-10-29 21:52:06 +08:00
Yeo Kheng Meng
7c06c99bd1 add missing return nil in SwiftSerialIM example 2016-10-29 21:46:27 +08:00
Yeo Kheng Meng
ad3142da95 Update README.md 2016-10-29 21:32:58 +08:00
Yeo Kheng Meng
0c14b6d2b9 Update main.swift 2016-10-29 21:24:03 +08:00
Yeo Kheng Meng
4522b9261e Update main.swift 2016-10-29 21:23:40 +08:00
Yeo Kheng Meng
144c9bbf5a Update README.md 2016-10-29 21:22:34 +08:00
Yeo Kheng Meng
20cf246eda Update README.md 2016-10-29 21:15:58 +08:00
Yeo Kheng Meng
02e0d4837e remove nonblock flag to prevent massive cpu usage during reading. Will require use of /dev/cu.* only 2016-10-29 21:11:40 +08:00
Yeo Kheng Meng
816720e15c Mac uses gcd to run background thread 2016-10-29 19:01:04 +08:00
Yeo Kheng Meng
c60e0334bd refactor stdin prep 2016-10-29 18:49:24 +08:00
Yeo Kheng Meng
ade0cdc3cd IM example on linux 2016-10-29 18:45:28 +08:00
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
Yeo Kheng Meng
640cf2f7c3 updated example 2016-10-27 09:30:05 +08:00
Yeo Kheng Meng
21adc2c8f4 add comment for Mac that root is not required to access serial port 2016-10-27 09:24:24 +08:00
Yeo Kheng Meng
4ff61e6c2c Remove Linux references 2016-10-27 09:14:42 +08:00
Yeo Kheng Meng
1cfb2028e4 Update README.md 2016-10-26 23:15:26 +08:00
Yeo Kheng Meng
eb5421114d Update README.md 2016-10-26 23:03:19 +08:00
Yeo Kheng Meng
75aac88f14 space in comment 2016-10-26 00:04:01 +08:00
Yeo Kheng Meng
860221c029 adjust code to read until new character is encountered. Example code now tests for this. 2016-10-25 23:57:25 +08:00
Yeo Kheng Meng
55fa2ee514 comment explanation for the type alias 2016-10-25 23:21:54 +08:00
Yeo Kheng Meng
58c6521bbf Correct issue where sometimes the bytesRead can be -1 especially when read() is called for the first time 2016-10-25 23:17:24 +08:00
Yeo Kheng Meng
f6730aae6f remove print top 2016-10-25 22:56:22 +08:00
Yeo Kheng Meng
4183f388a6 open port params separate for mac 2016-10-25 22:47:02 +08:00
Yeo Kheng Meng
d69f2a8f04 ability to choose parity even or odd if enabled 2016-10-25 22:23:11 +08:00
Yeo Kheng Meng
9c121788a6 adjust bug in param name 2016-10-25 21:56:08 +08:00
Yeo Kheng Meng
f206aa706c newer gitignore 2016-10-25 21:47:55 +08:00
Yeo Kheng Meng
1492511477 updated swift serial example to use proper library name 2016-10-25 21:45:49 +08:00
14 changed files with 1078 additions and 452 deletions

5
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,5 @@
build:
stage: build
image: vato.ddns.net:8083/swift:5.2.4
script:
- swift build

View File

@@ -0,0 +1,20 @@
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "SwiftSerialBinary",
dependencies: [
.package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.2")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftSerialBinary",
dependencies: ["SwiftSerial"],
path: "Sources"
),
]
)

View File

@@ -0,0 +1,60 @@
import Foundation
import SwiftSerial
print("You should do a loopback i.e short the TX and RX pins of the target serial port before testing.")
let testBinaryArray : [UInt8] = [0x11, 0x22, 0x33, 0x0D, 0x44]
let arguments = CommandLine.arguments
guard arguments.count >= 2 else {
print("Need serial port name, e.g. /dev/ttyUSB0 or /dev/cu.usbserial as the first argument.")
exit(1)
}
let portName = arguments[1]
let serialPort: SerialPort = SerialPort(path: portName)
do {
print("Attempting to open port: \(portName)")
try serialPort.openPort()
print("Serial port \(portName) opened successfully.")
defer {
serialPort.closePort()
print("Port Closed")
}
serialPort.setSettings(receiveRate: .baud9600,
transmitRate: .baud9600,
minimumBytesToRead: 1)
print("Sending: ", terminator:"")
print(testBinaryArray.map { String($0, radix: 16, uppercase: false) })
let dataToSend: Data = Data(_: testBinaryArray)
let bytesWritten = try serialPort.writeData(dataToSend)
print("Successfully wrote \(bytesWritten) bytes")
print("Waiting to receive what was written...")
let dataReceived = try serialPort.readData(ofLength: bytesWritten)
print("Received: ", terminator:"")
print(dataReceived.map { String($0, radix: 16, uppercase: false) })
if(dataToSend.elementsEqual(dataReceived)){
print("Received data is the same as transmitted data. Test successful!")
} else {
print("Uh oh! Received data is not the same as what was transmitted. This was what we received,")
print(dataReceived.map { String($0, radix: 16, uppercase: false) })
}
print("End of example");
} catch PortError.failedToOpen {
print("Serial port \(portName) failed to open. You might need root permissions.")
} catch {
print("Error: \(error)")
}

View File

@@ -1,8 +1,20 @@
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription
let package = Package( let package = Package(
name: "SwiftSerialExample", name: "SwiftSerialExample",
dependencies: [ dependencies: [
.Package(url: "https://github.com/yeokm1/SwiftSerial.git", majorVersion: 0) .package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.2")
] ],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftSerialExample",
dependencies: ["SwiftSerial"],
path: "Sources"
),
]
) )

View File

@@ -1,33 +1,40 @@
import Foundation import Foundation
import SwiftLinuxSerial import SwiftSerial
print("You should do a loopback i.e short the TX and RX pins of the target serial port before testing.") print("You should do a loopback i.e short the TX and RX pins of the target serial port before testing.")
let testString: String = "The quick brown fox jumps over the lazy dog 01234567890." let testString: String = "The quick brown fox jumps over the lazy dog 01234567890."
let numberOfMultiNewLineTest : Int = 5
let test3Strings: String = testString + "\n" + testString + "\n" + testString + "\n"
let arguments = CommandLine.arguments let arguments = CommandLine.arguments
guard arguments.count >= 2 else { guard arguments.count >= 2 else {
print("Need serial port name, e.g. /dev/ttyUSB0 as the first argument.") print("Need serial port name, e.g. /dev/ttyUSB0 or /dev/cu.usbserial as the first argument.")
exit(1) exit(1)
} }
let portName = arguments[1] let portName = arguments[1]
let serialPort: SerialPort = SerialPort(name: portName) let serialPort: SerialPort = SerialPort(path: portName)
do { do {
print("Attempting to open port: \(portName)")
try serialPort.openPort() try serialPort.openPort()
print("Serial port \(portName) opened successfully.") print("Serial port \(portName) opened successfully.")
defer { defer {
serialPort.closePort() serialPort.closePort()
print("Port Closed")
} }
serialPort.setSettings(receiveRate: .baud9600, serialPort.setSettings(receiveRate: .baud9600,
transmitRate: .baud9600, transmitRate: .baud9600,
minimumBytesToRead: 1) minimumBytesToRead: 1)
print("Writing test string <\(testString)> of \(testString.characters.count) characters to serial port") print("Writing test string <\(testString)> of \(testString.count) characters to serial port")
var bytesWritten = try serialPort.writeString(testString) let bytesWritten = try serialPort.writeString(testString)
print("Successfully wrote \(bytesWritten) bytes") print("Successfully wrote \(bytesWritten) bytes")
print("Waiting to receive what was written...") print("Waiting to receive what was written...")
@@ -41,6 +48,36 @@ do {
print("<\(stringReceived)>") print("<\(stringReceived)>")
} }
print("Now testing reading/writing of \(numberOfMultiNewLineTest) lines")
var multiLineString: String = ""
for _ in 1...numberOfMultiNewLineTest {
multiLineString += testString + "\n"
}
print("Now writing multiLineString")
var _ = try serialPort.writeString(multiLineString)
for i in 1...numberOfMultiNewLineTest {
let stringReceived = try serialPort.readLine()
if testString == stringReceived {
print("Received string \(i) is the same as transmitted section. Moving on...")
} else {
print("Uh oh! Received string \(i) is not the same as what was transmitted. This was what we received,")
print("<\(stringReceived)>")
break
}
}
print("End of example");
} catch PortError.failedToOpen { } catch PortError.failedToOpen {
print("Serial port \(portName) failed to open. You might need root permissions.") print("Serial port \(portName) failed to open. You might need root permissions.")
} catch { } catch {

View File

@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj

View File

@@ -0,0 +1,20 @@
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "SwiftSerialIM",
dependencies: [
.package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.2")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftSerialIM",
dependencies: ["SwiftSerial"],
path: "Sources"
),
]
)

View File

@@ -0,0 +1,128 @@
import Foundation
import SwiftSerial
let arguments = CommandLine.arguments
guard arguments.count >= 2 else {
print("Need serial port name, e.g. /dev/ttyUSB0 or /dev/cu.usbserial as the first argument.")
exit(1)
}
print("Connect a null modem serial cable between two machines before you continue to use this program")
let portName = arguments[1]
let serialPort: SerialPort = SerialPort(path: portName)
var myturn = true
// Prepares the stdin so we can getchar() without echoing
func prepareStdin() {
// Set up the control structure
var settings = termios()
// Get options structure for stdin
tcgetattr(STDIN_FILENO, &settings)
//Turn off ICANON and ECHO
settings.c_lflag &= ~tcflag_t(ICANON | ECHO)
tcsetattr(STDIN_FILENO, TCSANOW, &settings)
}
func getKeyPress () -> UnicodeScalar {
let valueRead: Int = Int(getchar())
guard let charRead = UnicodeScalar(valueRead) else{
return UnicodeScalar("")!
}
return charRead
}
func printToScreenFrom(myself: Bool, characterToPrint: UnicodeScalar){
if(myturn && !myself){
myturn = false
print("\n\nOther: ", terminator:"")
} else if (!myturn && myself){
myturn = true
print("\n\nMe: ", terminator:"")
}
print(characterToPrint, terminator:"")
}
func backgroundRead() {
while true{
do{
let readCharacter = try serialPort.readChar()
printToScreenFrom(myself: false, characterToPrint: readCharacter)
} catch {
print("Error: \(error)")
}
}
}
do {
print("Attempting to open port: \(portName)")
try serialPort.openPort()
print("Serial port \(portName) opened successfully.")
defer {
serialPort.closePort()
print("Port Closed")
}
serialPort.setSettings(receiveRate: .baud9600,
transmitRate: .baud9600,
minimumBytesToRead: 1)
prepareStdin()
//Turn off output buffering if not multiple threads will have problems printing
setbuf(stdout, nil);
//Run the serial port reading function in another thread
#if os(Linux)
var readingThread = pthread_t()
let pthreadFunc: @convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? = {
observer in
backgroundRead()
return nil
}
pthread_create(&readingThread, nil, pthreadFunc, nil)
#elseif os(OSX)
DispatchQueue.global(qos: .userInitiated).async {
backgroundRead()
}
#endif
print("\nReady to send and receive messages in realtime!")
print("\nMe: ", terminator:"")
while true {
let enteredKey = getKeyPress()
printToScreenFrom(myself: true, characterToPrint: enteredKey)
var _ = try serialPort.writeChar(enteredKey)
}
} catch PortError.failedToOpen {
print("Serial port \(portName) failed to open. You might need root permissions.")
} catch {
print("Error: \(error)")
}

View File

@@ -1,5 +1,19 @@
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription
let package = Package( let package = Package(
name: "SwiftSerial" name: "SwiftSerial",
products: [
.library(name: "SwiftSerial", targets: ["SwiftSerial"]),
],
dependencies: [],
targets: [
.target(
name: "SwiftSerial",
dependencies: [],
path: "Sources"
),
]
) )

216
README.md
View File

@@ -1,2 +1,216 @@
# SwiftSerial # SwiftSerial
A Swift 3 Linux and Mac library for reading and writing to serial ports. A Swift Linux and Mac library for reading and writing to serial ports. This library has been tested to work on macOS Mojove, Linux Mint 18 (based on Ubuntu 16.04) and on the [Raspberry Pi 3 on Ubuntu 16.04](https://wiki.ubuntu.com/ARM/RaspberryPi) and Raspberry Pi 4 on Raspian Buster. 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).
## Talk on this library
I gave a talk on this library and one of its examples SwiftSerialIM. Click on the links below to see the slides and video.
[![My slides on slideshare](first-slide.png)](http://www.slideshare.net/yeokm1/a-science-project-swift-serial-chat)
[![](http://img.youtube.com/vi/6PWP1eZo53s/0.jpg)](https://www.youtube.com/watch?v=6PWP1eZo53s)
## Mac OS Preparation
You should have Xcode 8 installed with the command line tools.
To develop app with XCode, enable the App Sandbox capability in Xcode, and under Hardware, select USB. (Mac Apps are sandboxed and you need the USB entitlement.)
<img src="https://user-images.githubusercontent.com/5688874/55690960-6ff8fb00-5998-11e9-9df6-7e3ebe50e19a.png" alt="Swift 3.0">
## Linux System Preparation
Varies depending on system...
## Jumping straight into sample code
To get started quickly, you can take a look at my example projects [here](Examples/).
### Example 1: Loopback Test
In order to run this example properly, you need to connect one of your (USB/UART) serial ports in a loopback manner. Basically, you short the TX and RX pins of the serial port. This library currently only support the `/dev/cu.*` variant on Mac. Read the beginning of the API usage section for more details.
```bash
git clone https://github.com/yeokm1/SwiftSerial.git
cd SwiftSerial/Examples/SwiftSerialExample/
swift build
#For Linux: You need root to access the serial port. Replace /dev/ttyUSB0 with the name of your serial port under test
sudo ./.build/debug/SwiftSerialExample /dev/ttyUSB0
#For Mac: Root is not required
./.build/debug/SwiftSerialExample /dev/cu.usbserial
#If all goes well you should see a series of messages informing you that data transmitted has been received properly.
```
### Example 2: Binary Loopback Test
Variant of example 1 but testing the transfer of binary data specifically ensuring the`0x0D` bit is not converted to another character.
```bash
git clone https://github.com/yeokm1/SwiftSerial.git
cd SwiftSerial/Examples/SwiftSerialBinary/
swift build
#For Linux: You need root to access the serial port. Replace /dev/ttyUSB0 with the name of your serial port under test
sudo ./.build/debug/SwiftSerialBinary /dev/ttyUSB0
#For Mac: Root is not required
./.build/debug/SwiftSerialBinary /dev/cu.usbserial
#If all goes well you should see a series of messages informing you that data transmitted has been received properly.
```
### Example 3: A chat app between 2 machines
In order to run this example properly, you need 2 machines connected by a [null-modem cable](https://en.wikipedia.org/wiki/Null_modem) or 2 USB-Serial adapters with the TX-RX pins connected to each other. Run a copy of my program on both machines.
```bash
git clone https://github.com/yeokm1/SwiftSerial.git
cd SwiftSerial/Examples/SwiftSerialIM/
swift build
#For Linux: You need root to access the serial port. Replace /dev/ttyUSB0 with the name of your serial port under test
sudo ./.build/debug/SwiftSerialIM /dev/ttyUSB0
#For Mac: Root is not required
./.build/debug/SwiftSerialIM /dev/cu.usbserial
```
People at both machines can now "chat" with each other.
## Integrating with your project
Add SwiftSerial as a dependency to your project by editing the `Package.swift` file.
```swift
let package = Package(
name: "NameOfMyProject",
dependencies: [
.package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.2"),
...
]
...
)
```
Make sure to `import SwiftSerial` in the source files that use my API.
Then run `swift build` to download the dependencies and compile your project. Your executable will be found in the `./.build/debug/` directory.
## API usage
### Initialise the class
```swift
let serialPort: SerialPort = SerialPort(path: portName)
```
Supply the portname that you wish to open like `/dev/ttyUSB0` or `/dev/cu.usbserial`.
For Macs, this library currently only works with the `/dev/cu.*` ports instead of the `/dev/tty.*`. I have enabled blocking on the serial port to prevent high CPU usage which will prevent the `/dev/tty.*` from working. Read more about the differences between the two [here](http://stackoverflow.com/questions/8632586/macos-whats-the-difference-between-dev-tty-and-dev-cu). If there is a problem, open an issue describing your situation and let me look into it.
### Opening the Serial Port
```swift
func openPort()
func openPort(toReceive receive: Bool, andTransmit transmit: Bool)
```
Opening the port without any parameters will set the port to receive and transmit by default. You can still choose to receive-only, transmit-only or both. Will throw `PortError.mustReceiveOrTransmit` if you set both parameters to false. Can also throw `PortError.failedToOpen` and `PortError.invalidPath`.
### Set port settings
```swift
serialPort.setSettings(receiveRate: .baud9600, transmitRate: .baud9600, minimumBytesToRead: 1)
```
The port settings call can be as simple as the above. For the baud rate, just supply both transmit and receive even if you are only intending to use one transfer direction. For example, transmitRate will be ignored if you specified `andTransmit : false` when opening the port.
`minimumBytesToRead` determines how many characters the system must wait to receive before it will return from a [read()](https://linux.die.net/man/2/read) function. If in doubt, just put 1.
This function has been defined with default settings as shown in the function definition.
```swift
func setSettings(receiveRate: BaudRate,
transmitRate: BaudRate,
minimumBytesToRead: Int,
timeout: Int = 0, /* 0 means wait indefinitely */
parityType: ParityType = .none,
sendTwoStopBits: Bool = false, /* 1 stop bit is the default */
dataBitsSize: DataBitsSize = .bits8,
useHardwareFlowControl: Bool = false,
useSoftwareFlowControl: Bool = false,
processOutput: Bool = false)
```
If the default settings do not suit you, just pass in extra parameters to override them.
### Reading data from port
There are several functions you can use to read data. All functions here are blocking till the expected number of bytes has been received or a condition has been met. All functions can throw `PortError.mustBeOpen`.
```swift
func readString(ofLength length: Int) throws -> String
```
This is the easiest to use if you are sending text data. Just provide how many bytes you expect to read. The result will then be returned as a typical Swift String. This function internally calls `readData()`.
```swift
func readData(ofLength length: Int) throws -> Data
```
This function is if you intend to receive binary data. This function internally calls `readBytes()`
```swift
func readBytes(into buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int
```
If you intend to play with unsafe pointers directly, this is the function for you! Will return the number of bytes read. Note that you are responsible for allocating the pointer before passing into this function then deallocate the pointer once you are done.
```swift
func readLine() throws -> String
```
Read byte by byte till the newline character `\n` is encountered. A String containing the result so far will be returned without the newline character. This function internally calls `readUntilChar()`. Can throw `PortError.stringsMustBeUTF8`.
```swift
func readUntilChar(_ terminator: CChar) throws -> String
```
Keep reading until the specified CChar is encountered. Return the string read so far without that value.
```swift
func readByte() throws -> UInt8
```
Read only one byte. This works best if `minimumBytesToRead` has been set to `1` when opening the port. This function internally calls `readBytes()`.
```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 `readByte()`.
### 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`.
```swift
func writeString(_ string: String) throws -> Int
```
Most straightforward function, String in then transmit! Will return how many bytes actually written. Internally calls `writeData()`
```swift
func writeData(_ data: Data) throws -> Int
```
Binary data in, then transmit! Will return how many bytes actually written. Internally calls `writeBytes()`.
```swift
func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int
```
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
Just do `serialPort.closePort()` to close the port once you are done using it.
## External References
This library cannot be written without the amazing reference code I depended on.
1. [Xanthium's Serial Port Programming on Linux](http://xanthium.in/Serial-Port-Programming-on-Linux)
2. [Chrishey Drick's Reading data from Serial Port](https://chrisheydrick.com/2012/06/17/how-to-read-serial-data-from-an-arduino-in-linux-with-c-part-3/)

View File

@@ -1,443 +1,555 @@
import Foundation import Foundation
#if os(Linux) #if os(Linux)
public enum BaudRate { public enum BaudRate {
case baud0 case baud0
case baud50 case baud50
case baud75 case baud75
case baud110 case baud110
case baud134 case baud134
case baud150 case baud150
case baud200 case baud200
case baud300 case baud300
case baud600 case baud600
case baud1200 case baud1200
case baud1800 case baud1800
case baud2400 case baud2400
case baud4800 case baud4800
case baud9600 case baud9600
case baud19200 case baud19200
case baud38400 case baud38400
case baud57600 case baud57600
case baud115200 case baud115200
case baud230400 case baud230400
case baud460800 case baud460800
case baud500000 case baud500000
case baud576000 case baud576000
case baud921600 case baud921600
case baud1000000 case baud1000000
case baud1152000 case baud1152000
case baud1500000 case baud1500000
case baud2000000 case baud2000000
case baud2500000 case baud2500000
case baud3500000 case baud3500000
case baud4000000 case baud4000000
var speedValue: speed_t { var speedValue: speed_t {
switch self { switch self {
case .baud0: case .baud0:
return speed_t(B0) return speed_t(B0)
case .baud50: case .baud50:
return speed_t(B50) return speed_t(B50)
case .baud75: case .baud75:
return speed_t(B75) return speed_t(B75)
case .baud110: case .baud110:
return speed_t(B110) return speed_t(B110)
case .baud134: case .baud134:
return speed_t(B134) return speed_t(B134)
case .baud150: case .baud150:
return speed_t(B150) return speed_t(B150)
case .baud200: case .baud200:
return speed_t(B200) return speed_t(B200)
case .baud300: case .baud300:
return speed_t(B300) return speed_t(B300)
case .baud600: case .baud600:
return speed_t(B600) return speed_t(B600)
case .baud1200: case .baud1200:
return speed_t(B1200) return speed_t(B1200)
case .baud1800: case .baud1800:
return speed_t(B1800) return speed_t(B1800)
case .baud2400: case .baud2400:
return speed_t(B2400) return speed_t(B2400)
case .baud4800: case .baud4800:
return speed_t(B4800) return speed_t(B4800)
case .baud9600: case .baud9600:
return speed_t(B9600) return speed_t(B9600)
case .baud19200: case .baud19200:
return speed_t(B19200) return speed_t(B19200)
case .baud38400: case .baud38400:
return speed_t(B38400) return speed_t(B38400)
case .baud57600: case .baud57600:
return speed_t(B57600) return speed_t(B57600)
case .baud115200: case .baud115200:
return speed_t(B115200) return speed_t(B115200)
case .baud230400: case .baud230400:
return speed_t(B230400) return speed_t(B230400)
case .baud460800: case .baud460800:
return speed_t(B460800) return speed_t(B460800)
case .baud500000: case .baud500000:
return speed_t(B500000) return speed_t(B500000)
case .baud576000: case .baud576000:
return speed_t(B576000) return speed_t(B576000)
case .baud921600: case .baud921600:
return speed_t(B921600) return speed_t(B921600)
case .baud1000000: case .baud1000000:
return speed_t(B1000000) return speed_t(B1000000)
case .baud1152000: case .baud1152000:
return speed_t(B1152000) return speed_t(B1152000)
case .baud1500000: case .baud1500000:
return speed_t(B1500000) return speed_t(B1500000)
case .baud2000000: case .baud2000000:
return speed_t(B2000000) return speed_t(B2000000)
case .baud2500000: case .baud2500000:
return speed_t(B2500000) return speed_t(B2500000)
case .baud3500000: case .baud3500000:
return speed_t(B3500000) return speed_t(B3500000)
case .baud4000000: case .baud4000000:
return speed_t(B4000000) return speed_t(B4000000)
} }
} }
} }
#elseif os(OSX) #elseif os(OSX)
public enum BaudRate { public enum BaudRate {
case baud0 case baud0
case baud50 case baud50
case baud75 case baud75
case baud110 case baud110
case baud134 case baud134
case baud150 case baud150
case baud200 case baud200
case baud300 case baud300
case baud600 case baud600
case baud1200 case baud1200
case baud1800 case baud1800
case baud2400 case baud2400
case baud4800 case baud4800
case baud9600 case baud9600
case baud19200 case baud19200
case baud38400 case baud38400
case baud57600 case baud57600
case baud115200 case baud115200
case baud230400 case baud230400
var speedValue: speed_t { var speedValue: speed_t {
switch self { switch self {
case .baud0: case .baud0:
return speed_t(B0) return speed_t(B0)
case .baud50: case .baud50:
return speed_t(B50) return speed_t(B50)
case .baud75: case .baud75:
return speed_t(B75) return speed_t(B75)
case .baud110: case .baud110:
return speed_t(B110) return speed_t(B110)
case .baud134: case .baud134:
return speed_t(B134) return speed_t(B134)
case .baud150: case .baud150:
return speed_t(B150) return speed_t(B150)
case .baud200: case .baud200:
return speed_t(B200) return speed_t(B200)
case .baud300: case .baud300:
return speed_t(B300) return speed_t(B300)
case .baud600: case .baud600:
return speed_t(B600) return speed_t(B600)
case .baud1200: case .baud1200:
return speed_t(B1200) return speed_t(B1200)
case .baud1800: case .baud1800:
return speed_t(B1800) return speed_t(B1800)
case .baud2400: case .baud2400:
return speed_t(B2400) return speed_t(B2400)
case .baud4800: case .baud4800:
return speed_t(B4800) return speed_t(B4800)
case .baud9600: case .baud9600:
return speed_t(B9600) return speed_t(B9600)
case .baud19200: case .baud19200:
return speed_t(B19200) return speed_t(B19200)
case .baud38400: case .baud38400:
return speed_t(B38400) return speed_t(B38400)
case .baud57600: case .baud57600:
return speed_t(B57600) return speed_t(B57600)
case .baud115200: case .baud115200:
return speed_t(B115200) return speed_t(B115200)
case .baud230400: case .baud230400:
return speed_t(B230400) return speed_t(B230400)
} }
} }
} }
#endif #endif
public enum DataBitsSize { #if os(OSX)
case bits5 // Darwin doesn't provide this
case bits6 private let FIONREAD: UInt = 0x541B
case bits7 #endif
case bits8
public enum DataBitsSize {
var flagValue: tcflag_t { case bits5
switch self { case bits6
case .bits5: case bits7
return tcflag_t(CS5) case bits8
case .bits6:
return tcflag_t(CS6) var flagValue: tcflag_t {
case .bits7: switch self {
return tcflag_t(CS7) case .bits5:
case .bits8: return tcflag_t(CS5)
return tcflag_t(CS8) case .bits6:
} return tcflag_t(CS6)
} case .bits7:
} return tcflag_t(CS7)
case .bits8:
public enum PortError: Int32, Error { return tcflag_t(CS8)
case failedToOpen = -1 // refer to open() }
case invalidPath }
case mustReceiveOrTransmit
case mustBeOpen }
case stringsMustBeUTF8
} public enum ParityType {
case none
public class SerialPort { case even
case odd
var path: String
var fileDescriptor: Int32? var parityValue: tcflag_t {
switch self {
public init(path: String) { case .none:
self.path = path return 0
} case .even:
return tcflag_t(PARENB)
public func openPort() throws { case .odd:
try openPort(toReceive: true, andTransmit: true) return tcflag_t(PARENB | PARODD)
} }
}
public func openPort(toReceive receive: Bool, andTransmit transmit: Bool) throws { }
guard !path.isEmpty else {
throw PortError.invalidPath public enum PortError: Int32, Error {
} case failedToOpen = -1 // refer to open()
case invalidPath
guard receive || transmit else { case mustReceiveOrTransmit
throw PortError.mustReceiveOrTransmit case mustBeOpen
} case stringsMustBeUTF8
case unableToConvertByteToCharacter
if receive && transmit { case deviceNotConnected
fileDescriptor = open(path, O_RDWR | O_NOCTTY) }
} else if receive {
fileDescriptor = open(path, O_RDONLY | O_NOCTTY) public class SerialPort {
} else if transmit {
fileDescriptor = open(path, O_WRONLY | O_NOCTTY) var path: String
} else { var fileDescriptor: Int32?
fatalError() private var dtrState = false
}
public init(path: String) {
// Throw error if open() failed self.path = path
if fileDescriptor == PortError.failedToOpen.rawValue { }
throw PortError.failedToOpen
} public func openPort() throws {
} try openPort(toReceive: true, andTransmit: true)
}
public func setSettings(receiveRate: BaudRate,
transmitRate: BaudRate, public func openPort(toReceive receive: Bool, andTransmit transmit: Bool) throws {
minimumBytesToRead: Int, guard !path.isEmpty else {
timeout: Int = 0, /* 0 means wait indefinitely */ throw PortError.invalidPath
enableParity: Bool = false, }
sendTwoStopBits: Bool = false, /* 1 stop bit is the default */
dataBitsSize: DataBitsSize = .bits8, guard receive || transmit else {
useHardwareFlowControl: Bool = false, throw PortError.mustReceiveOrTransmit
useSoftwareFlowControl: Bool = false, }
processOutput: Bool = false) {
guard let fileDescriptor = fileDescriptor else { var readWriteParam : Int32
return
} if receive && transmit {
readWriteParam = O_RDWR
// Set up the control structure } else if receive {
var settings = termios() readWriteParam = O_RDONLY
} else if transmit {
// Get options structure for the port readWriteParam = O_WRONLY
tcgetattr(fileDescriptor, &settings) } else {
fatalError()
// Set baud rates }
cfsetispeed(&settings, receiveRate.speedValue)
cfsetospeed(&settings, transmitRate.speedValue) #if os(Linux)
fileDescriptor = open(path, readWriteParam | O_NOCTTY)
// Set parity enable flag #elseif os(OSX)
if enableParity { fileDescriptor = open(path, readWriteParam | O_NOCTTY | O_EXLOCK)
settings.c_cflag |= ~tcflag_t(PARENB) #endif
} else {
settings.c_cflag &= ~tcflag_t(PARENB) // Throw error if open() failed
} if fileDescriptor == PortError.failedToOpen.rawValue {
throw PortError.failedToOpen
// Set stop bit flag }
if sendTwoStopBits { }
settings.c_cflag |= tcflag_t(CSTOPB)
} else { public func setSettings(receiveRate: BaudRate,
settings.c_cflag &= ~tcflag_t(CSTOPB) transmitRate: BaudRate,
} minimumBytesToRead: Int,
timeout: Int = 0, /* 0 means wait indefinitely */
// Set data bits size flag parityType: ParityType = .none,
settings.c_cflag &= ~tcflag_t(CSIZE) sendTwoStopBits: Bool = false, /* 1 stop bit is the default */
settings.c_cflag |= dataBitsSize.flagValue dataBitsSize: DataBitsSize = .bits8,
useHardwareFlowControl: Bool = false,
// Set hardware flow control flag useSoftwareFlowControl: Bool = false,
#if os(Linux) processOutput: Bool = false) {
if useHardwareFlowControl {
settings.c_cflag |= tcflag_t(CRTSCTS) guard let fileDescriptor = fileDescriptor else {
} else { return
settings.c_cflag &= ~tcflag_t(CRTSCTS) }
}
#elseif os(OSX)
if useHardwareFlowControl { // Set up the control structure
settings.c_cflag |= tcflag_t(CRTS_IFLOW) var settings = termios()
settings.c_cflag |= tcflag_t(CCTS_OFLOW)
} else { // Get options structure for the port
settings.c_cflag &= ~tcflag_t(CRTS_IFLOW) tcgetattr(fileDescriptor, &settings)
settings.c_cflag &= ~tcflag_t(CCTS_OFLOW)
} // Set baud rates
#endif cfsetispeed(&settings, receiveRate.speedValue)
cfsetospeed(&settings, transmitRate.speedValue)
// Set software flow control flags
let softwareFlowControlFlags = tcflag_t(IXON | IXOFF | IXANY) // Enable parity (even/odd) if needed
if useSoftwareFlowControl { settings.c_cflag |= parityType.parityValue
settings.c_iflag |= softwareFlowControlFlags
} else { // Set stop bit flag
settings.c_iflag &= ~softwareFlowControlFlags if sendTwoStopBits {
} settings.c_cflag |= tcflag_t(CSTOPB)
} else {
// Turn on the receiver of the serial port, and ignore modem control lines settings.c_cflag &= ~tcflag_t(CSTOPB)
settings.c_cflag |= tcflag_t(CREAD | CLOCAL) }
// Turn off canonical mode // Set data bits size flag
settings.c_lflag &= ~tcflag_t(ICANON | ECHO | ECHOE | ISIG) settings.c_cflag &= ~tcflag_t(CSIZE)
settings.c_cflag |= dataBitsSize.flagValue
// Set output processing flag
if processOutput { //Disable input mapping of CR to NL, mapping of NL into CR, and ignoring CR
settings.c_oflag |= tcflag_t(OPOST) settings.c_iflag &= ~tcflag_t(ICRNL | INLCR | IGNCR)
} else {
settings.c_oflag &= ~tcflag_t(OPOST) // Set hardware flow control flag
} #if os(Linux)
if useHardwareFlowControl {
// Special characters settings.c_cflag |= tcflag_t(CRTSCTS)
#if os(Linux) } else {
typealias specialCharactersTuple = (VINTR: cc_t, VQUIT: cc_t, VERASE: cc_t, VKILL: cc_t, VEOF: cc_t, VTIME: cc_t, VMIN: cc_t, VSWTC: cc_t, VSTART: cc_t, VSTOP: cc_t, VSUSP: cc_t, VEOL: cc_t, VREPRINT: cc_t, VDISCARD: cc_t, VWERASE: cc_t, VLNEXT: cc_t, VEOL2: cc_t, spare1: cc_t, spare2: cc_t, spare3: cc_t, spare4: cc_t, spare5: cc_t, spare6: cc_t, spare7: cc_t, spare8: cc_t, spare9: cc_t, spare10: cc_t, spare11: cc_t, spare12: cc_t, spare13: cc_t, spare14: cc_t, spare15: cc_t) settings.c_cflag &= ~tcflag_t(CRTSCTS)
var specialCharacters: specialCharactersTuple = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) // NCCS = 32 }
#elseif os(OSX) #elseif os(OSX)
typealias specialCharactersTuple = (VEOF: cc_t, VEOL: cc_t, VEOL2: cc_t, VERASE: cc_t, VWERASE: cc_t, VKILL: cc_t, VREPRINT: cc_t, spare1: cc_t, VINTR: cc_t, VQUIT: cc_t, VSUSP: cc_t, VDSUSP: cc_t, VSTART: cc_t, VSTOP: cc_t, VLNEXT: cc_t, VDISCARD: cc_t, VMIN: cc_t, VTIME: cc_t, VSTATUS: cc_t, spare: cc_t) if useHardwareFlowControl {
var specialCharacters: specialCharactersTuple = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) // NCCS = 20 settings.c_cflag |= tcflag_t(CRTS_IFLOW)
#endif settings.c_cflag |= tcflag_t(CCTS_OFLOW)
} else {
specialCharacters.VMIN = cc_t(minimumBytesToRead) settings.c_cflag &= ~tcflag_t(CRTS_IFLOW)
specialCharacters.VTIME = cc_t(timeout) settings.c_cflag &= ~tcflag_t(CCTS_OFLOW)
settings.c_cc = specialCharacters }
#endif
// Commit settings
tcsetattr(fileDescriptor, TCSANOW, &settings) // Set software flow control flags
} let softwareFlowControlFlags = tcflag_t(IXON | IXOFF | IXANY)
if useSoftwareFlowControl {
public func closePort() { settings.c_iflag |= softwareFlowControlFlags
if let fileDescriptor = fileDescriptor { } else {
close(fileDescriptor) settings.c_iflag &= ~softwareFlowControlFlags
} }
fileDescriptor = nil
} // Turn on the receiver of the serial port, and ignore modem control lines
} settings.c_cflag |= tcflag_t(CREAD | CLOCAL)
// MARK: Receiving // Turn off canonical mode
settings.c_lflag &= ~tcflag_t(ICANON | ECHO | ECHOE | ISIG)
extension SerialPort {
// Set output processing flag
public func readBytes(into buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int { if processOutput {
guard let fileDescriptor = fileDescriptor else { settings.c_oflag |= tcflag_t(OPOST)
throw PortError.mustBeOpen } else {
} settings.c_oflag &= ~tcflag_t(OPOST)
}
let bytesRead = read(fileDescriptor, buffer, size)
return bytesRead //Special characters
} //We do this as c_cc is a C-fixed array which is imported as a tuple in Swift.
//To avoid hardcoding the VMIN or VTIME value to access the tuple value, we use the typealias instead
public func readData(ofLength length: Int) throws -> Data { #if os(Linux)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: length) typealias specialCharactersTuple = (VINTR: cc_t, VQUIT: cc_t, VERASE: cc_t, VKILL: cc_t, VEOF: cc_t, VTIME: cc_t, VMIN: cc_t, VSWTC: cc_t, VSTART: cc_t, VSTOP: cc_t, VSUSP: cc_t, VEOL: cc_t, VREPRINT: cc_t, VDISCARD: cc_t, VWERASE: cc_t, VLNEXT: cc_t, VEOL2: cc_t, spare1: cc_t, spare2: cc_t, spare3: cc_t, spare4: cc_t, spare5: cc_t, spare6: cc_t, spare7: cc_t, spare8: cc_t, spare9: cc_t, spare10: cc_t, spare11: cc_t, spare12: cc_t, spare13: cc_t, spare14: cc_t, spare15: cc_t)
defer { var specialCharacters: specialCharactersTuple = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) // NCCS = 32
buffer.deallocate(capacity: length) #elseif os(OSX)
} typealias specialCharactersTuple = (VEOF: cc_t, VEOL: cc_t, VEOL2: cc_t, VERASE: cc_t, VWERASE: cc_t, VKILL: cc_t, VREPRINT: cc_t, spare1: cc_t, VINTR: cc_t, VQUIT: cc_t, VSUSP: cc_t, VDSUSP: cc_t, VSTART: cc_t, VSTOP: cc_t, VLNEXT: cc_t, VDISCARD: cc_t, VMIN: cc_t, VTIME: cc_t, VSTATUS: cc_t, spare: cc_t)
var specialCharacters: specialCharactersTuple = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) // NCCS = 20
let bytesRead = try readBytes(into: buffer, size: length) #endif
let data = Data(bytes: buffer, count: bytesRead)
return data specialCharacters.VMIN = cc_t(minimumBytesToRead)
} specialCharacters.VTIME = cc_t(timeout)
settings.c_cc = specialCharacters
public func readString(ofLength length: Int) throws -> String {
var remainingBytesToRead = length // Commit settings
var result = "" tcsetattr(fileDescriptor, TCSANOW, &settings)
}
while remainingBytesToRead > 0 {
let data = try readData(ofLength: remainingBytesToRead) public func closePort() {
if let string = String(data: data, encoding: String.Encoding.utf8) { if let fileDescriptor = fileDescriptor {
result += string close(fileDescriptor)
remainingBytesToRead -= data.count }
} else { fileDescriptor = nil
return result }
}
} public var dtr: Bool {
get{
return result return dtrState
} }
set (value){
public func readUntilChar(_ terminator: CChar) throws -> String { guard let fileDescriptor = fileDescriptor else {
var data = Data() // Need to open port first.
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1) return
defer { }
buffer.deallocate(capacity: 1) dtrState = value
} var flags = TIOCM_DTR
if(ioctl(fileDescriptor, UInt(dtrState ? TIOCMBIS : TIOCMBIC), &flags) != 0){
// Read byte by byte print("Failed to apply dtr")
while try readBytes(into: buffer, size: 1) > 0 { }
let character = CChar(buffer[0]) }
if character != terminator { }
data.append(buffer, count: 1)
} else { public var inWaiting: Int {
break get{
} return Int(ioctl(fileDescriptor!, UInt(FIONREAD)))
} }
}
if let string = String(data: data, encoding: String.Encoding.utf8) { }
return string
} else { // MARK: Receiving
throw PortError.stringsMustBeUTF8
} extension SerialPort {
}
public func readBytes(into buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int {
public func readLine() throws -> String { guard let fileDescriptor = fileDescriptor else {
let newlineChar = CChar(10) // Newline/Line feed character `\n` is 10 throw PortError.mustBeOpen
return try readUntilChar(newlineChar) }
}
} var s: stat = stat()
fstat(fileDescriptor, &s)
// MARK: Transmitting if s.st_nlink != 1 {
throw PortError.deviceNotConnected
extension SerialPort { }
public func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int { let bytesRead = read(fileDescriptor, buffer, size)
guard let fileDescriptor = fileDescriptor else { return bytesRead
throw PortError.mustBeOpen }
}
public func readData(ofLength length: Int) throws -> Data {
let bytesWritten = write(fileDescriptor, buffer, size) let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
return bytesWritten defer {
} buffer.deallocate()
}
public func writeData(_ data: Data) throws -> Int {
let size = data.count let bytesRead = try readBytes(into: buffer, size: length)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
defer { var data : Data
buffer.deallocate(capacity: size)
} if bytesRead > 0 {
data = Data(bytes: buffer, count: bytesRead)
data.copyBytes(to: buffer, count: size) } else {
//This is to avoid the case where bytesRead can be negative causing problems allocating the Data buffer
let bytesWritten = try writeBytes(from: buffer, size: size) data = Data(bytes: buffer, count: 0)
return bytesWritten }
}
return data
public func writeString(_ string: String) throws -> Int { }
guard let data = string.data(using: String.Encoding.utf8) else {
throw PortError.stringsMustBeUTF8 public func readString(ofLength length: Int) throws -> String {
} var remainingBytesToRead = length
var result = ""
return try writeData(data)
} while remainingBytesToRead > 0 {
} let data = try readData(ofLength: remainingBytesToRead)
if let string = String(data: data, encoding: String.Encoding.utf8) {
result += string
remainingBytesToRead -= data.count
} else {
return result
}
}
return result
}
public func readUntilChar(_ terminator: CChar) throws -> String {
var data = Data()
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer {
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 {
break
} else {
data.append(buffer, count: 1)
}
}
}
if let string = String(data: data, encoding: String.Encoding.utf8) {
return string
} else {
throw PortError.stringsMustBeUTF8
}
}
public func readLine() throws -> String {
let newlineChar = CChar(10) // Newline/Line feed character `\n` is 10
return try readUntilChar(newlineChar)
}
public func readByte() throws -> UInt8 {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer {
buffer.deallocate()
}
while true {
let bytesRead = try readBytes(into: buffer, size: 1)
if bytesRead > 0 {
return buffer[0]
}
}
}
public func readChar() throws -> UnicodeScalar {
let byteRead = try readByte()
let character = UnicodeScalar(byteRead)
return character
}
}
// MARK: Transmitting
extension SerialPort {
public func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int {
guard let fileDescriptor = fileDescriptor else {
throw PortError.mustBeOpen
}
let bytesWritten = write(fileDescriptor, buffer, size)
return bytesWritten
}
public func writeData(_ data: Data) throws -> Int {
let size = data.count
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
defer {
buffer.deallocate()
}
data.copyBytes(to: buffer, count: size)
let bytesWritten = try writeBytes(from: buffer, size: size)
return bytesWritten
}
public func writeString(_ string: String) throws -> Int {
guard let data = string.data(using: String.Encoding.utf8) else {
throw PortError.stringsMustBeUTF8
}
return try writeData(data)
}
public func writeChar(_ character: UnicodeScalar) throws -> Int{
let stringEquiv = String(character)
let bytesWritten = try writeString(stringEquiv)
return bytesWritten
}
}

BIN
first-slide.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 KiB

Binary file not shown.