Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e4b905e7b | ||
|
|
e73ea6056d | ||
|
|
640cf2f7c3 | ||
|
|
21adc2c8f4 | ||
|
|
4ff61e6c2c | ||
|
|
1cfb2028e4 |
@@ -25,6 +25,7 @@ do {
|
|||||||
print("Serial port \(portName) opened successfully.")
|
print("Serial port \(portName) opened successfully.")
|
||||||
defer {
|
defer {
|
||||||
serialPort.closePort()
|
serialPort.closePort()
|
||||||
|
print("Port Closed")
|
||||||
}
|
}
|
||||||
|
|
||||||
serialPort.setSettings(receiveRate: .baud9600,
|
serialPort.setSettings(receiveRate: .baud9600,
|
||||||
@@ -73,10 +74,10 @@ do {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print("End of example");
|
||||||
|
|
||||||
|
|
||||||
print("We successfully received back \(numberOfMultiNewLineTest) lines")
|
|
||||||
|
|
||||||
} catch PortError.failedToOpen {
|
} catch PortError.failedToOpen {
|
||||||
print("Serial port \(portName) failed to open. You might need root permissions.")
|
print("Serial port \(portName) failed to open. You might need root permissions.")
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
26
README.md
26
README.md
@@ -63,12 +63,16 @@ export PATH=$HOME/swift-3.0/usr/bin:$PATH
|
|||||||
To get started quickly, you can take a look at my example project [here](Examples/SwiftSerialExample). In order to run the 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.
|
To get started quickly, you can take a look at my example project [here](Examples/SwiftSerialExample). In order to run the 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.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://github.com/yeokm1/SwiftLinuxSerial.git
|
git clone https://github.com/yeokm1/SwiftSerial.git
|
||||||
cd SwiftLinuxSerial/Examples/SwiftSerialExample/
|
cd SwiftSerial/Examples/SwiftSerialExample/
|
||||||
swift build
|
swift build
|
||||||
#You need root to access the serial port. Replace /dev/ttyUSB0 with the name of your serial port under test
|
|
||||||
|
#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
|
sudo ./.build/debug/SwiftSerialExample /dev/ttyUSB0
|
||||||
|
|
||||||
|
#For Mac: Root is not required
|
||||||
|
./.build/debug/SwiftSerialExample /dev/tty.usbserial
|
||||||
|
|
||||||
#If all goes well you should see a series of messages informing you that data transmitted has been received properly.
|
#If all goes well you should see a series of messages informing you that data transmitted has been received properly.
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -98,7 +102,7 @@ Then run `swift build` to download the dependencies and compile your project. Yo
|
|||||||
```swift
|
```swift
|
||||||
let serialPort: SerialPort = SerialPort(path: portName)
|
let serialPort: SerialPort = SerialPort(path: portName)
|
||||||
```
|
```
|
||||||
Supply the portname that you wish to open like `/dev/ttyUSB0` or `/dev/usbserial`.
|
Supply the portname that you wish to open like `/dev/ttyUSB0` or `/dev/tty.usbserial`.
|
||||||
|
|
||||||
### Opening the Serial Port
|
### Opening the Serial Port
|
||||||
|
|
||||||
@@ -113,9 +117,9 @@ Opening the port without any parameters will set the port to receive and transmi
|
|||||||
```swift
|
```swift
|
||||||
serialPort.setSettings(receiveRate: .baud9600, transmitRate: .baud9600, minimumBytesToRead: 1)
|
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 intend to use one function. For example, transmitRate will be ignored if you specified `andTransmit : false` when opening the port.
|
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 Linux must wait to receive before it will return from a [read()](https://linux.die.net/man/2/read) function. If in doubt, just put 1.
|
`minimumBytesToRead` determines how many characters the system must wait to receive before it will return from a [read()](https://linux.die.net/man/2/read) function. If in doubt, just put 1.
|
||||||
|
|
||||||
This function has been defined with default settings as shown in the function definition.
|
This function has been defined with default settings as shown in the function definition.
|
||||||
|
|
||||||
@@ -162,6 +166,11 @@ func readUntilChar(_ terminator: CChar) throws -> String
|
|||||||
```
|
```
|
||||||
Keep reading until the specified CChar is encountered. Return the string read so far without that value.
|
Keep reading until the specified CChar is encountered. Return the string read so far without that value.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
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 `readBytes()`.
|
||||||
|
|
||||||
### Writing data to the port
|
### 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`.
|
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`.
|
||||||
@@ -181,6 +190,11 @@ func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> I
|
|||||||
```
|
```
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
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
|
### Closing the port
|
||||||
|
|
||||||
Just do `serialPort.closePort()` to close the port once you are done using it.
|
Just do `serialPort.closePort()` to close the port once you are done using it.
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ public enum ParityType {
|
|||||||
case even
|
case even
|
||||||
case odd
|
case odd
|
||||||
|
|
||||||
var parityValue: tcflag_t {
|
var parityValue: tcflag_t {
|
||||||
switch self {
|
switch self {
|
||||||
case .none:
|
case .none:
|
||||||
return 0
|
return 0
|
||||||
@@ -389,7 +389,7 @@ extension SerialPort {
|
|||||||
data = Data(bytes: buffer, count: bytesRead)
|
data = Data(bytes: buffer, count: bytesRead)
|
||||||
} else {
|
} else {
|
||||||
//This is to avoid the case where bytesRead can be negative causing problems allocating the Data buffer
|
//This is to avoid the case where bytesRead can be negative causing problems allocating the Data buffer
|
||||||
data = Data(bytes: buffer, count: 0)
|
data = Data(bytes: buffer, count: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data
|
return data
|
||||||
@@ -421,17 +421,17 @@ extension SerialPort {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while true {
|
while true {
|
||||||
let bytesRead = try readBytes(into: buffer, size: 1)
|
let bytesRead = try readBytes(into: buffer, size: 1)
|
||||||
|
|
||||||
if bytesRead > 0 {
|
if bytesRead > 0 {
|
||||||
let character = CChar(buffer[0])
|
let character = CChar(buffer[0])
|
||||||
|
|
||||||
if character == terminator {
|
if character == terminator {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
data.append(buffer, count: 1)
|
data.append(buffer, count: 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let string = String(data: data, encoding: String.Encoding.utf8) {
|
if let string = String(data: data, encoding: String.Encoding.utf8) {
|
||||||
@@ -445,6 +445,25 @@ extension SerialPort {
|
|||||||
let newlineChar = CChar(10) // Newline/Line feed character `\n` is 10
|
let newlineChar = CChar(10) // Newline/Line feed character `\n` is 10
|
||||||
return try readUntilChar(newlineChar)
|
return try readUntilChar(newlineChar)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func readChar() throws -> UnicodeScalar {
|
||||||
|
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
|
||||||
|
|
||||||
|
defer {
|
||||||
|
buffer.deallocate(capacity: 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
while true {
|
||||||
|
let bytesRead = try readBytes(into: buffer, size: 1)
|
||||||
|
|
||||||
|
if bytesRead > 0 {
|
||||||
|
let character = UnicodeScalar(buffer[0])
|
||||||
|
return character
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Transmitting
|
// MARK: Transmitting
|
||||||
@@ -480,4 +499,10 @@ extension SerialPort {
|
|||||||
|
|
||||||
return try writeData(data)
|
return try writeData(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func writeChar(_ character: UnicodeScalar) throws -> Int{
|
||||||
|
let stringEquiv = String(character)
|
||||||
|
let bytesWritten = try writeString(stringEquiv)
|
||||||
|
return bytesWritten
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user