Simpit-team / KerbalSimpitRevamped

A revamp of the Kerbal Simpit Mod for KSP
BSD 2-Clause "Simplified" License
26 stars 15 forks source link

Reset PayloadBuffer every time Port is opened or in the event of an overflow #94

Closed rettoph closed 6 months ago

rettoph commented 6 months ago

Running https://github.com/Simpit-team/KerbalSimpitRevamped-Arduino/blob/main/examples/KerbalSimpitHelloWorld/KerbalSimpitHelloWorld.ino#L34 I often get an IndexOutOfRange exception on handshake and the port status hangs on WAITING_HANDSHAKE. image This does not occur every time, and often only once per Arduino power cycle. Relaunching KSP often fixes it.

That said, it can be a very annoying issue with multiple mods installed and longer game load times. Manually closing and re-opening the port in game does not fix the issue when this occurs.

After some debugging I've found that, for some reason, the Arduino is sending several messages containing nothing but a single byte value: image The screenshot above demonstrates the contents of 3 incoming messages, followed by a properly formed Handshake. The initial 3 messages contain nothing but the byte 0x47

In my debugging I've seen 3 possible values: 0x7, 0x47, and 0x83. After a quick glance at https://github.com/Simpit-team/KerbalSimpitRevamped-Arduino/ I found no obvious cause for this data spam or significance behind the listed byte values.

In these instances KSPSerialPort::RecievedDataEvent never receives a 0x0 byte and thus the overflow is allowed to happen.

Currently, a newly opened port never resets the CurrentBytesRead value, which prevents simply closing/re-opening the port from resolving the issue.


Changes in this PR will:

  1. Ensure CurrentBytesRead gets reset each time a port is opened
  2. Reset CurrentBytesRead in the event of a PayloadBuffer overflow, discarding all remaining message contents at that time
PBechon commented 6 months ago

Thanks for the catch, I have added it in the develop branch and it will be integrated in the next release.