Skip to content
This repository was archived by the owner on Jan 27, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 30 additions & 43 deletions Examples/SwiftLinuxSerialTest/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -1,61 +1,48 @@
import Glibc
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 big brown fox jumps over the lazy dog 01234567890."
let testString: String = "The quick brown fox jumps over the lazy dog 01234567890."

let arguments = CommandLine.arguments

if(arguments.count < 2){
print("Need Serial Port name example /dev/ttyUSB0 as first argument");
exit(1)
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)

let serialHandler : SwiftLinuxSerial = SwiftLinuxSerial(serialPortName : portName)

let status = serialHandler.openPort(receive : true, transmit : true)

if(status.openSuccess){
print("Serial port " + portName + " opened successfully")
} else {
print("Serial port " + portName + " failed to open. You might need root permissions")
exit(1)
}

serialHandler.setPortSettings(receiveBaud : SwiftLinuxSerialBaud.BAUD_B9600,
transmitBaud : SwiftLinuxSerialBaud.BAUD_B9600,
charsToReadBeforeReturn : 1)

do {
try serialPort.openPort()
print("Serial port \(portName) opened successfully.")
defer {
serialPort.closePort()
}

let stringCharacterCount = testString.characters.count
serialPort.setSettings(receiveRate: .baud9600,
transmitRate: .baud9600,
minimumBytesToRead: 1)

print("Writing test string <" + testString + "> of " + String(stringCharacterCount) + " characters to serial port")
print("Writing test string <\(testString)> of \(testString.characters.count) characters to serial port")

var bytesWritten = serialHandler.writeStringToPortBlocking(stringToWrite : testString)
var bytesWritten = try serialPort.writeString(testString)

print("Sucessfully wrote " + String(bytesWritten) + " bytes")
print("Successfully wrote \(bytesWritten) bytes")
print("Waiting to receive what was written...")

print("Waiting to receive what was written...")
let stringReceived = try serialPort.readString(ofLength: bytesWritten)

let stringReceived = serialHandler.readStringFromPortBlocking(bytesToReadFor : 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)>")
}

serialHandler.closePort()

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)")
}









Loading