SlideShare a Scribd company logo
1 of 20
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

More Related Content

What's hot

Emanuele Faranda - Creating network overlays with IoT devices using N2N
Emanuele Faranda - Creating network overlays with IoT devices using N2NEmanuele Faranda - Creating network overlays with IoT devices using N2N
Emanuele Faranda - Creating network overlays with IoT devices using N2N
linuxlab_conf
 
Git Money
Git MoneyGit Money
Git Money
Tim N
 
BUD17-405: Building a reference IoT product with Zephyr
BUD17-405: Building a reference IoT product with Zephyr BUD17-405: Building a reference IoT product with Zephyr
BUD17-405: Building a reference IoT product with Zephyr
Linaro
 
Optimizing the Design and Implementation of KVM/ARM - SFO17-403
Optimizing the Design and Implementation of KVM/ARM - SFO17-403Optimizing the Design and Implementation of KVM/ARM - SFO17-403
Optimizing the Design and Implementation of KVM/ARM - SFO17-403
Linaro
 
High Performance Computing and Open Source & Linux Technical Excellence Sympo...
High Performance Computing and Open Source & Linux Technical Excellence Sympo...High Performance Computing and Open Source & Linux Technical Excellence Sympo...
High Performance Computing and Open Source & Linux Technical Excellence Sympo...
Gonéri Le Bouder
 
TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...
TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...
TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...
Linaro
 

What's hot (20)

Emanuele Faranda - Creating network overlays with IoT devices using N2N
Emanuele Faranda - Creating network overlays with IoT devices using N2NEmanuele Faranda - Creating network overlays with IoT devices using N2N
Emanuele Faranda - Creating network overlays with IoT devices using N2N
 
Git Money
Git MoneyGit Money
Git Money
 
MOVED: RDK/WPE Port on DB410C - SFO17-206
MOVED: RDK/WPE Port on DB410C - SFO17-206MOVED: RDK/WPE Port on DB410C - SFO17-206
MOVED: RDK/WPE Port on DB410C - SFO17-206
 
Micro control idsecconf2010
Micro control idsecconf2010Micro control idsecconf2010
Micro control idsecconf2010
 
BUD17-405: Building a reference IoT product with Zephyr
BUD17-405: Building a reference IoT product with Zephyr BUD17-405: Building a reference IoT product with Zephyr
BUD17-405: Building a reference IoT product with Zephyr
 
Device inspection to remote root
Device inspection to remote rootDevice inspection to remote root
Device inspection to remote root
 
CI For Embedded Software - Lucas Jenss & Folker Bernitt
CI For Embedded Software - Lucas Jenss & Folker BernittCI For Embedded Software - Lucas Jenss & Folker Bernitt
CI For Embedded Software - Lucas Jenss & Folker Bernitt
 
BUD17-104: Scripting Languages in IoT: Challenges and Approaches
BUD17-104: Scripting Languages in IoT: Challenges and ApproachesBUD17-104: Scripting Languages in IoT: Challenges and Approaches
BUD17-104: Scripting Languages in IoT: Challenges and Approaches
 
Optimizing the Design and Implementation of KVM/ARM - SFO17-403
Optimizing the Design and Implementation of KVM/ARM - SFO17-403Optimizing the Design and Implementation of KVM/ARM - SFO17-403
Optimizing the Design and Implementation of KVM/ARM - SFO17-403
 
ifwt remote (sydney ruxmon edition)
ifwt remote (sydney ruxmon edition)ifwt remote (sydney ruxmon edition)
ifwt remote (sydney ruxmon edition)
 
Michele Dionisio & Pietro Lorefice - Developing and testing a device driver w...
Michele Dionisio & Pietro Lorefice - Developing and testing a device driver w...Michele Dionisio & Pietro Lorefice - Developing and testing a device driver w...
Michele Dionisio & Pietro Lorefice - Developing and testing a device driver w...
 
High Performance Computing and Open Source & Linux Technical Excellence Sympo...
High Performance Computing and Open Source & Linux Technical Excellence Sympo...High Performance Computing and Open Source & Linux Technical Excellence Sympo...
High Performance Computing and Open Source & Linux Technical Excellence Sympo...
 
Bio routing - DKNOG9
Bio routing  - DKNOG9Bio routing  - DKNOG9
Bio routing - DKNOG9
 
Bus Pirate Workshop Ruxcon Hardware Hacking 2017
Bus Pirate Workshop Ruxcon Hardware Hacking 2017Bus Pirate Workshop Ruxcon Hardware Hacking 2017
Bus Pirate Workshop Ruxcon Hardware Hacking 2017
 
BSD Sockets API in Zephyr RTOS - SFO17-108
BSD Sockets API in Zephyr RTOS - SFO17-108BSD Sockets API in Zephyr RTOS - SFO17-108
BSD Sockets API in Zephyr RTOS - SFO17-108
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
 
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
 
