orsserialport


## ORSSerialPort


ORSSerialPort is a powerful library for working with serial ports on macOS. It provides a simple and easy-to-use API for opening, closing, reading from, and writing to serial ports.


### Installation


To install ORSSerialPort in your project, you can use CocoaPods. Simply add the following line to your `Podfile`:


“`ruby
pod ‘ORSSerialPort’
“`


### Usage


#### Opening a Serial Port


To open a serial port, you need to create an instance of `ORSSerialPort` and set the desired port name and settings. Here’s an example that opens a serial port with a baud rate of 9600:


“`swift
let serialPort = ORSSerialPort(path: “/dev/tty.usbserial”)
serialPort.baudRate = .baud9600
serialPort.open()
“`


#### Reading Data from a Serial Port


Once a serial port is open, you can start reading data from it. `ORSSerialPortDelegate` provides a method called `serialPort(_:didReceive:)` that gets called whenever new data has been received. Implement this method to handle the received data. Here’s an example that prints the received data:


“`swift
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
guard let string = String(data: data, encoding: .utf8) else { return }
print(“Received data: \(string)”)
}
“`


#### Writing Data to a Serial Port


To send data through a serial port, use the `writeData(_:)` method of `ORSSerialPort`. Here’s an example that sends a string:


“`swift
let data = “Hello, Serial Port!”.data(using: .utf8)!
serialPort.writeData(data)
“`


#### Closing a Serial Port


To close a serial port, call the `close()` method of `ORSSerialPort`. Here’s an example:


“`swift
serialPort.close()
“`


### Additional Functionality


#### Available Serial Ports


To get a list of available serial ports, use the `availableSerialPorts()` class method of `ORSSerialPortManager`. Here’s an example that prints the available serial ports:


“`swift
let availablePorts = ORSSerialPortManager.shared().availablePorts
for port in availablePorts {
print(“Available port: \(port.path)”)
}
“`


#### Observing Serial Port Additions and Removals


To receive notifications when serial ports are added or removed, use the `serialPortsWereConnected(_:)` and `serialPortsWereDisconnected(_:)` methods of `ORSSerialPortManagerDelegate`. Here’s an example:


“`swift
extension YourClass: ORSSerialPortManagerDelegate {
func serialPortsWereConnected(_ newPorts: [ORSSerialPort]) {
for port in newPorts {
print(“Port connected: \(port.path)”)
}
}

func serialPortsWereDisconnected(_ removedPorts: [ORSSerialPort]) {
for port in removedPorts {
print(“Port disconnected: \(port.path)”)
}
}
}
“`


### Conclusion


ORSSerialPort is a reliable and easy-to-use library for working with serial ports on macOS. With its simple API, you can easily open, close, read from, and write to serial ports in your applications. This documentation provides an overview of the library’s main features and how to use them, getting you up to speed in no time.


* Install ORSSerialPort via CocoaPods
* Open a serial port
* Read data from a serial port
* Write data to a serial port
* Close a serial port
* Get a list of available serial ports
* Observe serial port additions and removals