OBD WiFi アダプターを介して車の車両識別番号 (VIN) を読み取りたいです。アダプターへの ping は可能で、修正された IP アドレスとポートを取得しました。
ここで、ストリームで 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 マルチフレーム応答を返すコマンドです。
想像できること、またはワールドワイドウェブで見つけたすべてを実際に試しました。私は立ち往生していて、それ以上わからないので、ここの誰かが私を助けてくれることを願っています.