FROM : Wan, Nathan (CIV)
DATE : Fri Jun 27 22:32:33 2008
Hi all
I'm new to Objective-C and Cocoa and I am having trouble with the notifications system. This I thought was just a small project to write a native mac program to continuously read and write data from a serial port to a file. It was suggested I use NSFileHandle to maintain safe threads, as it can read the serial port asynchronously; I had hoped this program would read the text file, and as I changed it, there would be an output to the console. Something simple to see the notification system in action:
//CODE
#import <Cocoa/Cocoa.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSString.h>
#import <Foundation/NSNotificationCenter.h>
#import <stdio.h>
@interface test : NSObject
-(void) writeDataReadInBackground:(NSNotification *)notification;
-(void) testWriteData:(NSData *)data;
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *aData = [@"Test" dataUsingEncoding: NSASCIIStringEncoding];
NSFileHandle *in = [NSFileHandle fileHandleForReadingAtPath:@"/Users/_me_/test.txt"];
if(in == nil)
printf("somethig wrong\n");
test *t = [[test alloc] init];
[[NSNotificationCenter defaultCenter] addObserver: t selector:@selector(writeDataReadInBackground:) name: NSFileHandleDataAvailableNotification object: nil];
[t testWriteData: aData];
[in waitForDataInBackgroundAndNotify];
while(1)
sleep(5);
[t release];
[pool release];
return 0;
}
@implementation test
- (void) writeDataReadInBackground:(NSNotification *)notification {
printf("Notification recieved\n");
}
- (void) testWriteData:(NSData *)data {
NSFileHandle *stdOut = [NSFileHandle fileHandleWithStandardOutput];
[stdOut writeData:data];
}
@end
//END_CODE
Thanks for your help
Nathan
PS What's the proper way to work between NSString and NSData?
DATE : Fri Jun 27 22:32:33 2008
Hi all
I'm new to Objective-C and Cocoa and I am having trouble with the notifications system. This I thought was just a small project to write a native mac program to continuously read and write data from a serial port to a file. It was suggested I use NSFileHandle to maintain safe threads, as it can read the serial port asynchronously; I had hoped this program would read the text file, and as I changed it, there would be an output to the console. Something simple to see the notification system in action:
//CODE
#import <Cocoa/Cocoa.h>
#import <Foundation/NSFileHandle.h>
#import <Foundation/NSString.h>
#import <Foundation/NSNotificationCenter.h>
#import <stdio.h>
@interface test : NSObject
-(void) writeDataReadInBackground:(NSNotification *)notification;
-(void) testWriteData:(NSData *)data;
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *aData = [@"Test" dataUsingEncoding: NSASCIIStringEncoding];
NSFileHandle *in = [NSFileHandle fileHandleForReadingAtPath:@"/Users/_me_/test.txt"];
if(in == nil)
printf("somethig wrong\n");
test *t = [[test alloc] init];
[[NSNotificationCenter defaultCenter] addObserver: t selector:@selector(writeDataReadInBackground:) name: NSFileHandleDataAvailableNotification object: nil];
[t testWriteData: aData];
[in waitForDataInBackgroundAndNotify];
while(1)
sleep(5);
[t release];
[pool release];
return 0;
}
@implementation test
- (void) writeDataReadInBackground:(NSNotification *)notification {
printf("Notification recieved\n");
}
- (void) testWriteData:(NSData *)data {
NSFileHandle *stdOut = [NSFileHandle fileHandleWithStandardOutput];
[stdOut writeData:data];
}
@end
//END_CODE
Thanks for your help
Nathan
PS What's the proper way to work between NSString and NSData?
| Related mails | Author | Date |
|---|---|---|
| Wan, Nathan (CIV) | Jun 27, 22:32 | |
| Ken Thomases | Jun 28, 02:11 | |
| Jason Coco | Jun 28, 02:32 |






Cocoa mail archive

