4 Commits
0.0.3 ... 0.0.6

Author SHA1 Message Date
Yeo Kheng Meng
640cf2f7c3 updated example 2016-10-27 09:30:05 +08:00
Yeo Kheng Meng
21adc2c8f4 add comment for Mac that root is not required to access serial port 2016-10-27 09:24:24 +08:00
Yeo Kheng Meng
4ff61e6c2c Remove Linux references 2016-10-27 09:14:42 +08:00
Yeo Kheng Meng
1cfb2028e4 Update README.md 2016-10-26 23:15:26 +08:00
2 changed files with 13 additions and 8 deletions

View File

@@ -25,6 +25,7 @@ do {
print("Serial port \(portName) opened successfully.")
defer {
serialPort.closePort()
print("Port Closed")
}
serialPort.setSettings(receiveRate: .baud9600,
@@ -73,10 +74,10 @@ do {
break
}
}
print("End of example");
print("We successfully received back \(numberOfMultiNewLineTest) lines")
} catch PortError.failedToOpen {
print("Serial port \(portName) failed to open. You might need root permissions.")
} catch {

View File

@@ -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.
```bash
git clone https://github.com/yeokm1/SwiftLinuxSerial.git
cd SwiftLinuxSerial/Examples/SwiftSerialExample/
git clone https://github.com/yeokm1/SwiftSerial.git
cd SwiftSerial/Examples/SwiftSerialExample/
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
#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.
```
@@ -98,7 +102,7 @@ Then run `swift build` to download the dependencies and compile your project. Yo
```swift
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
@@ -113,9 +117,9 @@ Opening the port without any parameters will set the port to receive and transmi
```swift
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.