UART interrupt received data exceeds the buffer size

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
jk007
Posts: 9
Joined: Tue Nov 12, 2019 1:34 am

UART interrupt received data exceeds the buffer size

Post by jk007 » Wed Mar 18, 2020 8:00 am

Hi, I use UART(6) of pyb1.1,Firmware version is V1.11.

The code is as follows
#------------------------------------------------------------------
from pyb import UART,Switch
WT901_ISR_flag = 0
key = Switch()
def WT901_ISR(t):
global WT901_ISR_flag
WT901_ISR_flag = 1

WT901 = UART(6,9600)
WT901.init(baudrate=9600, bits=8, parity=None, stop=1, timeout=10, read_buf_len=64)
WT901.irq(trigger = WT901.IRQ_RXIDLE, handler = WT901_ISR)

while 1:
if WT901_ISR_flag == 1:
val = WT901.read()
WT901_ISR_flag = 0
print(val)
#------------------------------------------------------------------
When the data received by UART (6) exceeds the number of bits in the receive buffer(read_buf_len=64), the data received after the second time is empty.
I don't know how to deal with it. When UART (6) doesn't use interrupts, it can work normally even if the received data exceeds the receiving buffer, but the data still hasn't been taken away (it doesn't matter). It's mainly to find out how the use of interrupts can cause such problems. Thank you for checking

jk007
Posts: 9
Joined: Tue Nov 12, 2019 1:34 am

Re: UART interrupt received data exceeds the buffer size

Post by jk007 » Wed Mar 18, 2020 8:01 am

Sorry, the code indentation is invalid after release

User avatar
pythoncoder
Posts: 4578
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: UART interrupt received data exceeds the buffer size

Post by pythoncoder » Wed Mar 18, 2020 8:29 am

You seem to be using an undocumented method UART.irq. The pyb docs make no mention of this, and the machine docs say "Availability: WiPy.". Further the trigger value you're using doesn't seem to be documented. Where did you locate this information?

It may yet be working. If it raises an interrupt whenever the receiver is idle, then I'd expect you to see repeated interrupts when the receive buffer is empty. If you receive a long stream of data which overflows the buffer, the interrupt will only occur when the stream ends. Then it will repeat forever. Or so I'm guessing, never having encountered this IRQ ;)

It would help if you explained what you're trying to achieve. I suggest you look uasyncio if you want to do asynchronous UART reception. It's easier than writing interrupt handlers. Consider this example from the uasyncio tutorial which handles concurrent transmit and receive via a wire link.

Code: Select all

import uasyncio as asyncio
from pyb import UART
uart = UART(4, 9600)

async def sender():
    swriter = asyncio.StreamWriter(uart, {})
    while True:
        await swriter.awrite('Hello uart\n')
        await asyncio.sleep(2)

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.readline()
        print('Recieved', res)

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.create_task(receiver())
loop.run_forever()
Peter Hinch

jk007
Posts: 9
Joined: Tue Nov 12, 2019 1:34 am

Re: UART interrupt received data exceeds the buffer size

Post by jk007 » Sat Mar 21, 2020 1:01 am

Thank you very much for your answer. I used the asynchronous process recommended by you and solved my difficulties. Thank you again :lol:

Post Reply