- 1 year ago
pySerial in_waiting has variable access time
The in_waiting
property in the pySerial
library returns the number of bytes in the input buffer. However, the access time to in_waiting
can vary depending on the underlying hardware and operating system. Here are a few factors that can affect the access time:
Hardware Buffering: The behavior of the serial port hardware and its buffering mechanism can influence the access time to the
in_waiting
property. The amount of data buffered by the hardware before it becomes available to the software can impact the reported value.Operating System Behavior: Different operating systems may have different mechanisms for handling serial port data buffering and reporting. The way the operating system interacts with the serial port driver can affect the reported
in_waiting
value and the access time.Serial Port Settings: The configuration settings of the serial port, such as baud rate, data bits, stop bits, and flow control, can influence the behavior of the data buffering and reporting. Changing these settings may affect the reported
in_waiting
value and the access time.Data Transmission Speed: The speed at which data is transmitted over the serial port can impact the reporting of
in_waiting
. If data is transmitted at a high rate, there might be a delay between the data arriving at the serial port and it being reported byin_waiting
.
To handle variable access time to in_waiting
, it's a good practice to periodically check the property in a loop or as part of a continuous reading process. You can monitor the in_waiting
value over time and respond accordingly based on your application's requirements.
For example:
pythonimport time
import serial
ser = serial.Serial('COM1', 9600)
while True:
if ser.in_waiting > 0:
data = ser.read(ser.in_waiting)
print(f"Received: {data}")
time.sleep(0.1) # Adjust the sleep duration as per your application's requirements
In this example, the in_waiting
property is checked periodically in a loop, and if there are bytes in the input buffer, they are read and processed accordingly. The loop is designed to continuously monitor and respond to incoming data, accounting for any variations in the access time of in_waiting
.