LAS16-211: Using LAVA V2 for advanced KVM testing
LAS16-211: Using LAVA V2 for advanced KVM testingLAS16-211: Using LAVA V2 for advanced KVM testing
LAS16-211: Using LAVA V2 for advanced KVM testing
 
TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...
TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...
TSC Sponsored BoF: Can Linux and Automotive Functional Safety Mix ? Take 2: T...
 
Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4Project Basecamp: News From Camp 4
Project Basecamp: News From Camp 4
 

Viewers also liked

Viewers also liked (6)

Building a chat app with windows azure mobile services
Building a chat app with windows azure mobile servicesBuilding a chat app with windows azure mobile services
Building a chat app with windows azure mobile services
 
A Science Project: Building a sound card based on the Covox Speech Thing
A Science Project: Building a sound card based on the Covox Speech ThingA Science Project: Building a sound card based on the Covox Speech Thing
A Science Project: Building a sound card based on the Covox Speech Thing
 
Windows 3.1 (WFW) on vintage and modern hardware
Windows 3.1 (WFW) on vintage and modern hardwareWindows 3.1 (WFW) on vintage and modern hardware
Windows 3.1 (WFW) on vintage and modern hardware
 
PCB Business Card
PCB Business CardPCB Business Card
PCB Business Card
 
Raspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issuesRaspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issues
 
Repair Kopitiam Specialty Electrical Equipment
Repair Kopitiam Specialty Electrical EquipmentRepair Kopitiam Specialty Electrical Equipment
Repair Kopitiam Specialty Electrical Equipment
 

Similar to A Science Project: Swift Serial Chat

04 android
04 android04 android
04 android
guru472
 
Hands on Java8 and RaspberryPi
Hands on Java8 and RaspberryPiHands on Java8 and RaspberryPi
Hands on Java8 and RaspberryPi
Pance Cavkovski
 
Rapid IoT Prototyping with Tizen on Raspberry Pi
Rapid IoT Prototyping with Tizen on Raspberry PiRapid IoT Prototyping with Tizen on Raspberry Pi
Rapid IoT Prototyping with Tizen on Raspberry Pi
Leon Anavi
 
Serving Deep Learning Models At Scale With RedisAI: Luca Antiga
Serving Deep Learning Models At Scale With RedisAI: Luca AntigaServing Deep Learning Models At Scale With RedisAI: Luca Antiga
Serving Deep Learning Models At Scale With RedisAI: Luca Antiga
Redis Labs
 

Similar to A Science Project: Swift Serial Chat (20)

OSCON: System software goes weird
OSCON: System software goes weirdOSCON: System software goes weird
OSCON: System software goes weird
 
04 android
04 android04 android
04 android
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
 
ZeroMQ
ZeroMQZeroMQ
ZeroMQ
 
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
 
Open Source Swift Under the Hood
Open Source Swift Under the HoodOpen Source Swift Under the Hood
Open Source Swift Under the Hood
 
Tracking the International Space Station with Commodore Computers
Tracking the International Space Station with Commodore ComputersTracking the International Space Station with Commodore Computers
Tracking the International Space Station with Commodore Computers
 
Advanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCONAdvanced SOHO Router Exploitation XCON
Advanced SOHO Router Exploitation XCON
 
Rapid IoT prototyping with mruby
Rapid IoT prototyping with mrubyRapid IoT prototyping with mruby
Rapid IoT prototyping with mruby
 
Ham radio-without-a-radio
Ham radio-without-a-radioHam radio-without-a-radio
Ham radio-without-a-radio
 
tizen-oshw-tds14sh
tizen-oshw-tds14shtizen-oshw-tds14sh
tizen-oshw-tds14sh
 
How to port Tizen:Common to open source hardware devices?
How to port Tizen:Common to open source hardware devices? How to port Tizen:Common to open source hardware devices?
How to port Tizen:Common to open source hardware devices?
 
Dragon board 410c workshop - slideshow
Dragon board 410c workshop - slideshowDragon board 410c workshop - slideshow
Dragon board 410c workshop - slideshow
 
Hands on Java8 and RaspberryPi
Hands on Java8 and RaspberryPiHands on Java8 and RaspberryPi
Hands on Java8 and RaspberryPi
 
Embedded Erlang, Nerves, and SumoBots
Embedded Erlang, Nerves, and SumoBotsEmbedded Erlang, Nerves, and SumoBots
Embedded Erlang, Nerves, and SumoBots
 
How we use Twisted in Launchpad
How we use Twisted in LaunchpadHow we use Twisted in Launchpad
How we use Twisted in Launchpad
 
Rapid IoT Prototyping with Tizen on Raspberry Pi
Rapid IoT Prototyping with Tizen on Raspberry PiRapid IoT Prototyping with Tizen on Raspberry Pi
Rapid IoT Prototyping with Tizen on Raspberry Pi
 
