Asynchronous Values Update using GCDAsynSocket api

Viewed 346

I am developing one application in which I need to update multiple values like Engine RPM,Speed etc. parameters at a time using OBD connector. I need to achieve asynchronous command/response . For this I am sending commands using [gcdAsyncSocket writeData:data withTimeout:-1 tag:uniqueTag]; asynchronously with unique tag.

But when gcdAsync's delegate method "socketDidReadDatawithTag" is called, it returns the data but it is not proper.e.g. If I have sent one command "010C\r" (Read RPM), and "010D\r" (Speed),with Tag 263 and 264 respectively ,and if I parse the response with tag 264 in socketDidReadDatawithTag , sometimes it returns me the data of RPM. (My response gets Mixed up or OBD Device is unable to handle asynchronous response)

    NSLog(@"Command Sent for Async : %@",commandString);
    NSData *data = [commandString dataUsingEncoding:NSASCIIStringEncoding];

    long obdObjectTag = [obdObject getPIDTag];//Unique Tag
    [gcdAsyncSocket writeData:data withTimeout:-1 tag:obdObjectTag];

    NSData *readData = [@">" dataUsingEncoding:NSASCIIStringEncoding];
    [gcdAsyncSocket readDataToData:readData withTimeout:-1 tag:obdObjectTag];

And in socketdidReadDatawithTag data and tag are mismatched.

1 Answers
Related