通过TCP写入流但没有响应

时间:2012-12-13 16:18:39

标签: objective-c tcp stream obd-ii

我想通过OBD WiFi适配器读出汽车的车辆识别码(VIN)。 可以ping适配器,它有一个修复IP地址和端口。

现在我想在Stream上发送VIN请求,但没有任何反应。

我的.h文件

@interface Communicator : NSObject <NSStreamDelegate> {
@public

NSString *host;
int port;
}

- (void)setup;
- (void)open;
- (void)close;
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event;
- (void)readIn:(NSString *)s;
- (void)writeOut:(NSString *)s;

@end

我的.m文件

#import "Communicator.h"

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
NSInputStream *inputStream;
NSOutputStream *outputStream;

@implementation Communicator

- (void)setup {
NSURL *url = [NSURL URLWithString:host];

NSLog(@"Setting up connection to %@ : %i", [url absoluteString], port);

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)[url host], port, &readStream, &writeStream);

if(!CFWriteStreamOpen(writeStream)) {
    NSLog(@"Error, writeStream not open");
    return;
}
[self open];

NSLog(@"Status of outputStream: %i", [outputStream streamStatus]);
return;
}

- (void)open {
NSLog(@"Opening streams.");

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

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

if ([inputStream hasBytesAvailable])
{
    NSLog(@"We can recieve something from the InputStream");
}
if ([outputStream hasSpaceAvailable])
{
    NSLog(@"We can send something trough the OutputStream");
}

}

- (void)close {
NSLog(@"Closing streams.");
...
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event {
NSLog(@"Stream triggered.");

NSLog(@"event: %u", event);

switch(event) {
    case NSStreamEventHasSpaceAvailable: {
        if(stream == outputStream) {
            NSLog(@"outputStream is ready.");
        }
        break;
    }
    case NSStreamEventHasBytesAvailable: {
        if(stream == inputStream) {
            NSLog(@"inputStream is ready.");

            uint8_t buf[1024];
            unsigned int len = 0;

            len = [inputStream read:buf maxLength:1024];

            if(len > 0) {
                NSMutableData* data=[[NSMutableData alloc] initWithLength:0];

                [data appendBytes: (const void *)buf length:len];

                NSString *s = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

                [self readIn:s];

            }
        }
        break;
    }
    default: {
        NSLog(@"Stream is sending an Event: %i", event);

        break;
    }
}
}

- (void)readIn:(NSString *)s {
NSLog(@"Reading in the following:");
NSLog(@"%@", s);
}

- (void)writeOut:(NSString *)s {
uint8_t *buf = (uint8_t *)[s UTF8String];
[outputStream write:buf maxLength:strlen((char *)buf)];
NSLog(@"Status of outputStream: %i", [outputStream streamStatus]);

NSError *theError = [outputStream streamError];
NSLog(@"Error: %@", theError);

NSLog(@"Writing out the following:");
NSLog(@"%@", s);
}

@end 

我的主要

@autoreleasepool {

Communicator *c = [[Communicator alloc] init];

c->host = @"https://169.254.1.10";
//c->host = @"http://169.254.1.10";
//c->host = @"169.254.1.10";
c->port = 23;

[c setup];
[c open];

[c writeOut:@"0902"];

do {
} while (1);

         }
return 0;
}

控制台说:

2012-12-13 16:22:41.319 obdlink[1940:f803] Setting up connection to https://169.254.1.10 : 23
2012-12-13 16:22:55.409 obdlink[1940:f803] Opening streams.
2012-12-13 16:23:16.504 obdlink[1940:f803] Status of outputStream: 1
2012-12-13 16:23:20.878 obdlink[1940:f803] Opening streams.
2012-12-13 16:23:33.858 obdlink[1940:f803] Status of outputStream: 2
2012-12-13 16:23:46.092 obdlink[1940:f803] Error: (null)
2012-12-13 16:23:59.011 obdlink[1940:f803] Writing out the following:
2012-12-13 16:24:01.104 obdlink[1940:f803] 0902

“0902”是应该使用VIN返回5x5多帧响应的命令。

我真的尝试了我能想到的一切,或者我在万维网上找到的东西。 我卡住了,不知道进一步,我希望这里有人可以帮助我。

0 个答案:

没有答案