A Science Project: Swift Serial Chat
iOS Dev Scout (23 Nov 2016)
By: Yeo Kheng Meng (yeokm1@gmail.com)
https://github.com/yeokm1/SwiftSerial
1
Modern networking options for apps
2
Live Swift app Demo
3
Swift for IoT @ iOS Conf 2016
4
Swift Linux Serial Port library
• https://github.com/yeokm1/SwiftLinuxSerial
• Deprecated
5
Updated Swift Serial Port library
• https://github.com/yeokm1/SwiftSerial
• Mac and Linux compatible
• Swiftier: No more C in Swift!
• Thanks to Jay Jun’s pull request
• https://github.com/yeokm1/SwiftLinuxSerial/pull/1
6
Subh: Why not give a talk about your Swift Serial library?
7
Agenda
1. What is a Serial Port?
2. My hardware setup
3. Walkthrough of original Swift Linux Serial
4. Learning points of Jay Jun’s pull request
5. My cross-platform command-line Swift chat app
8
What is a Serial Port?
• Hardware complying to RS-232 standard
• Introduced in 1962 by Electronic Industries Alliance
• Standardised as RS-232C in 1969
• Vintage consumer usage
• Modems
• Mouse
• Point-to-point network
• Obsolete by 2000s
• Modern Usage
• Medical Devices
• Scientific equipment
• Industrial Control Systems
• Uninterruptible Power Supply
• Simpler/Cheaper to implement and longer distance (300m) compared to USB
9
Male DE-9 connector
Traditional DB-25 connector
RS232 Hardware Pinout
10
Pins Name Function
1 Data Carrier Detect (DCD) Device is connected
2 Receive (RD) Receive data
3 Transmit (TD) Transmit data
4 Data Terminal Ready (DTR) Ready to start a session
5 Ground Zero reference voltage for transmission
6 Ringing Indicator (RI) Phone is ringing!
7 Clear to Send (CTS) Hardware flow control (You can send data now!)
8 Request to Send (RTS) Hardware flow control (Can I send data to you?)
9 Data Set Ready (DSR) Ready to receive commands or data
http://www.db9-pinout.com/
RS232 Communication Parameters
• Baud Rate (Speed: bits/s)
• Linux max = 4 million bits/s
• Mac max = 230400 bits/s
• Data bits
• Indicates frame size
• Parity
• Error detection
• Even/Odd/None
• Makes number of 1 bits count even/odd
• Stop bits
• Number of bits to indicates end of frame
• Hardware flow control
• CTS/RTS Pins
• Software flow control
• Xon/Xoff = Transmit on /Transmit off
11https://learn.sparkfun.com/tutorials/serial-communication/rules-of-serial
Hardware setup
12
Thinkpad T400 (2009)
• Linux Mint 18
• FTDI FT232 USB-Serial adapter
Macbook Pro Retina 15 (Late 2013)
• macOS Sierra
• Prolific PL-2303 USB-serial adapter
Null-modem cable
Simple null modem cable
• GND <-> GND
• TX -> RX
• RX <- TX
13http://digital.ni.com/public.nsf/allkb/1EE0DD8AF67922FA86256F720071DECF
Original Swift Linux Serial code
• https://github.com/yeokm1/SwiftLinuxSerial/blob/master/Sources/SwiftLinuxSerial.swift
• Enums for
• Baud rate
• Data bits
• Stop bits
• Termios configuration structure
• c_cc C fixed array as Swift tuple
• Reading and Writing bytes to port
• UnsafeMutable Pointers required for C functions
14
Improved Swift Serial code by Jay Jun
• https://github.com/yeokm1/SwiftSerial/blob/master/Sources/SwiftSerial.swift
• Cross platform Mac and Linux
• Preprocessor directives:
• C directive is line level
• Swift directive is statement level
• Baud rates, port opening
• Swiftier
• Guard statements
• typealias c_cc objects of termios struct, synonyms for pre-existing types
15
My Swift chat app
• Swift 3 command line desktop app
• Uses Swift Serial library
• Cross platform: Mac and Linux
• Live chat between 2 machines
• https://github.com/yeokm1/SwiftSerial/blob/master/Examples/SwiftSerialIM/Sources/main.swift
16
Swift chat app interesting details
• openPort()
• Using Swift Serial
• prepareStdin()
• Don’t echo immediately to screen when user is typing
• Background thread reads from port
• Mac: Grand Central Dispatch
• Linux: Posix threads (pthreads) as GCD (libdispatch) does not work with Swift Package Manager on Linux
• https://bugs.swift.org/browse/SR-397
• Main thread reads from keyboard
• getchar() instead of readline() to read without pressing ”Enter”
• https://developer.apple.com/reference/swift/1641199-readline
• Turn off output buffering
• setbuf(stdout, nil)
• If not multiple threads will have problems printing to screen
• Print to screen without newline
• print(“string”, terminator:"")
• Default print prints the newline
17
What is a “Science Project”?
• By Raymond Chen
• Principal Software Engineer, Microsoft
• Joined 1992
• Runs a blog “Old New Thing”
• Mar 2013 post: https://blogs.msdn.microsoft.com/oldnewthing/20130319-00/?p=4913
18
Raymond Chen image from: http://www.dailytech.com/Microsoft+Exec+Reveals+Steve+Ballmer+Created+Original+Blue+Screen+of+Death+Message/article36512.htm
A Science Project by Raymond Chen
1. A feature that is really cool and challenging from a technological
standpoint but is overkill for the user-scenario:
• Networked bouncing ball screen saver for computers in a building
2. Requires hardware few people have:
• Tablet with 2 touch-enabled screens
3. Trying to solve a problem that nobody really considers to be a
problem. You're doing it just for the Gee Whiz factor.
19Taken from: https://blogs.msdn.microsoft.com/oldnewthing/20130319-00/?p=4913
Any questions for a world without these?
20

