Don't crash when serial port gets disconnected
This commit is contained in:
parent
ccfaf34129
commit
de93b9e2a9
|
@ -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;
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user