UART Receive Problem

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
yoyicue
Posts: 1
Joined: Mon Aug 04, 2014 4:23 am

UART Receive Problem

Post by yoyicue » Mon Aug 04, 2014 4:43 am

I can not Receive more than 1 Byte of data with UART.
I sent 4 Byte:

Code: Select all

>>> u = pyb.UART(4, 9600)
>>> u.recv(1)
b'a'
I sent 4 Byte again:

Code: Select all

>>> u.recv(4)
Traceback (most recent call last):
  File "<stdin>", line 0, in <module>
Exception: HAL_UART_Receive failed with code 3
What is wrong?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART Receive Problem

Post by dhylands » Mon Aug 04, 2014 6:59 am

Error 3 is a timeout. Since you didn't specify a timeout on your recv, it uses a default of 5 seconds.

It means that 4 characters didn't arrive within 5 seconds.

I'm not sure what the intended behaviour is for receiving less than the number of characters that you asked for. Generally I would have expected that if only 3 characters arrived, then it would return those 3 character rather than throwing an exception.

So, for the time being, you should read one character at a time. I'll post an issue and see what Damien says.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: UART Receive Problem

Post by Damien » Mon Aug 04, 2014 8:09 am

As dhylands say, it's timing out because 4 characters were not available.

To check for a timeout you can do:

try:
c = uart.recv(1)
except:
print('timeout')

This will work for getting 1 character, but if you want to get more than 1 you will need to put the above in a loop.

We'll work on this problem to get it fixed properly.

Post Reply