Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e824cc38c | ||
|
|
91a7ea35b6 | ||
|
|
46a834e147 | ||
|
|
e37d2df358 | ||
|
|
86625ee414 | ||
|
|
29bbe8505f | ||
|
|
005183c282 | ||
|
|
ddc06f53ef | ||
|
|
1027251723 | ||
|
|
6db41b985c | ||
|
|
62d6742987 | ||
|
|
66c27258da | ||
|
|
10b91eb8a2 | ||
|
|
581ebc7e10 | ||
|
|
db7a3b79a2 | ||
|
|
1b9591e07a | ||
|
|
af1fa134cc | ||
|
|
812a55a381 | ||
|
|
41ddf0247a | ||
|
|
f3c3a214e3 | ||
|
|
5536368ba7 | ||
|
|
e2063420e9 | ||
|
|
cfaf24357c | ||
|
|
543e7d39e0 | ||
|
|
f9981b1cec | ||
|
|
d36d0132a8 | ||
|
|
b29775dfbe | ||
|
|
8a8dfcb190 | ||
|
|
25600e48fb | ||
|
|
7c06c99bd1 | ||
|
|
ad3142da95 |
20
Examples/SwiftSerialBinary/Package.swift
Normal file
20
Examples/SwiftSerialBinary/Package.swift
Normal 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"
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
60
Examples/SwiftSerialBinary/Sources/main.swift
Normal file
60
Examples/SwiftSerialBinary/Sources/main.swift
Normal 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)")
|
||||||
|
}
|
||||||
4
Examples/SwiftSerialBinary/gitignore
Normal file
4
Examples/SwiftSerialBinary/gitignore
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
.DS_Store
|
||||||
|
/.build
|
||||||
|
/Packages
|
||||||
|
/*.xcodeproj
|
||||||
@@ -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"
|
||||||
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ do {
|
|||||||
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...")
|
||||||
@@ -54,7 +54,7 @@ do {
|
|||||||
var multiLineString: String = ""
|
var multiLineString: String = ""
|
||||||
|
|
||||||
|
|
||||||
for i in 1...numberOfMultiNewLineTest {
|
for _ in 1...numberOfMultiNewLineTest {
|
||||||
multiLineString += testString + "\n"
|
multiLineString += testString + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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: "SwiftSerialIM",
|
name: "SwiftSerialIM",
|
||||||
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: "SwiftSerialIM",
|
||||||
|
dependencies: ["SwiftSerial"],
|
||||||
|
path: "Sources"
|
||||||
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -44,10 +44,10 @@ func printToScreenFrom(myself: Bool, characterToPrint: UnicodeScalar){
|
|||||||
|
|
||||||
if(myturn && !myself){
|
if(myturn && !myself){
|
||||||
myturn = false
|
myturn = false
|
||||||
print("\nOther: ", terminator:"")
|
print("\n\nOther: ", terminator:"")
|
||||||
} else if (!myturn && myself){
|
} else if (!myturn && myself){
|
||||||
myturn = true
|
myturn = true
|
||||||
print("\nMe: ", terminator:"")
|
print("\n\nMe: ", terminator:"")
|
||||||
}
|
}
|
||||||
|
|
||||||
print(characterToPrint, terminator:"")
|
print(characterToPrint, terminator:"")
|
||||||
@@ -95,6 +95,7 @@ do {
|
|||||||
observer in
|
observer in
|
||||||
|
|
||||||
backgroundRead()
|
backgroundRead()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_create(&readingThread, nil, pthreadFunc, nil)
|
pthread_create(&readingThread, nil, pthreadFunc, nil)
|
||||||
@@ -112,7 +113,7 @@ do {
|
|||||||
|
|
||||||
|
|
||||||
while true {
|
while true {
|
||||||
var enteredKey = getKeyPress()
|
let enteredKey = getKeyPress()
|
||||||
printToScreenFrom(myself: true, characterToPrint: enteredKey)
|
printToScreenFrom(myself: true, characterToPrint: enteredKey)
|
||||||
var _ = try serialPort.writeChar(enteredKey)
|
var _ = try serialPort.writeChar(enteredKey)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
|
),
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
93
README.md
93
README.md
@@ -1,69 +1,33 @@
|
|||||||
# 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 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).
|
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).
|
||||||
|
|
||||||
<p>
|
## Talk on this library
|
||||||

|
|
||||||
<img src="https://img.shields.io/badge/OS-Ubuntu-blue.svg?style=flat" alt="Swift 3.0">
|
I gave a talk on this library and one of its examples SwiftSerialIM. Click on the links below to see the slides and video.
|
||||||
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/swift3-compatible-orange.svg?style=flat" alt="Swift 3 compatible" /></a>
|
|
||||||
<a href="https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a>
|
[](http://www.slideshare.net/yeokm1/a-science-project-swift-serial-chat)
|
||||||
|
|
||||||
|
[](https://www.youtube.com/watch?v=6PWP1eZo53s)
|
||||||
|
|
||||||
## Mac OS Preparation
|
## Mac OS Preparation
|
||||||
|
|
||||||
You should have Xcode 8 installed with the command line tools.
|
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
|
## Linux System Preparation
|
||||||
|
|
||||||
Before using this library, I assume you already have Ubuntu installed and fully updated on your system or single-board computer. To get Ubuntu installed on the Raspberry Pi, use this [link](https://wiki.ubuntu.com/ARM/RaspberryPi).
|
Varies depending on system...
|
||||||
|
|
||||||
### Install Swift 3 on Ubuntu on x86-based machines
|
|
||||||
|
|
||||||
Reference instructions obtained from [here](http://dev.iachieved.it/iachievedit/swift-3-0-for-ubuntu-16-04-xenial-xerus/). We will use a Swift binary produced by iachievedit.
|
|
||||||
```bash
|
|
||||||
#Add the repository key for iachievedit
|
|
||||||
wget -qO- http://dev.iachieved.it/iachievedit.gpg.key | sudo apt-key add -
|
|
||||||
|
|
||||||
#Add the Xenial repository to sources.list
|
|
||||||
echo "deb http://iachievedit-repos.s3.amazonaws.com/ xenial main" | sudo tee --append /etc/apt/sources.list
|
|
||||||
|
|
||||||
sudo apt-get update
|
|
||||||
sudo apt-get install swift-3.0
|
|
||||||
|
|
||||||
nano ~/.profile
|
|
||||||
#This command can be added to your bash profile so Swift will be in your PATH after a reboot
|
|
||||||
export PATH=/opt/swift/swift-3.0/usr/bin:$PATH
|
|
||||||
```
|
|
||||||
|
|
||||||
### Install Swift 3 on Ubuntu on Raspberry Pi 3
|
|
||||||
Instructions from this section is referenced from this [link](http://dev.iachieved.it/iachievedit/swift-3-0-on-raspberry-pi-2-and-3/).
|
|
||||||
|
|
||||||
Since Swift 3 is still rapidly evolving, we should not use the Swift packages provided via the apt package manager if they exist and instead use prebuilt binaries instead. We will also not install Swift 3 to the system-level directories to avoid problems in case we have to update the version.
|
|
||||||
|
|
||||||
Go to this [page](http://swift-arm.ddns.net/job/Swift-3.0-Pi3-ARM-Incremental/lastSuccessfulBuild/artifact/) and find what it is the link to the latest Swift compiled `tar.gz` package.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
#Install dependencies
|
|
||||||
sudo apt-get install libcurl4-openssl-dev libicu-dev clang-3.6
|
|
||||||
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100
|
|
||||||
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
|
|
||||||
|
|
||||||
cd ~
|
|
||||||
#Replace the link below with the latest version
|
|
||||||
wget http://swift-arm.ddns.net/job/Swift-3.0-Pi3-ARM-Incremental/lastSuccessfulBuild/artifact/swift-3.0-2016-10-13-RPi23-ubuntu16.04.tar.gz
|
|
||||||
mkdir swift-3.0
|
|
||||||
cd swift-3.0 && tar -xzf ../swift-3.0-2016-10-13-RPi23-ubuntu16.04.tar.gz
|
|
||||||
|
|
||||||
#This command can be added to your bash profile so Swift will be in your PATH after a reboot
|
|
||||||
nano ~/.profile
|
|
||||||
export PATH=$HOME/swift-3.0/usr/bin:$PATH
|
|
||||||
```
|
|
||||||
## Jumping straight into sample code
|
## Jumping straight into sample code
|
||||||
To get started quickly, you can take a look at my example projects [here](Examples/).
|
To get started quickly, you can take a look at my example projects [here](Examples/).
|
||||||
|
|
||||||
### Example 1: Loopback Test
|
### 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.
|
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
|
```bash
|
||||||
git clone https://github.com/yeokm1/SwiftSerial.git
|
git clone https://github.com/yeokm1/SwiftSerial.git
|
||||||
@@ -79,7 +43,25 @@ sudo ./.build/debug/SwiftSerialExample /dev/ttyUSB0
|
|||||||
#If all goes well you should see a series of messages informing you that data transmitted has been received properly.
|
#If all goes well you should see a series of messages informing you that data transmitted has been received properly.
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example 2: An chat app between 2 machines
|
### 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.
|
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.
|
||||||
|
|
||||||
@@ -104,7 +86,7 @@ Add SwiftSerial as a dependency to your project by editing the `Package.swift` f
|
|||||||
let package = Package(
|
let package = Package(
|
||||||
name: "NameOfMyProject",
|
name: "NameOfMyProject",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.Package(url: "https://github.com/yeokm1/SwiftSerial.git", majorVersion: 0),
|
.package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.2"),
|
||||||
...
|
...
|
||||||
]
|
]
|
||||||
...
|
...
|
||||||
@@ -124,7 +106,7 @@ let serialPort: SerialPort = SerialPort(path: portName)
|
|||||||
```
|
```
|
||||||
Supply the portname that you wish to open like `/dev/ttyUSB0` or `/dev/cu.usbserial`.
|
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 an problem, open an issue describing your situation and let me look into it.
|
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
|
### Opening the Serial Port
|
||||||
|
|
||||||
@@ -188,10 +170,15 @@ 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 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
|
```swift
|
||||||
func readChar() throws -> UnicodeScalar
|
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()`.
|
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
|
### Writing data to the port
|
||||||
|
|
||||||
|
|||||||
@@ -209,6 +209,8 @@ public enum PortError: Int32, Error {
|
|||||||
case mustReceiveOrTransmit
|
case mustReceiveOrTransmit
|
||||||
case mustBeOpen
|
case mustBeOpen
|
||||||
case stringsMustBeUTF8
|
case stringsMustBeUTF8
|
||||||
|
case unableToConvertByteToCharacter
|
||||||
|
case deviceNotConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SerialPort {
|
public class SerialPort {
|
||||||
@@ -297,6 +299,9 @@ public class SerialPort {
|
|||||||
settings.c_cflag &= ~tcflag_t(CSIZE)
|
settings.c_cflag &= ~tcflag_t(CSIZE)
|
||||||
settings.c_cflag |= dataBitsSize.flagValue
|
settings.c_cflag |= dataBitsSize.flagValue
|
||||||
|
|
||||||
|
//Disable input mapping of CR to NL, mapping of NL into CR, and ignoring CR
|
||||||
|
settings.c_iflag &= ~tcflag_t(ICRNL | INLCR | IGNCR)
|
||||||
|
|
||||||
// Set hardware flow control flag
|
// Set hardware flow control flag
|
||||||
#if os(Linux)
|
#if os(Linux)
|
||||||
if useHardwareFlowControl {
|
if useHardwareFlowControl {
|
||||||
@@ -371,6 +376,12 @@ extension SerialPort {
|
|||||||
throw PortError.mustBeOpen
|
throw PortError.mustBeOpen
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var s: stat = stat()
|
||||||
|
fstat(fileDescriptor, &s)
|
||||||
|
if s.st_nlink != 1 {
|
||||||
|
throw PortError.deviceNotConnected
|
||||||
|
}
|
||||||
|
|
||||||
let bytesRead = read(fileDescriptor, buffer, size)
|
let bytesRead = read(fileDescriptor, buffer, size)
|
||||||
return bytesRead
|
return bytesRead
|
||||||
}
|
}
|
||||||
@@ -378,7 +389,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)
|
||||||
@@ -417,13 +428,16 @@ 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 {
|
||||||
let bytesRead = try readBytes(into: buffer, size: 1)
|
let bytesRead = try readBytes(into: buffer, size: 1)
|
||||||
|
|
||||||
if bytesRead > 0 {
|
if bytesRead > 0 {
|
||||||
|
if ( buffer[0] > 127) {
|
||||||
|
throw PortError.unableToConvertByteToCharacter
|
||||||
|
}
|
||||||
let character = CChar(buffer[0])
|
let character = CChar(buffer[0])
|
||||||
|
|
||||||
if character == terminator {
|
if character == terminator {
|
||||||
@@ -446,22 +460,26 @@ extension SerialPort {
|
|||||||
return try readUntilChar(newlineChar)
|
return try readUntilChar(newlineChar)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func readChar() throws -> UnicodeScalar {
|
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 {
|
||||||
let character = UnicodeScalar(buffer[0])
|
return buffer[0]
|
||||||
return character
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func readChar() throws -> UnicodeScalar {
|
||||||
|
let byteRead = try readByte()
|
||||||
|
let character = UnicodeScalar(byteRead)
|
||||||
|
return character
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -483,7 +501,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)
|
||||||
|
|||||||
BIN
first-slide.png
Normal file
BIN
first-slide.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 311 KiB |
BIN
swift-serial-talk-slides.pptx
Normal file
BIN
swift-serial-talk-slides.pptx
Normal file
Binary file not shown.
Reference in New Issue
Block a user