• This repository has been archived on 27/Jan/2024
  • Stars
    star
    137
  • Rank 266,121 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 8 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Swift Linux and Mac library for reading and writing to serial ports.

SwiftSerial

A Swift Linux and Mac library for reading and writing to serial ports. This library has been tested to work on macOS Mojave, Linux Mint 18 (based on Ubuntu 16.04) and on the Raspberry Pi 3 on Ubuntu 16.04 and Raspberry Pi 4 on Raspian Buster. Other platforms using Ubuntu like the Beaglebone might work as well.

This library is an improvement over my previous now deprecated library SwiftLinuxSerial which was less Swifty and supported only Linux. This library is thanks largely to Jay Jun. His original pull request can be found here.

Talk on this library

I gave a talk on this library and one of its examples SwiftSerialIM. Click on the links below to see the slides and video.

My slides on slideshare

Mac OS Preparation

You should have Xcode 8 or above installed with the command line tools.

You may need to enable USB and/or serial entitlement.

To develop app with XCode, enable the App Sandbox capability in Xcode, and under Hardware, select USB. (Mac Apps are sandboxed and you need the USB entitlement.) Swift 3.0

For Serial entitlement, there is no checkbox for it. So com.apple.security.device.serial needs to be manually added to the "YourApp.entitlements" file in the Xcode project. Thanks to this issue raised by @doHernandezM.

Linux System Preparation

Varies depending on system...

Jumping straight into sample code

To get started quickly, you can take a look at my example projects here.

Example 1: Loopback Test

In order to run this example properly, you need to connect one of your (USB/UART) serial ports in a loopback manner. Basically, you short the TX and RX pins of the serial port. This library currently only support the /dev/cu.* variant on Mac. Read the beginning of the API usage section for more details.

git clone https://github.com/yeokm1/SwiftSerial.git
cd SwiftSerial/Examples/SwiftSerialExample/
swift build

#For Linux: You need root to access the serial port. Replace /dev/ttyUSB0 with the name of your serial port under test
sudo ./.build/debug/SwiftSerialExample /dev/ttyUSB0

#For Mac: Root is not required
./.build/debug/SwiftSerialExample /dev/cu.usbserial

#If all goes well you should see a series of messages informing you that data transmitted has been received properly.

Example 2: Binary Loopback Test

Variant of example 1 but testing the transfer of binary data specifically ensuring the0x0D bit is not converted to another character.

git clone https://github.com/yeokm1/SwiftSerial.git
cd SwiftSerial/Examples/SwiftSerialBinary/
swift build

#For Linux: You need root to access the serial port. Replace /dev/ttyUSB0 with the name of your serial port under test
sudo ./.build/debug/SwiftSerialBinary /dev/ttyUSB0

#For Mac: Root is not required
./.build/debug/SwiftSerialBinary /dev/cu.usbserial

#If all goes well you should see a series of messages informing you that data transmitted has been received properly.

Example 3: A chat app between 2 machines

In order to run this example properly, you need 2 machines connected by a null-modem cable or 2 USB-Serial adapters with the TX-RX pins connected to each other. Run a copy of my program on both machines.

git clone https://github.com/yeokm1/SwiftSerial.git
cd SwiftSerial/Examples/SwiftSerialIM/
swift build

#For Linux: You need root to access the serial port. Replace /dev/ttyUSB0 with the name of your serial port under test
sudo ./.build/debug/SwiftSerialIM /dev/ttyUSB0

#For Mac: Root is not required
./.build/debug/SwiftSerialIM /dev/cu.usbserial

People at both machines can now "chat" with each other.

Integrating with your project

Add SwiftSerial as a dependency to your project by editing the Package.swift file.

let package = Package(
    name: "NameOfMyProject",
    dependencies: [
        .package(url: "https://github.com/yeokm1/SwiftSerial.git", from: "0.1.2"),
        ...
    ]
    ...
)

Make sure to import SwiftSerial in the source files that use my API.

Then run swift build to download the dependencies and compile your project. Your executable will be found in the ./.build/debug/ directory.

API usage

Initialise the class

let serialPort: SerialPort = SerialPort(path: portName)

Supply the portname that you wish to open like /dev/ttyUSB0 or /dev/cu.usbserial.

For Macs, this library currently only works with the /dev/cu.* ports instead of the /dev/tty.*. I have enabled blocking on the serial port to prevent high CPU usage which will prevent the /dev/tty.* from working. Read more about the differences between the two here. If there is a problem, open an issue describing your situation and let me look into it.

Opening the Serial Port

func openPort()
func openPort(toReceive receive: Bool, andTransmit transmit: Bool)

Opening the port without any parameters will set the port to receive and transmit by default. You can still choose to receive-only, transmit-only or both. Will throw PortError.mustReceiveOrTransmit if you set both parameters to false. Can also throw PortError.failedToOpen and PortError.invalidPath.

Set port settings

serialPort.setSettings(receiveRate: .baud9600, transmitRate: .baud9600, minimumBytesToRead: 1)

