TCPストリームサーバーに接続するシンプルなアプリを作成しました(車内のOBD wifi接続)。接続も動作も非常に良好です。車からメッセージを送信したり、応答を取得したりできます。ここで、回答から指定されたデータのみを読み取りたいと思います。たとえば 01041 のように車にリクエストを送信すると、次のような応答が返されます。
検索: OK
41 04 7F
この回答の最後のバイトのみを含む変数を作成したいと思います。この例では7Fになります。そのデータを Label に表示したいので、これを行いたいと思います。したがって、この例の行 Searching でスキップし、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]]; ラベルに回答が表示されますが、すべてが表示されます。新しい変数出力を作成して何らかの方法で編集する必要があると思いますが、方法がわかりません