Don't crash when serial port gets disconnected

This commit is contained in:
Matthias Neeracher 2015-01-02 07:28:20 +01:00 committed by Matthias Neeracher
parent ccfaf34129
commit de93b9e2a9
3 changed files with 20 additions and 1 deletions

View File

@ -10,6 +10,12 @@
extern NSString * kASSerialPortsChanged; extern NSString * kASSerialPortsChanged;
@interface NSFileHandle (ExceptionSafety)
- (NSData *)availableDataIgnoringExceptions;
@end
@interface ASSerial : NSObject @interface ASSerial : NSObject
+ (NSString *) fileNameForPort:(NSString *)port; + (NSString *) fileNameForPort:(NSString *)port;

View File

@ -17,6 +17,19 @@ static NSMutableDictionary * savedAttrs;
NSString * kASSerialPortsChanged = @"PortsChanged"; NSString * kASSerialPortsChanged = @"PortsChanged";
@implementation NSFileHandle (ExceptionSafety)
- (NSData *)availableDataIgnoringExceptions {
@try {
return [self availableData];
}
@catch (NSException *exception) {
return [NSData data];
}
}
@end
@implementation ASSerial @implementation ASSerial
+ (void)initialize { + (void)initialize {

View File

@ -146,7 +146,7 @@ class ASSerialWin: NSWindowController {
serialData = "" serialData = ""
logView.setString(serialData) logView.setString(serialData)
portHandle!.readabilityHandler = {(handle) in portHandle!.readabilityHandler = {(handle) in
let newData = handle.availableData let newData = handle.availableDataIgnoringExceptions()
let newString = NSString(data: newData, encoding: NSASCIIStringEncoding)! let newString = NSString(data: newData, encoding: NSASCIIStringEncoding)!
self.serialData += newString self.serialData += newString
dispatch_async(dispatch_get_main_queue(), { () -> Void in dispatch_async(dispatch_get_main_queue(), { () -> Void in