Network Stack in Userspace (NUSE)
Network Stack in Userspace (NUSE)Network Stack in Userspace (NUSE)
Network Stack in Userspace (NUSE)
 
Serving Deep Learning Models At Scale With RedisAI: Luca Antiga
Serving Deep Learning Models At Scale With RedisAI: Luca AntigaServing Deep Learning Models At Scale With RedisAI: Luca Antiga
Serving Deep Learning Models At Scale With RedisAI: Luca Antiga
 
Network-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQNetwork-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQ
 

More from yeokm1

More from yeokm1 (20)

I became a Private Pilot and this is my story
I became a Private Pilot and this is my storyI became a Private Pilot and this is my story
I became a Private Pilot and this is my story
 
What's inside a Cessna 172 and flying a light plane
What's inside a Cessna 172 and flying a light planeWhat's inside a Cessna 172 and flying a light plane
What's inside a Cessna 172 and flying a light plane
 
Speaking at Tech meetups/conferences for Junior Devs
Speaking at Tech meetups/conferences for Junior DevsSpeaking at Tech meetups/conferences for Junior Devs
Speaking at Tech meetups/conferences for Junior Devs
 
Reflections on Trusting Trust for Go
Reflections on Trusting Trust for GoReflections on Trusting Trust for Go
Reflections on Trusting Trust for Go
 
Meltdown and Spectre
Meltdown and SpectreMeltdown and Spectre
Meltdown and Spectre
 
Gentoo on a 486
Gentoo on a 486Gentoo on a 486
Gentoo on a 486
 
BLE Localiser (Full) for iOS Dev Scout
BLE Localiser (Full) for iOS Dev ScoutBLE Localiser (Full) for iOS Dev Scout
BLE Localiser (Full) for iOS Dev Scout
 
BLE Localiser for iOS Conf SG 2017
BLE Localiser for iOS Conf SG 2017BLE Localiser for iOS Conf SG 2017
BLE Localiser for iOS Conf SG 2017
 
Repair Kopitiam Specialty Tools (Part 2): Short Circuit Limiter
 Repair Kopitiam Specialty Tools (Part 2): Short Circuit Limiter Repair Kopitiam Specialty Tools (Part 2): Short Circuit Limiter
Repair Kopitiam Specialty Tools (Part 2): Short Circuit Limiter
 
PCB Business Card (Singapore Power)
PCB Business Card (Singapore Power)PCB Business Card (Singapore Power)
PCB Business Card (Singapore Power)
 
SP Auto Door Unlocker
SP Auto Door UnlockerSP Auto Door Unlocker
SP Auto Door Unlocker
 
SP IoT Doorbell
SP IoT DoorbellSP IoT Doorbell
SP IoT Doorbell
 
Distance Machine Locker
Distance Machine LockerDistance Machine Locker
Distance Machine Locker
 
The slide rule
The slide ruleThe slide rule
The slide rule
 
Repair Kopitiam Circuit Breaker Training
Repair Kopitiam Circuit Breaker TrainingRepair Kopitiam Circuit Breaker Training
Repair Kopitiam Circuit Breaker Training
 
A2: Analog Malicious Hardware
A2: Analog Malicious HardwareA2: Analog Malicious Hardware
A2: Analog Malicious Hardware
 
Getting Started with Raspberry Pi
Getting Started with Raspberry PiGetting Started with Raspberry Pi
Getting Started with Raspberry Pi
 
My Life as a Maker
My Life as a MakerMy Life as a Maker
My Life as a Maker
 
Talk on The Essential Guide to Electronics in Shenzhen by Andrew 'Bunnie' Huang
Talk on The Essential Guide to Electronics in Shenzhen by Andrew 'Bunnie' HuangTalk on The Essential Guide to Electronics in Shenzhen by Andrew 'Bunnie' Huang
Talk on The Essential Guide to Electronics in Shenzhen by Andrew 'Bunnie' Huang
 
Intel Edison: Beyond the Breadboard
Intel Edison: Beyond the BreadboardIntel Edison: Beyond the Breadboard
Intel Edison: Beyond the Breadboard
 

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Recently uploaded (20)

MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 

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
  • 3. Live Swift app Demo 3
  • 4. Swift for IoT @ iOS Conf 2016 4
  • 5. Swift Linux Serial Port library • https://github.com/yeokm1/SwiftLinuxSerial • Deprecated 5
  • 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. Subh: Why not give a talk about your Swift Serial library? 7
  • 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. 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. 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. 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 modem cable • GND <-> GND • TX -> RX • RX <- TX 13http://digital.ni.com/public.nsf/allkb/1EE0DD8AF67922FA86256F720071DECF
  • 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. 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. 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. 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. 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 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. Any questions for a world without these? 20