0

I am trying to make a app that can connect to a device and ask for a query. To be able to have the a reply, I need to send an NSString command first. I have it in a loop with NSTimer.

I normally have like this in Interface Builder

Read: SIDDA1999AP

then after some time changed to

Read: @REL10345

and so on until the last string send @"H\r\n" then back to the first one.

Part of my code base in Asyncsocket

- (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 string from debugPrint and display it individually. Because its in a loop data would be dynamic. I also need to extract a substring (shorten from SIDDA1999AP to 1999). In other words, remove the prefix and suffix.

My xib would have like

Data1: 1999
Data2: 10345
....

Here's an image of my application in progress:

enter image description here

I hope somebody could help.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Bren
  • 1
  • 4
  • Welcome to StackOverflow. Please use the preview available below your question as you're typing it to check things like formatting of code and text, and try to follow the appearance of other posts here. It makes your questions much easier to understand, and also means that other people don't have to edit them to clean them up for you. :) Thanks. – Ken White Nov 24 '11 at 00:02
  • Hi Bren! I see you recently posted a comment on http://stackoverflow.com/questions/10902710/how-do-i-read-data-using-cocoaasyncsocket/10903095#10903095. Is this thread/question still active then? I might be able to help. But question here is pretty unclear. – Rok Jarc Nov 02 '12 at 11:31

0 Answers0