I've been trying to figure this out when I feeling like pulling my hair out for the last couple of months now. I'm using an Uno R3 and interfacing with this https://www.sparkfun.com/products/9555
When the shield is connected a couple of commands needs to be sent to set the shield to work properly. I need to send the commands ATI and verify the response before moving on. The response is the version of the chip"Elm327 V1.3". If the chip doesn't respond with the version and instead returns with a "?" I need to reset the board by sending the command "ATZ".
Once I have read the board version the next set of commands I need to send are "AT L1" and "AT SP2" . After each command is sent the board will respond in 1 of 2 ways. If it's accepted it will respond with "OK", if it's not accepted it will respond with a "?". If I get an "Ok" response I can move on to the next command, if not I need to reset the board.
The final command I need to send is to check the baud rate, I send the command "ST CBR" and it will respond with a 3 to 7 digit number reflecting the current baud rate. Depending on the value it responds with I may also need to change the baud rate, if I do need to change it I would send "STSBR 115200" and receive the response of "OK".
I already know people work around what I'm trying to do with delay but none of them actually address reading the response and sending the next command based on how the shield responded. People just use delay, assign values and assume it's going to work correctly.
What I've been trying to do is figure out a way to send the command, wait for a response and compare the value and then either send the next command or send the command to reset the shield if the command was not accepted. Since I'm dealing with letters, numbers and a symbol in varying lengths of responses I don't even know where to start. I've tried this in so many different methods all ending the same way, they either won't compile, start an endless loop or totally ignore what the response was. I've deleted every sketch I've tried previously and I'm starting fresh.
I've read numerous posts and articles over the last couple of months but none of them address a mixed string or a string of unknown length, if they did it was not something that was clearly given as an example. I'm a visual learner so with out a working example it doesn't help me get what I need out of it.
I would be VERY grateful is someone could provide with with an example of sending even just one command, reading the response back and then determine if it should send the next command or reset(Command is ATZ).