This tutorial explains how you can program the serial port in Swift. Weguide you through a reference implementation for your own projectsincluding all practical steps. The main prerequisite for this tutorialis that you have installed CocoaPods.
1. Background Information¶
A simple serial port program has three main functions. First it shouldinit the serial port, and then it should read data from the serial portand transmit data read from the serial port. The latter requirementmeans that two processes are necessary running simultaneously.
About Java SerialPort SerialPort is the world's first, and still foremost Java product for serial ports. SerialPort provides highly flexible control of serial ports from your Java application.SerialPort is a high-performance class that also provides low-level serial port control. Java and the Mac OS X Terminal $ java -version $ javac HelloWorld.java $ java HelloWorld if your program gets stuck in an infinite loop, type Ctrl-c to break out. If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-d for EOF (end of file).
In a Unix environment a serial port is accessed via a file descriptor.This means a terminal program uses the file access API to read and writedata to a serial port. The serial port handle is available in /dev andhas the prefix cu, which stands for calling-unit. To access the serialport, this handle is just opened as an ordinary file. The opened serialport file descriptor also conforms to the TTY API for setting serialport specific settings like baudrate.
This tutorial describes how to implement the simple serial port programin OS X in Swift. Accessing a serial port in OS X requires the use ofthree API's: IOKit, common BSD file handling APIs and BSD TTY APIs. Thefirst two are reasonably easy to use from Swift, but the third does notimport quite well. The BSD TTY APIs uses complex macros and varargswhich are not supported by Swift directly. The obvious choice is to usean Objective-C or C wrapper as we also will do in this tutorial. Wecould write our own wrapper, but luckily, an excellent serial portlibrary exists. This library is ORSSerialPort whichprovides a great API for using serial ports in OS X. The last nicefeature of the ORSSerialPort library is that it also handles thethreading related to using the serial port.
2. Setting up Xcode¶
Start Xcode, and create a new Command Line project for OS X.
3. Import ORSSerialPort¶
Edit the file and add the line:
During downloading the dependencies CocoaPods creates an Xcode workspaceproject. Close your earlier created project, and reopen the workspacefile. Xcode should now open with a new folder 'Pods' containing theORSSerialPort.
Java Serial Port Mac Os X 10.10
Try to run the project, it should print 'Hello World!'. Note, theORSSerialPort library uses the newest API functionality of Apple, likeNS ASSUME NONNULL and nullable. in case you use XCode prior to 6.3.2you need to disable the NS ASSUME NONNULL BEGIN andNS ASSUME NONNULL END macros and remove the ‘nullable' property in deORSSerialPort source code.
About Java SerialPort SerialPort is the world's first, and still foremost Java product for serial ports. SerialPort provides highly flexible control of serial ports from your Java application.SerialPort is a high-performance class that also provides low-level serial port control. Java and the Mac OS X Terminal $ java -version $ javac HelloWorld.java $ java HelloWorld if your program gets stuck in an infinite loop, type Ctrl-c to break out. If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-d for EOF (end of file).
In a Unix environment a serial port is accessed via a file descriptor.This means a terminal program uses the file access API to read and writedata to a serial port. The serial port handle is available in /dev andhas the prefix cu, which stands for calling-unit. To access the serialport, this handle is just opened as an ordinary file. The opened serialport file descriptor also conforms to the TTY API for setting serialport specific settings like baudrate.
This tutorial describes how to implement the simple serial port programin OS X in Swift. Accessing a serial port in OS X requires the use ofthree API's: IOKit, common BSD file handling APIs and BSD TTY APIs. Thefirst two are reasonably easy to use from Swift, but the third does notimport quite well. The BSD TTY APIs uses complex macros and varargswhich are not supported by Swift directly. The obvious choice is to usean Objective-C or C wrapper as we also will do in this tutorial. Wecould write our own wrapper, but luckily, an excellent serial portlibrary exists. This library is ORSSerialPort whichprovides a great API for using serial ports in OS X. The last nicefeature of the ORSSerialPort library is that it also handles thethreading related to using the serial port.
2. Setting up Xcode¶
Start Xcode, and create a new Command Line project for OS X.
3. Import ORSSerialPort¶
Edit the file and add the line:
During downloading the dependencies CocoaPods creates an Xcode workspaceproject. Close your earlier created project, and reopen the workspacefile. Xcode should now open with a new folder 'Pods' containing theORSSerialPort.
Java Serial Port Mac Os X 10.10
Try to run the project, it should print 'Hello World!'. Note, theORSSerialPort library uses the newest API functionality of Apple, likeNS ASSUME NONNULL and nullable. in case you use XCode prior to 6.3.2you need to disable the NS ASSUME NONNULL BEGIN andNS ASSUME NONNULL END macros and remove the ‘nullable' property in deORSSerialPort source code.
4. Hooking Objective-C code to your Swift project¶
To use the ORSSerialPort library in your Swift code you need to link itvia a bridge header. First add a new header file to your project.
Add the following lines to this bridge header file:
Second, you need to tell the compiler to use this bridge header in thelinking process. Under the project Build Settings, set the Objective-CBridging Header build setting under Swift Compiler - Code Generation tothe path of the header relative to the project root.
Try tool compile the project to see if it still works.
5. The Serial Terminal Program¶
You are now ready to access the serial port from Swift. We removed allthe serial device detection code in this example, but it is not hard toadd our example, or use the ORSSerialPortManager provided by ORSSerialPort.
The complete source of the simple serial program for Swift:
How does it works: First, a SerialHandler class is defined, whichimplements the ORSSerialPortDelegates. When this class is initialized,it setups a stdin reader. This reader calls the 'handleUserInput'function to send the received data to the serial port. Second, it setupsthe serial port and adds itself as delegate to the serial port objectprovided by ORSSerialPort. This Delegate handles all the events of theserial port. The function 'serialPort' will be called if the serial portreceives data. Last step is that it keeps itself in a loop to listen toall incoming data.
You should adjust the handle in this code to the filename of your serialdevice. When you run the tool you should be able to send and receivedata:
6. Downloads¶
Java Serial Port Mac Os X El Capitan
You can download the code from GitHubvia: https://github.com/bjarnoldus/osx-swift-serial
Java Serial Port Mac Os X 10.13
Mac OS x 10.8.3, running a Java's web application on Tomcat, port 8080. Users from same network are able to access my web server via port 80, but failed on port 8080. Is there a firewall setting blocked the incoming connection via port 8080.
Solution 1
The simplest solution is turn off your Firewall.
Mac icon > System Preferences > Security & Privacy > Firewall > Turn Off Firewall
Solution 2
Actually, Mac didn't block port 8080, it just don't allows 'incoming connections' for '/usr/bin/java
'.
Mac icon > System Preferences > Security & Privacy > Firewall > Firewall Options…
Java Serial Port Mac Os X64
Add a new entry with the '+
' button, search for 'Java', add it and choose 'Allow incoming connections'.