swift serial example

This commit is contained in:
Yeo Kheng Meng
2016-10-25 21:42:04 +08:00
parent ecf296c107
commit 50e274f5a9
3 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
import PackageDescription
let package = Package(
name: "SwiftSerialExample",
dependencies: [
.Package(url: "https://github.com/yeokm1/SwiftSerial.git", majorVersion: 0)
]
)

View File

@@ -0,0 +1,48 @@
import Foundation
import SwiftLinuxSerial
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 arguments = CommandLine.arguments
guard arguments.count >= 2 else {
print("Need serial port name, e.g. /dev/ttyUSB0 as the first argument.")
exit(1)
}
let portName = arguments[1]
let serialPort: SerialPort = SerialPort(name: portName)
do {
try serialPort.openPort()
print("Serial port \(portName) opened successfully.")
defer {
serialPort.closePort()
}
serialPort.setSettings(receiveRate: .baud9600,
transmitRate: .baud9600,
minimumBytesToRead: 1)
print("Writing test string <\(testString)> of \(testString.characters.count) characters to serial port")
var bytesWritten = try serialPort.writeString(testString)
print("Successfully wrote \(bytesWritten) bytes")
print("Waiting to receive what was written...")
let stringReceived = try serialPort.readString(ofLength: bytesWritten)
if testString == stringReceived {
print("Received string is the same as transmitted string. Test successful!")
} else {
print("Uh oh! Received string is not the same as what was transmitted. This was what we received,")
print("<\(stringReceived)>")
}
} catch PortError.failedToOpen {
print("Serial port \(portName) failed to open. You might need root permissions.")
} catch {
print("Error: \(error)")
}

View File

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