The port settings call can be as simple as the above. For the baud rate, just supply both transmit and receive even if you are only intending to use one transfer direction. For example, transmitRate will be ignored if you specified andTransmit : false when opening the port.

minimumBytesToRead determines how many characters the system must wait to receive before it will return from a read() function. If in doubt, just put 1.

This function has been defined with default settings as shown in the function definition.

func setSettings(receiveRate: BaudRate,
                 transmitRate: BaudRate,
                 minimumBytesToRead: Int,
                 timeout: Int = 0, /* 0 means wait indefinitely */
                 parityType: ParityType = .none,
                 sendTwoStopBits: Bool = false, /* 1 stop bit is the default */
                 dataBitsSize: DataBitsSize = .bits8,
                 useHardwareFlowControl: Bool = false,
                 useSoftwareFlowControl: Bool = false,
                 processOutput: Bool = false)

If the default settings do not suit you, just pass in extra parameters to override them.

Reading data from port

There are several functions you can use to read data. All functions here are blocking till the expected number of bytes has been received or a condition has been met. All functions can throw PortError.mustBeOpen.

func readString(ofLength length: Int) throws -> String

This is the easiest to use if you are sending text data. Just provide how many bytes you expect to read. The result will then be returned as a typical Swift String. This function internally calls readData().

func readData(ofLength length: Int) throws -> Data

This function is if you intend to receive binary data. This function internally calls readBytes()

