Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

of

A Science Project: Swift Serial Chat Slide 1 A Science Project: Swift Serial Chat Slide 2 A Science Project: Swift Serial Chat Slide 3 A Science Project: Swift Serial Chat Slide 4 A Science Project: Swift Serial Chat Slide 5 A Science Project: Swift Serial Chat Slide 6 A Science Project: Swift Serial Chat Slide 7 A Science Project: Swift Serial Chat Slide 8 A Science Project: Swift Serial Chat Slide 9 A Science Project: Swift Serial Chat Slide 10 A Science Project: Swift Serial Chat Slide 11 A Science Project: Swift Serial Chat Slide 12 A Science Project: Swift Serial Chat Slide 13 A Science Project: Swift Serial Chat Slide 14 A Science Project: Swift Serial Chat Slide 15 A Science Project: Swift Serial Chat Slide 16 A Science Project: Swift Serial Chat Slide 17 A Science Project: Swift Serial Chat Slide 18 A Science Project: Swift Serial Chat Slide 19 A Science Project: Swift Serial Chat Slide 20
Upcoming SlideShare
Building a chat app with windows azure mobile services
Next
Download to read offline and view in fullscreen.

1 Like

Share

Download to read offline

A Science Project: Swift Serial Chat

Download to read offline

A talk I gave at iOS Dev Scout on 23 Nov 2016 about a desktop Swift chat app I wrote that uses the Serial Port. It uses the Swift Serial library which I also wrote.

Code for both library and the example app can be found here: https://github.com/yeokm1/SwiftSerial

Video of my talk can be found here: https://engineers.sg/v/1275

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

A Science Project: Swift Serial Chat

  1. 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. 2. Modern networking options for apps 2
  3. 3. Live Swift app Demo 3
  4. 4. Swift for IoT @ iOS Conf 2016 4
  5. 5. Swift Linux Serial Port library • https://github.com/yeokm1/SwiftLinuxSerial • Deprecated 5
  6. 6. 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
  7. 7. Subh: Why not give a talk about your Swift Serial library? 7
  8. 8. 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
  9. 9. 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
  10. 10. 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/
  11. 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. 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. 13. Simple null modem cable • GND <-> GND • TX -> RX • RX <- TX 13http://digital.ni.com/public.nsf/allkb/1EE0DD8AF67922FA86256F720071DECF
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 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. 19. 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
  20. 20. Any questions for a world without these? 20
  • williammdavis

    Dec. 23, 2016

A talk I gave at iOS Dev Scout on 23 Nov 2016 about a desktop Swift chat app I wrote that uses the Serial Port. It uses the Swift Serial library which I also wrote. Code for both library and the example app can be found here: https://github.com/yeokm1/SwiftSerial Video of my talk can be found here: https://engineers.sg/v/1275

Views

Total views

1,397

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

1

Shares

0

Comments

0

Likes

1

×