views:

249

answers:

1

my source-code as belowed :

a part of source-code as belowed :

- (id)init
{
 NSFileHandle *fh =[[NSFileHandle alloc] initWithFileDescriptor:myfileDescriptor closeOnDealloc:YES];  
 NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];  

 [notificationCenter addObserver:self   selector: @selector(getData:)
                                 name:  NSFileHandleReadCompletionNotification
                                 object:  fh ]; 

 [fh readInBackgroundAndNotify];
}

- (void) getData: (NSNotification *)aNotification
{
 NSLog(@"notification received");
}

note : The filedescript "myfileDescriptor" ,it ok ,i can receive data using [fh availableData] , but whenever ,i can't receive any notificaton from NSFilehandle .

please help me to solve the question .thanks .

A: 

thanks for your answer . please keep your attenation for it .

i develop in mac ,the os version=10.5.8

the more source-code as belowed:

// // serialTest.m // qt_simulator // // Created by diags on 12/17/09. // Copyright 2009 Foxconn. All rights reserved. //

import "serialTest.h"

@implementation serialTest -(id)init { [super init] ; filehandle = nil ; mythread = nil ; bEnable = false ; NSString *temp = @"/dev/cu.usbserial-A8003ROM" ;

[self configSerialPort] ; filehandle = [[NSFileHandle alloc] initWithFileDescriptor:filedescription closeOnDealloc:YES ] ;

if (filehandle==nil) { NSLog(@"\n---obtain the serial port fail 2\n"); return self ; } NSLog(@"\n---obtain the serial port successful ,filedescripter =%d\n",[filehandle fileDescriptor]); mythread = [[NSThread alloc] initWithTarget:self selector:@selector(filehandleThread) object:nil ] ;

/notification test start/ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileHandleDataAvailable:) name:nil object:filehandle] ; /notification test end/

[filehandle acceptConnectionInBackgroundAndNotify]; return self ; }

-(void)dealloc { bEnable = false ; [mythread cancel] ; [mythread release] ;

[filehandle closeFile] ; [filehandle release] ;

NSLog(@"\n------------dealloc-----------------n");; [[NSNotificationCenter defaultCenter] removeObject:self] ; [super dealloc] ; }

-(void)filehandleThread { while (bEnable) {

if 0

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init] ;

NSDate *temp ;
temp =[filehandle readDataToEndOfFile] ;

if (temp ==nil)
{
    [pool release] ;
    continue ;
}

if ([temp length]>0) { NSLog(@"\n---giga---receive data length=%d\n",[temp length]) ; NSString *strTmp = [[NSString alloc] initWithData:temp encoding:1];

     [textview insertText:strTmp] ;

[strTmp release] ; } [pool release ] ;

else

[filehandle readInBackgroundAndNotify] ; //NSLog(@"\n---giga---receive data \n");

endif

} }

-(IBAction) btn_start:(id)sender { bEnable = true ; [mythread start] ; }

-(IBAction)btn_stop:(id)sender { bEnable = false ; [mythread cancel] ; }

-(void)configSerialPort { filedescription = open("/dev/cu.usbserial-A8003ROM",O_RDWR|O_NOCTTY|O_NDELAY|O_EXLOCK) ; //filedescription = open("/dev/cu.usbserial-A8003ROM",O_ASYNC|O_EVTONLY) ; tcgetattr(filedescription,&serialOption) ; cfsetispeed(&serialOption, B115200) ; cfsetospeed(&serialOption, B115200) ; serialOption.c_cflag &=~PARENB ; serialOption.c_cflag &=~CSTOP ; serialOption.c_cflag &=~CSIZE ; serialOption.c_cflag |=CS8 ;

serialOption.c_cc[VMIN] = 0 ; serialOption.c_cc[VTIME] = 1 ; serialOption.c_cflag |=(CLOCAL|CREAD) ;

if (tcsetattr(filedescription,TCSANOW,&serialOption)!=0 ) { NSLog(@"\n configure serial port fail filedescript =%d\n",filedescription ); return ; } NSLog(@"\n configure serial port successful filedescript =%d\n",filedescription ); return ; }

  • (void)fileHandleDataAvailable:(NSNotification*)note ; { NSLog(@"\n----giga giga receive notification-----notification=%@---\n",note); } @end
GigaJin