diff --git a/Examples/SwiftSerialBinary/Package.swift b/Examples/SwiftSerialBinary/Package.swift new file mode 100644 index 0000000..630bb6d --- /dev/null +++ b/Examples/SwiftSerialBinary/Package.swift @@ -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" + ), + ] +) diff --git a/Examples/SwiftSerialBinary/Sources/main.swift b/Examples/SwiftSerialBinary/Sources/main.swift new file mode 100644 index 0000000..df2b418 --- /dev/null +++ b/Examples/SwiftSerialBinary/Sources/main.swift @@ -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)") +} diff --git a/Examples/SwiftSerialBinary/gitignore b/Examples/SwiftSerialBinary/gitignore new file mode 100644 index 0000000..8b16c47 --- /dev/null +++ b/Examples/SwiftSerialBinary/gitignore @@ -0,0 +1,4 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj diff --git a/Examples/SwiftSerialExample/Package.swift b/Examples/SwiftSerialExample/Package.swift index 1e03659..3ab18d5 100644 --- a/Examples/SwiftSerialExample/Package.swift +++ b/Examples/SwiftSerialExample/Package.swift @@ -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. diff --git a/Examples/SwiftSerialIM/Package.swift b/Examples/SwiftSerialIM/Package.swift index df153f3..1428820 100644 --- a/Examples/SwiftSerialIM/Package.swift +++ b/Examples/SwiftSerialIM/Package.swift @@ -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.