A Science Project: Swift Serial Chat

  • 1.
    A Science Project:Swift Serial Chat iOS Dev Scout (23 Nov 2016) By: Yeo Kheng Meng (yeokm1@gmail.com) https://github.com/yeokm1/SwiftSerial 1
  • 2.
  • 3.
  • 4.
    Swift for IoT@ iOS Conf 2016 4
  • 5.
    Swift Linux SerialPort library • https://github.com/yeokm1/SwiftLinuxSerial • Deprecated 5
  • 6.
    Updated Swift SerialPort library • https://github.com/yeokm1/SwiftSerial • Mac and Linux compatible • Swiftier: No more C in Swift! • Thanks to Jay Jun’s pull request • https://github.com/yeokm1/SwiftLinuxSerial/pull/1 6
  • 7.
    Subh: Why notgive a talk about your Swift Serial library? 7
  • 8.
    Agenda 1. What isa Serial Port? 2. My hardware setup 3. Walkthrough of original Swift Linux Serial 4. Learning points of Jay Jun’s pull request 5. My cross-platform command-line Swift chat app 8
  • 9.
    What is aSerial Port? • Hardware complying to RS-232 standard • Introduced in 1962 by Electronic Industries Alliance • Standardised as RS-232C in 1969 • Vintage consumer usage • Modems • Mouse • Point-to-point network • Obsolete by 2000s • Modern Usage • Medical Devices • Scientific equipment • Industrial Control Systems • Uninterruptible Power Supply • Simpler/Cheaper to implement and longer distance (300m) compared to USB 9 Male DE-9 connector Traditional DB-25 connector
  • 10.
    RS232 Hardware Pinout 10 PinsName Function 1 Data Carrier Detect (DCD) Device is connected 2 Receive (RD) Receive data 3 Transmit (TD) Transmit data 4 Data Terminal Ready (DTR) Ready to start a session 5 Ground Zero reference voltage for transmission 6 Ringing Indicator (RI) Phone is ringing! 7 Clear to Send (CTS) Hardware flow control (You can send data now!) 8 Request to Send (RTS) Hardware flow control (Can I send data to you?) 9 Data Set Ready (DSR) Ready to receive commands or data http://www.db9-pinout.com/
  • 11.
    RS232 Communication Parameters •Baud Rate (Speed: bits/s) • Linux max = 4 million bits/s • Mac max = 230400 bits/s • Data bits • Indicates frame size • Parity • Error detection • Even/Odd/None • Makes number of 1 bits count even/odd • Stop bits • Number of bits to indicates end of frame • Hardware flow control • CTS/RTS Pins • Software flow control • Xon/Xoff = Transmit on /Transmit off 11https://learn.sparkfun.com/tutorials/serial-communication/rules-of-serial
  • 12.
    Hardware setup 12 Thinkpad T400(2009) • Linux Mint 18 • FTDI FT232 USB-Serial adapter Macbook Pro Retina 15 (Late 2013) • macOS Sierra • Prolific PL-2303 USB-serial adapter Null-modem cable
  • 13.
    Simple null modemcable • GND <-> GND • TX -> RX • RX <- TX 13http://digital.ni.com/public.nsf/allkb/1EE0DD8AF67922FA86256F720071DECF
  • 14.
    Original Swift LinuxSerial code • https://github.com/yeokm1/SwiftLinuxSerial/blob/master/Sources/SwiftLinuxSerial.swift • Enums for • Baud rate • Data bits • Stop bits • Termios configuration structure • c_cc C fixed array as Swift tuple • Reading and Writing bytes to port • UnsafeMutable Pointers required for C functions 14
  • 15.
    Improved Swift Serialcode by Jay Jun • https://github.com/yeokm1/SwiftSerial/blob/master/Sources/SwiftSerial.swift • Cross platform Mac and Linux • Preprocessor directives: • C directive is line level • Swift directive is statement level • Baud rates, port opening • Swiftier • Guard statements • typealias c_cc objects of termios struct, synonyms for pre-existing types 15
  • 16.
    My Swift chatapp • Swift 3 command line desktop app • Uses Swift Serial library • Cross platform: Mac and Linux • Live chat between 2 machines • https://github.com/yeokm1/SwiftSerial/blob/master/Examples/SwiftSerialIM/Sources/main.swift 16
  • 17.
    Swift chat appinteresting details • openPort() • Using Swift Serial • prepareStdin() • Don’t echo immediately to screen when user is typing • Background thread reads from port • Mac: Grand Central Dispatch • Linux: Posix threads (pthreads) as GCD (libdispatch) does not work with Swift Package Manager on Linux • https://bugs.swift.org/browse/SR-397 • Main thread reads from keyboard • getchar() instead of readline() to read without pressing ”Enter” • https://developer.apple.com/reference/swift/1641199-readline • Turn off output buffering • setbuf(stdout, nil) • If not multiple threads will have problems printing to screen • Print to screen without newline • print(“string”, terminator:"") • Default print prints the newline 17
  • 18.
    What is a“Science Project”? • By Raymond Chen • Principal Software Engineer, Microsoft • Joined 1992 • Runs a blog “Old New Thing” • Mar 2013 post: https://blogs.msdn.microsoft.com/oldnewthing/20130319-00/?p=4913 18 Raymond Chen image from: http://www.dailytech.com/Microsoft+Exec+Reveals+Steve+Ballmer+Created+Original+Blue+Screen+of+Death+Message/article36512.htm
  • 19.
    A Science Projectby Raymond Chen 1. A feature that is really cool and challenging from a technological standpoint but is overkill for the user-scenario: • Networked bouncing ball screen saver for computers in a building 2. Requires hardware few people have: • Tablet with 2 touch-enabled screens 3. Trying to solve a problem that nobody really considers to be a problem. You're doing it just for the Gee Whiz factor. 19Taken from: https://blogs.msdn.microsoft.com/oldnewthing/20130319-00/?p=4913
  • 20.
    Any questions fora world without these? 20