iOS套接字编程 - 接收nsstream事件4

时间:2012-11-29 19:27:55

标签: ios sockets arduino nsstream

我正在使用一个名为WiFly的设备,其IP地址为:169.254.1.1和端口2000.我正在尝试通过iOS应用程序连接到此设备。我使用以下代码进行连接:

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

UInt32 port = 2000;
CFStringRef host = CFSTR("169.254.1.1");

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readStream, &writeStream);

inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;

// set the delegates to this view controller
[inputStream setDelegate:self];
[outputStream setDelegate:self];

// Set run loops to continuous receive information
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

// Finally, open the connection
[inputStream open];
[outputStream open];

然后我使用以下内容来处理流事件:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
    NSLog(@"stream event %i", streamEvent);

    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;

        case NSStreamEventHasBytesAvailable:
            if (theStream == inputStream) {

                uint8_t buffer[1024];
                int len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        if (nil != output) {
                            NSLog(@"server said: %@", output);
                            [self messageReceived:output];
                        }
                    }
                }
            }
            break;

        case NSStreamEventErrorOccurred:
            NSLog(@"Can't connect to server");
            break;

        case NSStreamEventEndEncountered:
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            break;

        default:
            NSLog(@"Unknown event");
    }

所以,我可以看到前两个流正确打开。然后它立即跟随一个流事件4,从我的理解是预期的。但是,我接着尝试调用函数:

- (IBAction)moveForward
{
    NSLog(@"move forward called");
    NSString *response  = [NSString stringWithFormat:@"2"];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
}

哪个应该从一个arduino uno通过wifly返回“前进”。但是,当我点击时,由于某种原因我得到另一个NSStreamEvent 4。我还通过终端telnet到设备:

  

telnet 169.254.1.1 2000

然后键入“2”......这会立即返回所需的“前进”。从iPad的角度来看,我做错了什么?

非常感谢帮助!

编辑由于某种原因,每次访问处理程序时,传递的流都是输出流,这是完全错误的...为什么输入流不能通过?

这是arduino代码:

void loop() 
{ while (Serial.available() > 0) {
    int next = Serial.parseInt();if(Serial.available() > 0)
    {
        if(next == 20) {
            Serial.println("leftMove");
 }}}}

0 个答案:

没有答案