Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid busy looping in rx thread #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pyusbtin/usbtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def stop_rx_thread(self):

# tell the thread to exit
self.rx_thread_state = USBtin.RX_THREAD_TERMINATE
self.serial_port.cancel_read()
while self.rx_thread_state != USBtin.RX_THREAD_STOPPED:
# wait for thread to end, sleep 1ms
sleep(0.001)
Expand All @@ -272,11 +273,9 @@ def rx_thread(self):

""" process data as long as requested """
while self.rx_thread_state == USBtin.RX_THREAD_RUNNING:
# fetch bytes if available
rxcount = self.serial_port.inWaiting()
if rxcount > 0:
# fetch all data from serial buffer
buf = self.serial_port.read(rxcount)
# fetch at least one byte of data from serial buffer
buf = self.serial_port.read(max(1, self.serial_port.in_waiting))
if buf:
if PY_VERSION == 2:
buf = [ord(b) for b in buf]

Expand Down