I have a development board which outputs A...Z
repeatedly through the USB serial port.
Then I created a simple Python script to read from the serial port device file /dev/ttyACM0
.
with open("/dev/ttyACM0", "rb") as sdf:
while count < 1000:
content = sdf.read(10)
count = count + 1 <=============== HERE
I run my script in debug mode and pause it randomly at HERE
, but still I don't see any characters missing. They are always continuous ABCD....Z
. Like below:
content
b'FGHIJKLMNO'
content
b'PQRSTUVWXY'
content
b'ZABCDEFGHI'
But the board is never paused. It output characters very fast.
- How could I never miss a single char?
Maybe this is related to how a char starts from the USB serial port and reaches my program.
There seems to be some buffer in the middle.
- Is the buffer on the board or on the Ubuntu host?
- How to check the buffer size if there is one?
- Will it be consumed up and will a wrap-around happen?
Some information:
I am using the Zephyr RTOS (https://github.com/zephyrproject-rtos/zephyr). The board I am using is the reel board (https://docs.zephyrproject.org/2.6.0/boards/arm/reel_board/doc/index.html).
ADD 1 - 10:08 PM 9/4/2022
This may be not exactly the same as what I asked here. But I just recall some UART driver code I wrote before.
In that code, if the UART hardware status is busy sending something, the sender will be blocked. I guess the flow control is kind of similar to this. When the receiver cannot handle more data, it must send some flow control signal to the sender, and indicated as some hardware status as well. And the driver should detect that in time and block sending request from the upper-layer application.
ADD 2 - 9:44 PM 9/12/2022
I did some more experiment.
I let the board to output A...Z
together with a timestamp every 1ms
. It outputs something like below:
[A, 1902277]
[B, 1902279]
...
Then I wrote a python script to read from /dev/ttyACM0
. Like below:
count = 0
with open("/dev/ttyACM0", "rb") as sdf:
while True:
content = sdf.readline()
count = count + 1
sys.stdout.write(content.decode("utf-8", "replace"))
if count%100 == 0:
sys.stdout.flush()
Every time I launched the python script, it output something like this:
[p, 1871972] <------------ python script starts
[q, 1871974]
[r, 1871977]
[s, 1871979]
[t, 1871 1902214] <============= PLACE 1
[c, 1902217]
[d, 1902219]
[e, 1902221]
[f, 1902224]
[g, 1902226]
[h, 1902229]
It seems a short while right after the script starts, a bumpy output happens at PLACE 1. And I run the script for several seconds. I didn't see any more bumpy output after PLACE 1.
Why the bumpy always happens at the beginning?
I am wondering if there'll be more bumpy outputs if I run the script long enough. i.e. The host OS cannot catch up with the board's output.
And it seems the board and the host doesn't have much flow-control agreement. Otherwise there should be no bumpy result. The board just keeps outputting no matter the host can catch it up or not.