func readBytes(into buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int

If you intend to play with unsafe pointers directly, this is the function for you! Will return the number of bytes read. Note that you are responsible for allocating the pointer before passing into this function then deallocate the pointer once you are done.

func readLine() throws -> String

Read byte by byte till the newline character \n is encountered. A String containing the result so far will be returned without the newline character. This function internally calls readUntilChar(). Can throw PortError.stringsMustBeUTF8.

func readUntilChar(_ terminator: CChar) throws -> String

Keep reading until the specified CChar is encountered. Return the string read so far without that value.

func readByte() throws -> UInt8

Read only one byte. This works best if minimumBytesToRead has been set to 1 when opening the port. This function internally calls readBytes().

func readChar() throws -> UnicodeScalar

Read only one character. This works best if minimumBytesToRead has been set to 1 when opening the port. This function internally calls readByte().

Writing data to the port

There are several functions you can use to write data. All functions here are blocking till all the data has been written. All functions can throw PortError.mustBeOpen.

func writeString(_ string: String) throws -> Int

Most straightforward function, String in then transmit! Will return how many bytes actually written. Internally calls writeData()

func writeData(_ data: Data) throws -> Int

Binary data in, then transmit! Will return how many bytes actually written. Internally calls writeBytes().

func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int

Function for those that want to mess with unsafe pointers. You have to specify how many bytes have to be written. Will return how many bytes actually written.

func writeChar(_ character: UnicodeScalar) throws -> Int{

Writes only one character. Will return 1 if successful. This function internally calls writeString(). Pull requests for a better way of doing this is appreciated.

Closing the port

Just do serialPort.closePort() to close the port once you are done using it.

External References

This library cannot be written without the amazing reference code I depended on.

  1. Xanthium's Serial Port Programming on Linux
  2. Chrishey Drick's Reading data from Serial Port

More Repositories

1

docs-to-pdf-converter

A standalone Java library/command line tool that converts DOC, DOCX, PPT, PPTX and ODT documents to PDF files.
Java
590
star
2

w31slack

A proof-of-concept Slack client for Windows for Workgroups 3.11 with tests.
C
219
star
3

doschgpt

A proof-of-concept ChatGPT and Hugging Face client for DOS with text-to-speech for Sound Blaster compatible systems.
C++
199
star
4

http-to-https-proxy

A proxy that upgrades HTTP connections to HTTPS for systems which cannot make HTTPS requests.
Go
83
star
5

gentoo-on-486

Instructions on how to install modern Gentoo Linux on ancient 486-based PCs.
67
star
6

programming-win31

The sample codes provided in the floppy disk of Charles Petzold's book Programming Windows 3.1.
C
49
star
7

ndp2019-wristband-teardown

Tear-down effort of the Pixmob wristband used in NDP2019.
47
star
8

retro-configs

Collection of my DOS configurations and drivers of my retro machines
Assembly
31
star
9

reverse-engineering-ndp2016-wristband

Schematics of the reverse engineered Pixmob wristband used in NDP2016. Sample IR brute-force code is available but has not been fully tested in practice.
KiCad Layout
25
star
10

covox-music-player

A Linux program that plays MP3 or WAV files to the Covox Speech Thing or its clones via a native parallel port.
C
18
star
11

nus-soc-print

An Android application that prints office documents and PDF files to Unix printers in NUS School Of Computing via SSH
Java
16
star
12

reflections-on-trusting-trust

A talk (including sample code) about the Turing award lecture "Reflections on Trusting Trust" originally given by Ken Thompson.
C
16
star
13

pcb-covox-amp

A tiny sound card based on the Covox Speech Thing design which includes an LM386 amplifier.
15
star
14

coders-dvorak-regular-keypad

Programmer Dvorak layout with the typical keypad layout instead of the modified one by Roland Kaufmann.
15
star
15

distance-machine-locker

A system that locks your computer the moment you move away from it.
Swift
14
star
16

ssim

Program to measure the similarity between two videos using the OpenCV library and the structural similarity algorithm (SSIM). This is modified from the video-input-psnr-ssim tutorial of OpenCV.
C++
13
star
17

intro-to-ble

A talk about basic Bluetooth Low Energy concepts followed by code explanations.
Java
11
star
18

ne2000plus-collection

Collection of NE2000+ software obtained from various sources
Assembly
11
star
19

pcb-covox

A tiny sound card based on the Covox Speech Thing design.
9
star
20

pi-radio

Raspberry Pi internet radio streamer for Singapore. Can be modified to stream other internet radio stations.
Python
9
star
21

nus-soc-print-ios

An iOS Application that prints office documents and PDF files to Unix printers in NUS School Of Computing.
Swift
8
star
22

ssim-cuda

CUDA Program to measure the similarity between two videos using the OpenCV library and the structural similarity algorithm (SSIM). This is modified from the video-input-psnr-ssim tutorial of OpenCV
C++
7
star
23

ble-localiser

This talk is about how I manage to do localisation using 3 Raspberry Pis(RPis) as beacons.
Swift
7
star
24

iot-esp32-mcu-workshop

This is an introductory IoT ESP32 Microcontroller workshop to get acquainted with microcontroller programming and simple electronics.
C++
6
star
25

dvfs

A C++ native Android program that does dynamic frequency scaling to achieve a target frames-per-second (FPS).
C++
5
star
26

reflections-on-trusting-trust-go

A talk (including sample Golang code) about the Turing award lecture "Reflections on Trusting Trust" originally given by Ken Thompson.
Go
5
star
27

SwiftLinuxSerial

A Swift 3 Linux-only library for reading and writing to serial ports. (Deprecated for SwiftSerial)
Swift
4
star
28

ht107e-socket-tester-teardown

4
star
29

intro-to-pcb-design-eagle

A class to introduce students to designing Printed Circuit Boards (PCBs) using the Eagle software.
Eagle
4
star
30

pcb-usb-ft245r-parallel-adapter

A USB to parallel adapter using the FTDI FT245R chip. This is not a IEEE 1284 compliant parallel port. Just using the DB25 connector to access the data pins.
KiCad Layout
3
star
31

pcb-name-card

My business card that is on a Printed Circuit Board (PCB). Comes with an white LED, UV LED, ruler and QR code.
Eagle
3
star
32

getting-started-with-rpi

A talk about introducing the audience to setting up the Raspberry Pi as well as basic Python coding on the GPIO pins.
Python
3
star
33

intro-to-rpi

A short introductory course I conducted for people new to Raspberry Pis and Linux.
Python
3
star
34

odroid-power-measure

A Java program that dynamically shows FPS, CPU/GPU frequency and power of Odroid-XU.
Java
2
star
35

pcb-breakout-ltc3625-tps63031

Pin breakout of the LTC3625 supercapacitor charger with TPS63031 3.3V regulator to through-hole pin headers for prototyping
2
star
36

pcb-covox-amp-v2

A tiny sound card based on the Covox Speech Thing design which includes an LM386 amplifier and FT245RL chip to receive data via USB. This project is a combination of my 2 earlier projects pcb-covox-amp and pcb-usb-ft245r-parallel-adapter
KiCad Layout
2
star
37

mov-is-turing-complete

A talk about the academic paper "mov is Turing-complete" written by Stephan Dolan". Practical examples from movcc compiler by Chris Domas.
C
2
star
38

sensor_clock_2

A clock composed of an Arduino, temperature and humidity sensors. Includes integrated charger.
Arduino
2
star
39

vibrate_alarm_clock_2

An vibrating alarm clock based on Microview.
Arduino
1
star
40

start-sslstrip-on-boot

Scripts to to start SSLStrip on boot for Arch Linux ARM on Raspberry Pi
Shell
1
star
41

power_measure_tool

An Arduino setup to measure the voltage and current of a load.
C++
1
star
42

retro-configs-apple

Collection of my setup configurations and disc locations of my retro Apple machines
1
star
43

cs5272-project

A group project done in NUS CS5272 module in AY2014-2015 Sem 2.
C
1
star
44

yeokm1.github.io-hugo

Hugo source files for my site at http://yeokhengmeng.com.
JavaScript
1
star
45

yeokm1.github.io

Hugo compiled frontend files for my site at http://yeokhengmeng.com.
HTML
1
star
46

pcb-bme280-breakout

Pin breakout of the Bosch BME280 sensor to through-hole pin headers for prototyping
Eagle
1
star
47

dvfsapp

An app front end that does dynamic frequency scaling to achieve a target frames-per-second (FPS).
Java
1
star
48

vibrate_alarm_clock

An alarm clock with vibration capabilities.
Arduino
1
star
49

auto-rotate-screen

A program that auto rotates the screen when the Thinkpad is rotated.
C++
1
star
50

fpsmeasure

Java
1
star