How to get specified data from stream?

152 Views Asked by At

I did simple app that connects to tcp stream server(OBD wifi connection in car). It connects and works pretty well - I can send messages and get answers from car. Now I want to read only specified data from answer. When I send request to car for example 01041 it will answer something like this:

SEARCHING: OK

41 04 7F

I want create a variable that will contain only last bytes of this answer. In this example it will be 7F. I want do this because I'd like to present that data in Label. So I need to skip in this example line Searching and skip first 2 bytes in line 2 to get only 7F. How I can do it?

   @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

I did that tcp connection with help from tutorial from http://www.raywenderlich.com so I don't understand it that much to solve my problem.

This line [self.RPM setText:[self.RPM.text stringByAppendingString:output]]; shows answer in Label but it shows everything. I think I need to create new variable output and edit it somehow but I don't know how

0

There are 0 best solutions below