问题
I am using USB GlobalSat(USG-MR350) GPS device. I want to get location data (latitude and longitude) from the device within my mac cocoa application.Tried to run the AMSerialPort sample code.It is detecting the usb device but it is giving output in nonreadable format.How can this data be converted to readable format.This is a part of the source code:
- (void)serialPortReadData:(NSDictionary *)dataDictionary
{
// this method is called if data arrives
// @"data" is the actual data, @"serialPort" is the sending port
AMSerialPort *sendPort = [dataDictionary objectForKey:@"serialPort"];
NSData *data = [dataDictionary objectForKey:@"data"];
if ([data length] > 0) {
NSString *text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[outputTextView insertText:text];
[text release];
// continue listening
[sendPort readDataInBackground];
} else { // port closed
[outputTextView insertText:@"port closed\r"];
}
[outputTextView setNeedsDisplay:YES];
[outputTextView displayIfNeeded];
}
回答1:
I think that Sirf receiver is by default in binary mode, and that you have to put it in NMEA mode explicitly.
回答2:
Try using the NSData's ability to print itself as bytes via the description method.
[outputTextView insertText:[data description]];
回答3:
I don't see you are checking port settings.
Default settings for NMEA over serial port is speed:4800stopbit:1 parity:none
.
Check it on the device.
来源:https://stackoverflow.com/questions/9462538/how-to-read-nmea-data-from-usb-gps-device-through-cocoa-application