Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6657120

Browse files
committedOct 2, 2019
[Fix] fixes issue where program would get stuck if trying to read data and there was actually no data sent by another XBee
1 parent a7376fd commit 6657120

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed
 

‎Examples/RxTxExample/Sources/RxTxExample/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let xbee = SwiftyXBee(for: .RaspberryPi3, serialConnection: serialConnection)
88
print("Receiving packet...")
99

1010
do {
11-
let readingPacket = try xbee.readRFDataPacket()
11+
let readingPacket = try xbee.readRFDataPacket(maxTimeout: 5) // Wait up to 5 seconds for available data
1212
print("Packet received: \(readingPacket.frameData.receivedData)")
1313
} catch let error {
1414
print("Error receiving packet: \(error)")

‎Sources/SwiftyXBee/Helpers/Constants.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import Foundation
99

1010
public enum Constant {
11+
public static let defaultReadDataTimeout: Double = 1
1112
public static let minimumRawDataLength = 4
1213
public static let startDelimiter: UInt8 = 0x7E
1314
public static let escapeByteXOR: UInt8 = 0x20

‎Sources/SwiftyXBee/Helpers/XBeeSerial.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
// Created by Samuel Cornejo on 7/13/19.
66
//
77

8+
import Foundation
89
import SwiftyGPIO
910

1011
enum XBeeSerialError: Error {
1112
case checksumFailure
13+
case noSerialDataAvailable
1214
}
1315

1416
public struct XBeeSerial {
@@ -24,12 +26,17 @@ public struct XBeeSerial {
2426

2527
/// Reads all the available bytes in the serial port.
2628
///
27-
/// - Parameter serial: The serial port to extract data from
29+
/// - Parameters:
30+
/// - serial: The serial port to extract data from
31+
/// - maxTimeout: The maximum time to wait for data availability
2832
/// - Returns: All the bytes extracted from the serial port
2933
/// - Throws: Serial port reading errors
30-
public mutating func readData(from serial: UARTInterface) throws -> [UInt8] {
31-
data = []
34+
public mutating func readData(from serial: UARTInterface, maxTimeout: TimeInterval) throws -> [UInt8] {
35+
let start = Date()
36+
while try !serial.hasAvailableData() && abs(start.timeIntervalSinceNow) < maxTimeout { }
37+
guard try serial.hasAvailableData() else { throw XBeeSerialError.noSerialDataAvailable }
3238

39+
data = []
3340
while try dataIsIncomplete() {
3441
let readData = serial.readData().map({ UInt8(bitPattern: $0) })
3542
data += readData

‎Sources/SwiftyXBee/SwiftyXBee.swift

+14-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import Darwin.C
1212
#endif
1313

14+
import Foundation
1415
import SwiftyGPIO
1516

1617
public class SwiftyXBee {
@@ -53,22 +54,24 @@ public class SwiftyXBee {
5354

5455
/// Reads and process an RF data packet.
5556
///
57+
/// - Parameter maxTimeout: The maximum time to wait before checking the serial port for data
5658
/// - Returns: A Receive Packet API Frame
5759
/// - Throws: Any error while reading the RF data packet
58-
public func readRFDataPacket() throws -> APIFrame<ZigBeeReceivePacketData> {
59-
let rawData = try readSerialData()
60+
public func readRFDataPacket(maxTimeout: TimeInterval = Constant.defaultReadDataTimeout) throws -> APIFrame<ZigBeeReceivePacketData> {
61+
let rawData = try readSerialData(maxTimeout: maxTimeout)
6062
let frameData = ZigBeeReceivePacketData(rawData: rawData)
6163
return try APIFrame(rawData: rawData, frameData: frameData)
6264
}
6365

6466
/// Reads the transmission status after issuing a Transmit Request API Frame.
6567
///
68+
/// - Parameter maxTimeout: The maximum time to wait before checking the serial port for data
6669
/// - Returns: A Transmit Status API Frame
6770
/// - Throws: Any error while reading the transmit status packet
6871
/// - Note: If delivery status is 0x00, the transmission was successfully delivered to the destination address.
6972
/// Otherwise, the number received in this byte will indicate the kind of issue that prevented the delivery.
70-
public func readTransmitStatus() throws -> APIFrame<ZigBeeTransmitStatusData> {
71-
let rawData = try readSerialData()
73+
public func readTransmitStatus(maxTimeout: TimeInterval = Constant.defaultReadDataTimeout) throws -> APIFrame<ZigBeeTransmitStatusData> {
74+
let rawData = try readSerialData(maxTimeout: maxTimeout)
7275
let frameData = ZigBeeTransmitStatusData(rawData: rawData)
7376
return try APIFrame(rawData: rawData, frameData: frameData)
7477
}
@@ -88,20 +91,22 @@ public class SwiftyXBee {
8891

8992
/// Reads and process an AT Command response
9093
///
94+
/// - Parameter maxTimeout: The maximum time to wait before checking the serial port for data
9195
/// - Returns: An AT Command Response Frame
9296
/// - Throws: Any error while reading the RF data packet
93-
public func readATCommandResponse() throws -> APIFrame<ATCommandResponseData> {
94-
let rawData = try readSerialData()
97+
public func readATCommandResponse(maxTimeout: TimeInterval = Constant.defaultReadDataTimeout) throws -> APIFrame<ATCommandResponseData> {
98+
let rawData = try readSerialData(maxTimeout: maxTimeout)
9599
let frameData = ATCommandResponseData(rawData: rawData)
96100
return try APIFrame(rawData: rawData, frameData: frameData)
97101
}
98102

99103
/// Reads the serial port.
100104
///
105+
/// - Parameter maxTimeout: The maximum time to wait before checking the serial port for data
101106
/// - Returns: All the available data in the serial port
102-
/// - Throws: Any errors while reading the serial port
103-
public func readSerialData() throws -> [UInt8] {
104-
return try serial.readData(from: uart)
107+
/// - Throws: Any errors while reading the serial port
108+
public func readSerialData(maxTimeout: TimeInterval = Constant.defaultReadDataTimeout) throws -> [UInt8] {
109+
return try serial.readData(from: uart, maxTimeout: maxTimeout)
105110
}
106111

107112
/// Writes data to the serial port.

0 commit comments

Comments
 (0)
Failed to load comments.