Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.

Commit

Permalink
add extra test for binary
Browse files Browse the repository at this point in the history
  • Loading branch information
yeokm1 committed Jan 26, 2020
1 parent 46a834e commit 91a7ea3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Examples/SwiftSerialBinary/Package.swift
Original file line number Diff line number Diff line change
@@ -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 changes: 60 additions & 0 deletions Examples/SwiftSerialBinary/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions Examples/SwiftSerialBinary/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
2 changes: 1 addition & 1 deletion Examples/SwiftSerialExample/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "SwiftSerialExample",
dependencies: [
.package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.1")
.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.
Expand Down
2 changes: 1 addition & 1 deletion Examples/SwiftSerialIM/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "SwiftSerialIM",
dependencies: [
.package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.1")
.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.
Expand Down

0 comments on commit 91a7ea3

Please sign in to comment.