Don't crash when serial port gets disconnected
This commit is contained in:
parent
ccfaf34129
commit
de93b9e2a9
|
@ -10,6 +10,12 @@
|
|||
|
||||
extern NSString * kASSerialPortsChanged;
|
||||
|
||||
@interface NSFileHandle (ExceptionSafety)
|
||||
|
||||
- (NSData *)availableDataIgnoringExceptions;
|
||||
|
||||
@end
|
||||
|
||||
@interface ASSerial : NSObject
|
||||
|
||||
+ (NSString *) fileNameForPort:(NSString *)port;
|
||||
|
|
|
@ -17,6 +17,19 @@ static NSMutableDictionary * savedAttrs;
|
|||
|
||||
NSString * kASSerialPortsChanged = @"PortsChanged";
|
||||
|
||||
@implementation NSFileHandle (ExceptionSafety)
|
||||
|
||||
- (NSData *)availableDataIgnoringExceptions {
|
||||
@try {
|
||||
return [self availableData];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
return [NSData data];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation ASSerial
|
||||
|
||||
+ (void)initialize {
|
||||
|
|
|
@ -146,7 +146,7 @@ class ASSerialWin: NSWindowController {
|
|||
serialData = ""
|
||||
logView.setString(serialData)
|
||||
portHandle!.readabilityHandler = {(handle) in
|
||||
let newData = handle.availableData
|
||||
let newData = handle.availableDataIgnoringExceptions()
|
||||
let newString = NSString(data: newData, encoding: NSASCIIStringEncoding)!
|
||||
self.serialData += newString
|
||||
dispatch_async(dispatch_get_main_queue(), { () -> Void in
|
||||
|
|
Loading…
Reference in New Issue
Block a user