add extra test for binary
This commit is contained in:
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
|
||||||
@@ -6,7 +6,7 @@ import PackageDescription
|
|||||||
let package = Package(
|
let package = Package(
|
||||||
name: "SwiftSerialExample",
|
name: "SwiftSerialExample",
|
||||||
dependencies: [
|
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: [
|
||||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import PackageDescription
|
|||||||
let package = Package(
|
let package = Package(
|
||||||
name: "SwiftSerialIM",
|
name: "SwiftSerialIM",
|
||||||
dependencies: [
|
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: [
|
||||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||||
|
|||||||
Reference in New Issue
Block a user