TheThingsNetwork / arduino-device-lib

Arduino Library for TTN Devices
MIT License
207 stars 96 forks source link

Feature/prevent inf loop #210

Closed jpmeijers closed 7 years ago

jpmeijers commented 7 years ago

Prevent an infinite loop in the readLine function. An infinite loop might occur when the wrong Serial port is defined. Therefore print out an error in the showStatus function, returning after 10 seconds, rather than waiting indefinitely.

jpmeijers commented 7 years ago

while (!read && loops--) is indeed shorter, but I am not a fan of that style. I prefer to write longer code, which is more clear and less prone to bugs. In the end the compiler will anyway optimise it out.

while (loops--) is really difficult to read for someone with no or little C experience. I therefore agree with: https://github.com/TheThingsNetwork/arduino-device-lib/pull/205#issuecomment-306750723

But this comes down to style preference. If you want the library to follow your style I'll change it.

johanstokking commented 7 years ago

This should only be used after resetting the module, to test that there is actually a module out there.

Otherwise, the RN module is stable enough to respond with something. Setting an arbitrary timeout, even user defined, is causing trouble by having stuff available on the serial that causes issues with request/response expectations.

jpmeijers commented 7 years ago

The original reason I added this was to notify the user that communication with the RN was unsuccessful. This can be either because the wrong serial port was defined and passed to this library, or that the RN module is not responding (bad wiring, etc). In the current state the library will just hang indefinitely.