0

我做了简单的应用程序连接到 tcp 流服务器(车载 OBD wifi 连接)。它可以连接并且运行良好 - 我可以发送消息并从汽车中获取答案。现在我只想从答案中读取指定的数据。当我向汽车发送请求时,例如 01041,它会回答如下内容:

搜索:好的

41 04 7楼

我想创建一个仅包含此答案的最后一个字节的变量。在本例中,它将是 7F。我想这样做是因为我想在 Label 中显示该数据。所以我需要跳过这个示例行搜索并跳过第 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

我在http://www.raywenderlich.com教程的帮助下完成了 tcp 连接,所以我不太了解它来解决我的问题。

这一行 [self.RPM setText:[self.RPM.text stringByAppendingString:output]]; 在标签中显示答案,但它显示了一切。我想我需要创建新的变量输出并以某种方式对其进行编辑,但我不知道如何

4

0 回答 0