How to separate the received data? using asyncsocket

I am trying to create an application that can connect to a device and request a request. To have an answer, I need to send an NSString command first. I have it in a loop with NSTimer .

Generally in Interface Builder

Read: SIDDA1999AP

and then after a while changed to

Read: @ REL10345

and so on, until the last line sends @ "H \ r \ n" and then returns to the first.

Part of my Asyncsocket code base

 - (void)viewDidLoad { [super viewDidLoad]; // Read Data as loaded NSTimer *t; t = [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector:@selector(onTick:) userInfo: nil repeats:YES]; } -(void)onTick:(NSTimer *)timer { sendStr = @"S\r" @"@\r\n" @"P\r\n" @"H\r\n" ; NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding]; [asyncSocket writeData:sendData withTimeout:-1.0 tag:0]; [asyncSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0]; } - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { DDLogVerbose(@"socket:didReadData:withTag:"); NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; componentsSeparatedByString:@"L"]objectAtIndex:6]; [self debugPrint2:[NSString stringWithFormat:@"Read: \n%@",response]]; [response release]; [asyncSocket readDataWithTimeout:-1 tag:0]; } 

debugPrint2 where I display my text.

I want to extract a string from debugPrint and display it separately. Because his loop data will be dynamic. I also need to extract the substring (short for SIDDA1999AP to 1999 ). In other words, remove the prefix and suffix.

My xib would like

 Data1: 1999 Data2: 10345 .... 

Here is a picture of my application:

enter image description here

I hope someone can help.

0
source share

Source: https://habr.com/ru/post/1416211/


All Articles