开源软件名称(OpenSource Name):armadsen/ORSSerialPort开源软件地址(OpenSource Url):https://github.com/armadsen/ORSSerialPort开源编程语言(OpenSource Language):Objective-C 99.1%开源软件介绍(OpenSource Introduction):ORSSerialPortORSSerialPort is an easy-to-use Objective-C serial port library for macOS. It is useful for programmers writing Objective-C or Swift Mac apps that communicate with external devices through a serial port (most commonly RS-232). You can use ORSSerialPort to write apps that connect to Arduino projects, robots, data acquisition devices, ham radios, and all kinds of other devices. Using ORSSerialPort to open a port and send data can be as simple as this: let serialPort = ORSSerialPort(path: "/dev/cu.KeySerial1")
serialPort.baudRate = 4800
serialPort.open()
serialPort.send(someData) // someData is an NSData object
serialPort.close() // Later, when you're done with the port Or, in Objective-C: ORSSerialPort *serialPort = [ORSSerialPort serialPortWithPath:@"/dev/cu.KeySerial1"];
serialPort.baudRate = @4800;
[serialPort open];
[serialPort sendData:someData]; // someData is an NSData object
[serialPort close]; // Later, when you're done with the port ORSSerialPort is released under an MIT license, meaning you're free to use it in both closed and open source projects. However, even in a closed source project, you must include a publicly-accessible copy of ORSSerialPort's copyright notice, which you can find in the LICENSE file. If you have any questions about, suggestions for, or contributions to ORSSerialPort, please contact me. I'd also love to hear about any cool projects you're using it in. This readme provides an overview of the ORSSerialPort library and is meant to provide enough information to get up and running quickly. You can read complete technical documentation for ORSSerialPort on http://cocoadocs.org/docsets/ORSSerialPort/. The ORSSerialPort wiki also contains detailed documentation. Most of the example code in this readme is in Swift. However, ORSSerialPort can also easily be used from Objective-C code. The Examples folder contains Swift and Objective-C versions of all four example projects. See the Example Projects section below for more information. How to Use ORSSerialPortThere are a number of ways to add ORSSerialPort to your project. You can use the included framework project, Carthage, CocoaPods, or the Swift Package Manager. See the Guide to Installing ORSSerialPort for detailed instructions for each of these methods. Opening a Port and Setting It UpYou can get an let port = ORSSerialPort(path: "/dev/cu.KeySerial1") Note that you must give After you've got a port instance, you can open it with the Port settings such as baud rate, number of stop bits, parity, and flow control settings can be set using the various properties port.baudRate = 9600
port.parity = .none
port.numberOfStopBits = 1
port.usesRTSCTSFlowControl = true For more information, see the Getting Started Guide. Sending DataSend raw data by passing a let dataToSend = "Hello".data(using: .utf8)
port.send(dataToSend) Receiving DataTo receive data, you can implement the func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
let string = String(data: data, encoding: .utf8)
print("Got \(string) from the serial port!")
} ORSSerialPortDelegate
ORSSerialPortManager
let ports = ORSSerialPortManager.shared().availablePorts ORSSerialPortManager's For more information about ORSSerialPortManager, see the Getting Started Guide, or read the documentation in ORSSerialPortManager.h. ORSSerialPacketDescriptorIncoming serial data is delivered to your application as it is received. A low level library like ORSSerialPort has no way of knowing anything about the structure and format of the data you're sending and receiving. For example, you may be expecting a complete packet of data, but receive callbacks for each byte. Normally, this requires you to maintain a buffer which you fill up with incoming data, only processing it when a complete packet has been received. In order to eliminate the need for manual management and buffering of incoming data, ORSSerialPort includes a packet parsing API. This is implemented by For more information about ORSSerialPort's packet parsing API, see the Packet Parsing API Guide, read the documentation in ORSSerialPacketDescriptor.h, and see the PacketParsingDemo example app. ORSSerialRequestOften, applications will want to send a command to a device, then wait to receive a specific response before continuing. To ease implementing this kind of scenario, ORSSerialPort includes a request/response API. This is implemented by For example, a program that read the temperature from a connected device might do the following: func readTemperature() {
let command = "$TEMP?;".data(using: String.Encoding.ascii)!
let responseDescriptor = ORSSerialPacketDescriptor(prefixString: "!TEMP", suffixString: ";", maximumPacketLength: 10, userInfo: nil)
let request = ORSSerialRequest(dataToSend: command,
userInfo: SerialBoardRequestType.readTemperature.rawValue,
timeoutInterval: 0.5,
responseDescriptor: responseDescriptor)
serialPort?.send(request)
}
func serialPort(_ serialPort: ORSSerialPort, didReceiveResponse responseData: Data, to request: ORSSerialRequest) {
temperature = temperatureFromResponsePacket(responseData)!
}
func serialPort(_ serialPort: ORSSerialPort, requestDidTimeout request: ORSSerialRequest) {
print("Command timed out!")
} For more information about ORSSerialPort's request/response API, see the Request/Response API Guide, read the documentation in ORSSerialRequest.h, and see the RequestResponseDemo example app. Example ProjectsIncluded with ORSSerialPort is a folder called Examples, containing Xcode projects for small programs demonstrating the use of ORSSerialPort. Each example is available in both Objective-C and Swift. The following example apps are included:
You can read more about these examples on the ORSSerialPort wiki. ContributingContributions to ORSSerialPort are very welcome. However, contributors are encouraged to read the contribution guidelines before starting work on any contributions. Please also feel free to open a GitHub issue or email with questions about specific contributions. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论