From d69f2a8f049e3b0dd2f8b99a42e10f01947cfc65 Mon Sep 17 00:00:00 2001 From: Yeo Kheng Meng Date: Tue, 25 Oct 2016 22:23:11 +0800 Subject: [PATCH] ability to choose parity even or odd if enabled --- Sources/SwiftSerial.swift | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Sources/SwiftSerial.swift b/Sources/SwiftSerial.swift index a76c550..aded83e 100644 --- a/Sources/SwiftSerial.swift +++ b/Sources/SwiftSerial.swift @@ -183,6 +183,24 @@ public enum DataBitsSize { return tcflag_t(CS8) } } + +} + +public enum ParityType { + case none + case even + case odd + + var parityValue: tcflag_t { + switch self { + case .none: + return 0 + case .even: + return tcflag_t(PARENB) + case .odd: + return tcflag_t(PARENB | PARODD) + } + } } public enum PortError: Int32, Error { @@ -235,7 +253,7 @@ public class SerialPort { transmitRate: BaudRate, minimumBytesToRead: Int, timeout: Int = 0, /* 0 means wait indefinitely */ - enableParity: Bool = false, + parityType: ParityType = .none, sendTwoStopBits: Bool = false, /* 1 stop bit is the default */ dataBitsSize: DataBitsSize = .bits8, useHardwareFlowControl: Bool = false, @@ -255,12 +273,8 @@ public class SerialPort { cfsetispeed(&settings, receiveRate.speedValue) cfsetospeed(&settings, transmitRate.speedValue) - // Set parity enable flag - if enableParity { - settings.c_cflag |= ~tcflag_t(PARENB) - } else { - settings.c_cflag &= ~tcflag_t(PARENB) - } + // Enable parity (even/odd) if needed + settings.c_cflag |= parityType.parityValue // Set stop bit flag if sendTwoStopBits {