Swift nsinputstream没有从Trimble

时间:2015-09-14 19:08:03

标签: swift nsinputstream

我已经为Android开发了这个,并且只使用了 bufferedreader br.readline(),并且代码正确地解释了通过的数据。

我应该得到这样的数据:

$GPRMC,191830.00,A,4159.87613,N,09338.29258,W,0.065,303.4,270815,0.8,E,D*21

我得到的是这样的数据:

(WK#(是的,那就是它)

viewDidLoad()

中的代码
if accessoryList.count > 0 {
        println(accessoryList[0].description)
        session = EASession(accessory: accessoryList[0] as! EAAccessory, forProtocol: "com.trimble.mcs.gnss")
        if session != nil {
            inputStream = session.inputStream
            //outputStream = session.outputStream

            inputStream.delegate = self
            inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
            inputStream.open()
        }
    }
    else {
    }

和我的流函数()

func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {

    let inStream = aStream as? NSInputStream

    switch (eventCode){
    case NSStreamEvent.OpenCompleted:
        NSLog("Stream opened")
        break
    case NSStreamEvent.HasBytesAvailable:
        NSLog("HasBytesAvailable")
        let bufferSize = 128
        var buffer = [UInt8](count: bufferSize, repeatedValue: 0)
        if(session.inputStream!.hasBytesAvailable) {
            let result :Int = session.inputStream!.read(&buffer, maxLength: bufferSize)
            if(result > 0){
                var output = NSString(bytes: &buffer, length: bufferSize, encoding: NSUTF8StringEncoding)
                if(output != nil) {
                    println(output!)
                }
            }
        }
        break
    case NSStreamEvent.ErrorOccurred:
        NSLog("ErrorOccurred")
        break
    case NSStreamEvent.EndEncountered:
        NSLog("EndEncountered")
        break
    default:
        NSLog("unknown.")
    }
}

我有什么遗失的东西吗?我收到数据,但不是英文。我知道编码是UTF8。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

事实证明,有两种方法可以建立配对连接。 Trimble向我发送了2个数据流,一个是二进制写的,另一个是nmea字符串。我还没弄明白如何在iphone上获取nmea字符串,但我更喜欢更准确的二进制值。以下是有关如何解释二进制数据http://www.trimble.com/OEM_ReceiverHelp/v4.85/en/GSOFmessages_Overview.html的说明和解释的链接。

我在一些帮助下重写了代码:

case NSStreamEvent.HasBytesAvailable:
        NSLog("HasBytesAvailable")
        let bufferSize = 512
        var buffer = Array<UInt8>(count: bufferSize, repeatedValue: 0)
        if(session.inputStream!.hasBytesAvailable) {
            let result :Int = session.inputStream!.read(&buffer, maxLength: bufferSize)
            print(buffer[0..<result].map{String(format: "%02x", Int32($0))}.joinWithSeparator(" "))
        }
        break

我希望这可以帮助其他人寻找这个答案。此代码以2字节数组打印出来。