Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86625ee414 | ||
|
|
29bbe8505f | ||
|
|
005183c282 | ||
|
|
ddc06f53ef | ||
|
|
1027251723 | ||
|
|
6db41b985c | ||
|
|
62d6742987 | ||
|
|
66c27258da | ||
|
|
10b91eb8a2 | ||
|
|
581ebc7e10 | ||
|
|
db7a3b79a2 | ||
|
|
1b9591e07a | ||
|
|
af1fa134cc | ||
|
|
812a55a381 | ||
|
|
41ddf0247a | ||
|
|
f3c3a214e3 | ||
|
|
5536368ba7 | ||
|
|
e2063420e9 | ||
|
|
cfaf24357c | ||
|
|
543e7d39e0 | ||
|
|
f9981b1cec | ||
|
|
d36d0132a8 | ||
|
|
b29775dfbe | ||
|
|
8a8dfcb190 |
@@ -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.1")
|
||||||
]
|
],
|
||||||
|
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.1")
|
||||||
]
|
],
|
||||||
|
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"
|
||||||
|
),
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -113,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"
|
||||||
|
),
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
36
README.md
36
README.md
@@ -1,5 +1,5 @@
|
|||||||
# 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).
|
||||||
|
|
||||||
@@ -9,13 +9,26 @@ This library is an improvement over my previous now deprecated library [SwiftLin
|
|||||||
<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://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>
|
<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>
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
[](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).
|
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).
|
||||||
|
|
||||||
### Install Swift 3 on Ubuntu on x86-based machines
|
### Install Swift 3 on Ubuntu on x86-based machines
|
||||||
|
|
||||||
@@ -50,16 +63,16 @@ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6
|
|||||||
|
|
||||||
cd ~
|
cd ~
|
||||||
#Replace the link below with the latest version
|
#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
|
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
|
mkdir swift-3.0
|
||||||
cd swift-3.0 && tar -xzf ../swift-3.0-2016-10-13-RPi23-ubuntu16.04.tar.gz
|
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
|
#This command can be added to your bash profile so Swift will be in your PATH after a reboot
|
||||||
nano ~/.profile
|
nano ~/.profile
|
||||||
export PATH=$HOME/swift-3.0/usr/bin:$PATH
|
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
|
||||||
|
|
||||||
@@ -104,7 +117,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.1"),
|
||||||
...
|
...
|
||||||
]
|
]
|
||||||
...
|
...
|
||||||
@@ -122,7 +135,7 @@ Then run `swift build` to download the dependencies and compile your project. Yo
|
|||||||
```swift
|
```swift
|
||||||
let serialPort: SerialPort = SerialPort(path: portName)
|
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 a 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.
|
||||||
|
|
||||||
@@ -139,7 +152,7 @@ Opening the port without any parameters will set the port to receive and transmi
|
|||||||
```swift
|
```swift
|
||||||
serialPort.setSettings(receiveRate: .baud9600, transmitRate: .baud9600, minimumBytesToRead: 1)
|
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.
|
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.
|
`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.
|
||||||
|
|
||||||
@@ -188,10 +201,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
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
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