Asynchronous Values Update using GCDAsynSocket api

374 Views Asked by At

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

There are 1 best solutions below

1
On

The OBD-II connector (I assume it's an ELM-327) cannot really handle asynchronous calls as far as I know.

It cannot handle multiple requests at once. You send 1 command, and the OBD-II device will gather that info from the OBD-bus and return with an answer. Then it will process your next command. Ofcourse, the commands you send end up in a buffer, that will be processed one by one. I'm thinking this might be a problem for you to make it, but I'm not sure.

I'm not familiar at all with the ios programming and what happens with those tags. You set those tags to identify what paramters are the data for? In the reply data, you can also see for what parameter it is meant, so in the answer itself you can see that data represents RPM or Speed etc.

I hope the OBD-II part has shed some light on it. Will check this question more for some discussion perhaps.