Question

I am trying to use a Java library to communicate with a car via the serial port using OBD2 protocol. The protocol is simple: you send an ASCII string (e.g. "01 0d"), and the car answers with an ASCII value. I've found many libraries in the web, but there is one concept I don't understand in the examples. After every send command, the programmer put a call to sleep. Why is that? For example:

send(pid)
sleep(200)
receive(response)

I don't understand, because read is a blocking function call, so I should be able to wait on read. Why is the additional call to sleep?

Était-ce utile?

La solution

I did a bunch of work with the (Mitsubishi/Subaru) MUT-II protocol a few years ago, which uses the ISO9141 protocol and it was the same way. 200ms pause after every single request. It was later confirmed by the community/forums that the only pause that was actually necessary was the one after the initial 5 baud init, once changed to 10400 no more pauses were necessary.

Autres conseils

If you are going via a hardware interface (like OBDKey or a similar ELM327 based device) then the protocol timings are taken care of for you, so that is unlikely to be the cause of the sleep delay.

You are right, read does block. But note that there can be a timeout set up in the read mechanism when establishing the COM / serial port parameters. In this case a call to read returns with some or no data when the timeout expires.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top