Skip to content
This repository was archived by the owner on Jan 27, 2024. It is now read-only.

Conversation

@jayjun
Copy link

@jayjun jayjun commented Oct 23, 2016

Changes

  1. Removed import Glibc because it's redundant with import Foundation.

  2. Removed SwiftLinuxSerial- prefix from type names because Swift identifiers are already namespaced by module name.

    // Before
    enum SwiftLinuxSerialBaud
    enum SwiftLinuxSerialDataBit
    
    // After
    enum BaudRate
    enum DataBitsSize
  3. Changed enumerations from SCREAMING_CASE to idiomatic camelCase convention.

  4. Convert BaudRate to speed_t via a speedValue property.

  5. Convert DataBitsSize to tcflag_t via a flagValue property.

  6. Renamed SwiftLinuxSerial class to SerialPort. Since macOS support is added, consider renaming package name (see below).

  7. Use optional fileDescriptor instead of -1 to represent nil/error state.

  8. Replaced conditionals checking for magic numbers with guard clauses.

  9. Removed openPort()'s parameter defaults and added convenience function that calls openPort(toReceive: true, andTransmit: true) instead. Previously, API wasn't intuitive because to open a read-only serial port, one calls openPort(transmit: false).

  10. Changed setPortSettings() signature to be Swiftier.

    // Before
    func setPortSettings(receiveBaud: SwiftLinuxSerialBaud,
                         transmitBaud: SwiftLinuxSerialBaud,
                         charsToReadBeforeReturn: UInt8,
                         parity: Bool = false,
                         dataBits: SwiftLinuxSerialDataBit = SwiftLinuxSerialDataBit.DATA_BIT_8,
                         stopBit: SwiftLinuxSerialStopBit = SwiftLinuxSerialStopBit.STOP_BIT_1,
                         hardwareFlowControl: Bool = false,
                         softwareFlowControl: Bool = false,
                         outputProcessing: Bool = false,
                         minimumTimeToWaitBeforeReturn: UInt8 = 0)
    
    // After
    func setSettings(receiveRate: BaudRate,
                     transmitRate: BaudRate,
                     minimumBytesToRead: Int,
                     timeout: Int = 0,
                     enableParity: Bool = false,
                     sendTwoStopBits: Bool = false,
                     dataBitsSize: DataBitsSize = .bits8,
                     useHardwareFlowControl: Bool = false,
                     useSoftwareFlowControl: Bool = false,
                     processOutput: Bool = false)
  11. Prefer typealiases (e.g. tcflag_t) over regular integer types, and removed redundant casting.

    // Before
    srSettings.c_lflag &= ~(UInt32(ICANON) | UInt32(ECHO) | UInt32(ECHOE) | UInt32(ISIG))
    
    // After
    settings.c_lflag &= ~tcflag_t(ICANON | ECHO | ECHOE | ISIG)
  12. Use platform-specific named tuples for c_cc tuple element access.

    // Before
    srSettings.c_cc.6 = charsToReadBeforeReturn
    
    // After
    var specialCharacters: SpecialCharactersTuple = (0, 0, ... 0)
    specialCharacters.VMIN = cc_t(minimumBytesToRead)
    settings.c_cc = specialCharacters
  13. Moved reading and writing methods into extensions for clarity.

  14. Throw errors from reading and writing methods instead of returning magic numbers.

  15. Removed -ToPortBlocking suffixes from method names. The absence of a trailing closure parameter implies blocking call.

    // Before
    func writeDataToPortBlocking(dataToWrite : Data) -> Int
    
    // After
    func writeData(_ data: Data) throws -> Int
    
    // Possible asynchronous version
    func writeData(_ data: Data, completion: (Int) -> Void) throws
  16. Changed readTillCharacterBlocking(characterRep: UnicodeScalar) to readUntilChar(_: CChar) to better reflect underlying algorithm, i.e. byte-size CChars are read in sequence, not UnicodeScalars.

  17. Improved readUntilChar(_:) algorithm to append to Data instead of String, thus supporting UTF8 strings with multibyte characters.

  18. Updated SwiftLinuxSerialTest to use new APIs.

  19. Use string interpolation instead of concatenation in print() statements.

    // Before
    print("Successfully wrote " + String(bytesWritten) + " bytes")
    
    // After
    print("Successfully wrote \(bytesWritten) bytes")
  20. Changed indentation from tabs to 4 spaces. 83% of Swift repos use 4 spaces.

Additions

  1. Supported macOS (i.e. import Darwin).
  2. Added throwable PortError type. Far more idiomatic than inferring errors from returned tuples or magic numbers.

Suggestions

  1. Rename package name if cross-platform support will be maintained.
  2. Add proper unit tests (using XCTest under Tests directory), rather than creating a separate SwiftLinuxSerialTest package.

@yeokm1
Copy link
Owner

yeokm1 commented Oct 26, 2016

Your improvements have been used in my newer SwiftSerial repo which has a newer name to make it less platform specific. This repo is now deprecated though I'll leave it for historical reasons.

Thanks for the effort to make my code Swiftier and multi-platform.

@yeokm1 yeokm1 closed this Oct 26, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants