armadsen / ORSSerialPort

Serial port library for Objective-C and Swift macOS apps
MIT License
751 stars 183 forks source link

how to use ORSSerialPacketDescriptor without prefix? #175

Closed iopq1 closed 2 years ago

iopq1 commented 3 years ago

hi my friends. my device not returned prefix. suffix "\r\n". i try use - let responseDescripto = ORSSerialPacketDescriptor(prefixString: nil, suffixString: "\r\n", maximumPacketLength: 12, userInfo: nil) but get {length = 2, bytes = 0x0d0a} in - func serialPort(_ serialPort: ORSSerialPort, didReceiveResponse responseData: Data, to request: ORSSerialRequest) { print(responseData as NSData) } really data contains 12 bytes include suffix

armadsen commented 2 years ago

If you're sure that packets always contain exactly 12 bytes, you can do this:

let responseDescriptor = ORSSerialPacketDescriptor(maximumPacketLength: 12, userInfo: nil) { data in
    guard let data = data, let string = String(data: data, encoding: .ascii) else { return false }
    if !string.hasSuffix("\r\n") { return false }
    return data.count == 12
}