سؤال

I am developing an OBD2 scanner app for windows using C#. I was hoping someone could tell me the optimal time to wait for a response (using thread.Sleep()) from the car's ecu before reading the response from the serial port.

I am developing using a bluetooth adapter and the Baud rate is 9600. I've currently got my wait period set to 100ms, but I am hoping to go much shorter than that if possible. I just don't want to run into a situation where I try to read data when the port is not ready. I want to get a good trade-off between a smooth GUI and comfortable margins for error.

UPDATE: thanks of the ideas. Handling DataReceived is a good general idea but the reason I'm sleeping a thread is that I need to poll the ECU repeatedly at intervals so that I can do things like update an RPM gauge. I am trying to find a sweetspot where I am getting as many reps as possible from the ECU, without overwhelming it. If I were polling it once only, then simply handling an event would be fine, but I need to know what an acceptable interval would be between polls. I will definitely change my code to be event-driven, but I still need to know how long is a respectable time between polls.

هل كانت مفيدة؟

المحلول 2

I decided to follow the lead of my car's built-in trip computer and refresh the values once every 1000ms, or more accurately, to refresh 10 values per second, one at a time at 100ms, 200ms, 300ms, etc.

I also prioritised things that change more often, like instant consumption, RPM and a few specialised sensors. Things like battery voltage, temperature and fuel levels only need to realistically be polled every 5, 10 and 20 seconds respectively, so there's lots of room for prioritisation.

نصائح أخرى

Instead of using Thread.Sleep(), it may be more appropriate to spawn a background thread that continuously tries to read from the stream. When the data has been fully read, then fire an event for the main thread to handle. As long as you have opened the port, there shouldn't be a problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top