|
@@ -15,26 +15,19 @@ public class SerialPort {
|
|
|
self.path = path
|
|
self.path = path
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public func openPort() throws {
|
|
|
|
|
- try openPort(toReceive: true, andTransmit: true)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public func openPort(toReceive receive: Bool, andTransmit transmit: Bool) throws {
|
|
|
|
|
|
|
+ public func openPort(portMode: PortMode = .receiveAndTransmit) throws {
|
|
|
guard !path.isEmpty else { throw PortError.invalidPath }
|
|
guard !path.isEmpty else { throw PortError.invalidPath }
|
|
|
guard isOpen == false else { throw PortError.instanceAlreadyOpen }
|
|
guard isOpen == false else { throw PortError.instanceAlreadyOpen }
|
|
|
|
|
|
|
|
- guard receive || transmit else { throw PortError.mustReceiveOrTransmit }
|
|
|
|
|
-
|
|
|
|
|
- var readWriteParam : Int32
|
|
|
|
|
|
|
+ let readWriteParam: Int32
|
|
|
|
|
|
|
|
- if receive && transmit {
|
|
|
|
|
- readWriteParam = O_RDWR
|
|
|
|
|
- } else if receive {
|
|
|
|
|
|
|
+ switch portMode {
|
|
|
|
|
+ case .receive:
|
|
|
readWriteParam = O_RDONLY
|
|
readWriteParam = O_RDONLY
|
|
|
- } else if transmit {
|
|
|
|
|
|
|
+ case .transmit:
|
|
|
readWriteParam = O_WRONLY
|
|
readWriteParam = O_WRONLY
|
|
|
- } else {
|
|
|
|
|
- fatalError()
|
|
|
|
|
|
|
+ case .receiveAndTransmit:
|
|
|
|
|
+ readWriteParam = O_RDWR
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#if os(Linux)
|
|
#if os(Linux)
|
|
@@ -49,7 +42,7 @@ public class SerialPort {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
guard
|
|
guard
|
|
|
- receive,
|
|
|
|
|
|
|
+ portMode.receive,
|
|
|
let fileDescriptor
|
|
let fileDescriptor
|
|
|
else { return }
|
|
else { return }
|
|
|
let pollSource = DispatchSource.makeReadSource(fileDescriptor: fileDescriptor, queue: .global(qos: .default))
|
|
let pollSource = DispatchSource.makeReadSource(fileDescriptor: fileDescriptor, queue: .global(qos: .default))
|