This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters