Open-Source-Satellite / Mercury-GS

An Open Source Program that allows users to interact with a Spacecraft in a lab environment, pre-launch.
Other
47 stars 8 forks source link

'unpack requires a buffer of 12 bytes' runtime error #12

Closed marvin6x9 closed 2 years ago

marvin6x9 commented 2 years ago

The serial comms thread stops with runtime error 'unpack requires a buffer of 12 bytes' when two packets are received with no delay between them. Packets are observed to need a delay of 2 seconds between them to be processed correctly (1 second is not long enough). This appears to happen because the data field of the first packet is left in frame_buffer in addition to the new packet, so frame_buffer then contains far more bytes than the 12 which are expected. The condition can be avoided by adding a line to serial_comms.py after line 233 (time.sleep(1)) containing 'self.frame_buffer.clear()'. This may not be the best solution but is suggested to form part of the investigation.

KISPE-Jamie commented 2 years ago

Hey Kevin! I believe I have fixed this issue. You are correct. The frame_buffer does get populated with more than one frame if they are sent too quickly. I believe this is because the handler thread wasn't processing them quickly enough, and the time.sleep function was giving just enough time to process one frame if not sent too quickly. My solution was to use the blocking function .join() on the frame_queue, which blocks until the frame is handled by the handler thread, and then unblocks once that thread calls frame_queue.task_done(). Then a call to self.frame_buffer.clear() clears the buffer ready for the next incoming frame to be parsed.

Feel free to pull what I've done and test with your utility :) Then we can close this issue if the problem is resolved.