Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从流中获取指定的数据?_Ios_Objective C_Tcp_Obd Ii - Fatal编程技术网

Ios 如何从流中获取指定的数据?

Ios 如何从流中获取指定的数据?,ios,objective-c,tcp,obd-ii,Ios,Objective C,Tcp,Obd Ii,我做了一个连接到tcp流服务器的简单应用程序(车载OBD wifi连接)。 它的连接和工作相当好-我可以发送信息,并从车上得到答案。现在我只想从答案中读取指定的数据。当我向car发送请求时,例如01041,它将回答如下内容: 搜索:好的 41 04 7F 我想创建一个变量,它只包含这个答案的最后一个字节。在本例中,它将是7F。我想这样做是因为我想在标签中显示数据。所以在这个例子中,我需要跳过行搜索,跳过第2行的前2个字节,只得到7F。我怎么能做到 @implementation ViewC

我做了一个连接到tcp流服务器的简单应用程序(车载OBD wifi连接)。 它的连接和工作相当好-我可以发送信息,并从车上得到答案。现在我只想从答案中读取指定的数据。当我向car发送请求时,例如01041,它将回答如下内容:

搜索:好的

41 04 7F

我想创建一个变量,它只包含这个答案的最后一个字节。在本例中,它将是7F。我想这样做是因为我想在标签中显示数据。所以在这个例子中,我需要跳过行搜索,跳过第2行的前2个字节,只得到7F。我怎么能做到

   @implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)initNetworkCommunication {
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.0.10", 35000, &readStream, &writeStream);
    self.inputStream = objc_unretainedObject(readStream);
    self.outputStream = objc_unretainedObject(writeStream);
    [self.inputStream setDelegate:self];
    [self.outputStream setDelegate:self];
    [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [self.inputStream open];
    [self.outputStream open];
}
- (IBAction)Connect:(id)sender {
    [self initNetworkCommunication];
}

- (IBAction)Play:(id)sender {
    NSString *response  = [NSString stringWithFormat:@"01041\r\n"];
    [NSThread sleepForTimeInterval:0.05];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];

}
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
    switch (eventCode) {

        case NSStreamEventOpenCompleted:
            [self.logTextView setText:[self.logTextView.text stringByAppendingString:@"Stream opened\n"]];
            break;

        case NSStreamEventHasBytesAvailable:

            if (aStream == self.inputStream) {

                uint8_t buffer[1024];
                long len;

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


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

                        if (nil != output) {
                            [self.logTextView setText:[self.logTextView.text stringByAppendingString:output]];
                            [self.RPM setText:[self.RPM.text stringByAppendingString:output]];
                        }
                    }
                }
            }
            break;

        case NSStreamEventErrorOccurred:
            [self.logTextView setText:[self.logTextView.text stringByAppendingString:@"Can not connect to the host!"]];
            break;

        case NSStreamEventEndEncountered:
            break;

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

}

- (IBAction)sendCode:(id)sender {

    NSString *command = self.commandTextField.text;

    NSString *response  = [NSString stringWithFormat:@"%@\r\n", command];

    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
    [self.outputStream write:[data bytes] maxLength:[data length]];
}
@end
我在教程的帮助下完成了tcp连接,所以我不太明白如何解决我的问题


此行[self.RPM setText:[self.RPM.text stringByAppendingString:output]];在标签中显示答案,但它显示所有内容。我想我需要创建新的变量输出并以某种方式进行编辑,但我不知道我是如何尝试在web上查找一些信息的,现在我想我需要使用RegularExpression来完成这项工作,对吗?解决了!如上所述,我使用了RegularExpression,效果很好!